String的getBytes()默认编码问题
1
2
3
|
public byte[] getBytes ( ) { return StringCoding.encode ( value , 0 , value .length ) ; } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
static byte[] encode ( char[] ca , int off , int len ) { String csn = Charset.defaultCharset ( ) . name ( ) ; try { / / use charset name encode ( ) variant which provides caching. return encode ( csn , ca , off , len ) ; } catch ( UnsupportedEncodingException x ) { warnUnsupportedCharset ( csn ) ; } try { return encode ( "ISO-8859-1" , ca , off , len ) ; } catch ( UnsupportedEncodingException x ) { / / If this code is hit during VM initialization , MessageUtils is / / the only way we will be able to get any kind of error message . MessageUtils.err ( "ISO-8859-1 charset not available: " + x.toString ( ) ) ; / / If we can not find ISO -8859 -1 ( a required encoding ) then things / / are seriously wrong with the installation. System.exit ( 1 ) ; return null; } } |
1
|
String csn = Charset.defaultCharset ( ) . name ( ) ; |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
public static Charset defaultCharset ( ) { if ( defaultCharset = = null ) { synchronized ( Charset. class ) { String csn = AccessController.doPrivileged ( new GetPropertyAction ( "file.encoding" ) ) ; Charset cs = lookup ( csn ) ; if ( cs ! = null ) defaultCharset = cs; else defaultCharset = forName ( "UTF-8" ) ; } } return defaultCharset; } |
1
2
3
4
5
6
7
|
public class Demo { public static void main ( String[] args ) { System.out.println ( "file.encoding=" + System.getProperty ( "file.encoding" ) ) ; byte[] bytes = "黑马程序员" .getBytes ( ) ; System.out.println ( Arrays.toString ( bytes ) ) ; } } |
1
2
|
file .encoding = UTF -8 [ -23 , -69 , -111 , -23 , -87 , -84 , -25 , -88 , -117 , -27 , -70 , -113 , -27 , -111 , -104 ] |
01
02
03
04
05
06
07
08
09
10
11
12
13
|
@WebServlet ( "/user/findAllServlet" ) public class ServletDemo 1 extends HttpServlet { protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { System.out.println ( "findAllServlet..." ) ; System.out.println ( "file.encoding=" + System.getProperty ( "file.encoding" ) ) ; byte[] bytes = "黑马程序员" .getBytes ( ) ; System.out.println ( Arrays.toString ( bytes ) ) ; } protected void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { this.doPost ( request , response ) ; } } |
1
2
|
file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
发现事与愿违.file.encoding依然是UTF-8的.也就是说file.encoding跟文件的编码没有任何关系.相同的代码.我们用命令行窗口去编译和运行.发现file.encoding竟然变成了GBK
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
我们讲Demo.java文件改成UTF-8编码格式的用 javac -encoding utf-8 Demo.java进行编译.然后运行发现file.encoding竟然没有变:
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
这说明file.encoding跟编译环境没有关系.只跟运行环境有关.我们尝试运行的时候指定file.encoding编码:
1
|
java - Dfile.encoding = utf -8 Demo |
运行结果就是:
1
2
|
file .encoding = utf -8 [ -23 , -69 , -111 , -23 , -87 , -84 , -25 , -88 , -117 , -27 , -70 , -113 , -27 , -111 , -104 ] |
运行时将file.encoding指定成gbk
1
|
java - Dfile.encoding = gbk Demo |
运行结果就是:
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
到此我们就知道了.Tomcat在启动的时候将file.encoding指定成了gbk.所以我们在Servlet中获取字节码数组的时候,默认用的就是gbk.
Tomcat是根据当前操作系统来设置file.encoding的值.我电脑是windows简体中问的.所以默认就是GBK的.
我们可以在catalina.bat中将file.encoding设置成utf-8.
1
|
set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dfile.encoding=UTF-8" |
重启Tomcat.再访问我们的Servlet.file.encoding就改成UTF-8的了.
至此,困扰我们的getBytes()默认编码格式问题就解决了.
1
2
3
|
public byte[] getBytes ( ) { return StringCoding.encode ( value , 0 , value .length ) ; } |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
|
static byte[] encode ( char[] ca , int off , int len ) { String csn = Charset.defaultCharset ( ) . name ( ) ; try { / / use charset name encode ( ) variant which provides caching. return encode ( csn , ca , off , len ) ; } catch ( UnsupportedEncodingException x ) { warnUnsupportedCharset ( csn ) ; } try { return encode ( "ISO-8859-1" , ca , off , len ) ; } catch ( UnsupportedEncodingException x ) { / / If this code is hit during VM initialization , MessageUtils is / / the only way we will be able to get any kind of error message . MessageUtils.err ( "ISO-8859-1 charset not available: " + x.toString ( ) ) ; / / If we can not find ISO -8859 -1 ( a required encoding ) then things / / are seriously wrong with the installation. System.exit ( 1 ) ; return null; } } |
1
|
String csn = Charset.defaultCharset ( ) . name ( ) ; |
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
public static Charset defaultCharset ( ) { if ( defaultCharset = = null ) { synchronized ( Charset. class ) { String csn = AccessController.doPrivileged ( new GetPropertyAction ( "file.encoding" ) ) ; Charset cs = lookup ( csn ) ; if ( cs ! = null ) defaultCharset = cs; else defaultCharset = forName ( "UTF-8" ) ; } } return defaultCharset; } |
1
2
3
4
5
6
7
|
public class Demo { public static void main ( String[] args ) { System.out.println ( "file.encoding=" + System.getProperty ( "file.encoding" ) ) ; byte[] bytes = "黑马程序员" .getBytes ( ) ; System.out.println ( Arrays.toString ( bytes ) ) ; } } |
1
2
|
file .encoding = UTF -8 [ -23 , -69 , -111 , -23 , -87 , -84 , -25 , -88 , -117 , -27 , -70 , -113 , -27 , -111 , -104 ] |
01
02
03
04
05
06
07
08
09
10
11
12
13
|
@WebServlet ( "/user/findAllServlet" ) public class ServletDemo 1 extends HttpServlet { protected void doPost ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { System.out.println ( "findAllServlet..." ) ; System.out.println ( "file.encoding=" + System.getProperty ( "file.encoding" ) ) ; byte[] bytes = "黑马程序员" .getBytes ( ) ; System.out.println ( Arrays.toString ( bytes ) ) ; } protected void doGet ( HttpServletRequest request , HttpServletResponse response ) throws ServletException , IOException { this.doPost ( request , response ) ; } } |
1
2
|
file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
发现事与愿违.file.encoding依然是UTF-8的.也就是说file.encoding跟文件的编码没有任何关系.相同的代码.我们用命令行窗口去编译和运行.发现file.encoding竟然变成了GBK
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
我们讲Demo.java文件改成UTF-8编码格式的用 javac -encoding utf-8 Demo.java进行编译.然后运行发现file.encoding竟然没有变:
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
这说明file.encoding跟编译环境没有关系.只跟运行环境有关.我们尝试运行的时候指定file.encoding编码:
1
|
java - Dfile.encoding = utf -8 Demo |
运行结果就是:
1
2
|
file .encoding = utf -8 [ -23 , -69 , -111 , -23 , -87 , -84 , -25 , -88 , -117 , -27 , -70 , -113 , -27 , -111 , -104 ] |
运行时将file.encoding指定成gbk
1
|
java - Dfile.encoding = gbk Demo |
运行结果就是:
1
2
3
|
C : \Users\yanjingpan\Desktop > java Demo file .encoding = GBK [ -70 , -38 , -62 , -19 , -77 , -52 , -48 , -14 , -44 , -79 ] |
到此我们就知道了.Tomcat在启动的时候将file.encoding指定成了gbk.所以我们在Servlet中获取字节码数组的时候,默认用的就是gbk.
Tomcat是根据当前操作系统来设置file.encoding的值.我电脑是windows简体中问的.所以默认就是GBK的.
我们可以在catalina.bat中将file.encoding设置成utf-8.
1
|
set "JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG% -Dfile.encoding=UTF-8" |
重启Tomcat.再访问我们的Servlet.file.encoding就改成UTF-8的了.
至此,困扰我们的getBytes()默认编码格式问题就解决了.
更多免费技术资料可关注:annalin1203