Java乱码处理

(一)

1.web服务搜索乱码时,可用过滤器设置request和response的编码

      req.setCharacterEncoding("UTF-8");
       resp.setContentType("text/html;charSet=UTF-8");

2.也可以在tomcat的server.xml中配置:URIEncoding="UTF-8"  useBodyEncodingForURI="true"

URIEncoding和useBodyEncodingForURI区别是,

URIEncoding是对所有GET方式的请求的数据进行统一的重新编码,

而useBodyEncodingForURI则是根据响应该请求的页面的request.setCharacterEncoding参数对数据进行的重新编码,不同的页面可以有不同的重新编码的编码

(二)

设置Java文件的编码

System.setProperty("file.encoding","utf-8");

扫描二维码关注公众号,回复: 763342 查看本文章

System.setProperty("file.encoding","utf-8");String s = new String("中文".getBytes("UTF-8"),"UTF-8");

(三)

设置 from 表单的编码

1.v_idname = java.net.URLDecoder.decode(member.getName(), "UTF-8");

2.v_idname = URLEncoder.encode(member.getName(), "GBK");

3.// 设置请求属性
            // 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
            httpConn.setRequestProperty("Accept-Charset", "GBK");
            httpConn.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");
            httpConn.setRequestProperty("contentType", "GBK");
            httpConn.setRequestProperty("Content-Length",
                    String.valueOf(reqValues.length()));

4.<form id=\"submit\"  action=\""
                + v_action_url + "\" accept-charset=\"GBK\" method=\"post\">"

 (四)打成jar包后乱码:

在系统环境变量中添加

 JAVA_TOOL_OPTIONS, 变量值为:-Dfile.encoding=UTF-8,

猜你喜欢

转载自baihuashu225.iteye.com/blog/2244380