为JAXB和response设置编码,解决wechat4j中文乱码(2)

简介: 为JAXB和response设置编码,解决wechat4j中文乱码

3.tomcat的输出环境作怪


针对这一点,网上有人提供这样的解决思路。


set JAVA_OPTS=%JAVA_OPTS% %LOGGING_MANAGER% -Dfile.encoding=UTF-8

设置后重启tomcat,问题是能够解决,但副作用是整个tomcat在服务器上运行输出(tomcat的cmd窗口)一直是乱码,我认为这种方案不可取。


在运行的war中加入以下代码


System.getProperty("file.encoding");


你会惊奇的发现,tomcat的运行环境(window server 2008)竟然是GBK,不知道你是否不惊奇,我是吓到了,为什么不是UTF-8呢?如果是GBK的话,上面两个步骤中我加入再多的UTF-8页扯淡啊,不解。


三、解决问题


有了以上的经验,我们修改以下wechat4j的代码,主要是第二点。

public String toXML(Object obj) {
    String result = null;
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller m = context.createMarshaller();
        String encoding = Config.instance().getJaxb_encoding();
        logger.debug("toXML encoding " + encoding + "System file.encoding " + System.getProperty("file.encoding"));
        m.setProperty(Marshaller.JAXB_ENCODING, encoding);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XMLSerializer serializer = getXMLSerializer(os);
        m.marshal(obj, serializer.asContentHandler());
        result = os.toString(encoding);
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("response text:" + result);
    return result;
}
private XMLSerializer getXMLSerializer(OutputStream os) {
    OutputFormat of = new OutputFormat();
    formatCDataTag();
    of.setCDataElements(cdataNode);
    of.setPreserveSpace(true);
    of.setIndenting(true);
    of.setOmitXMLDeclaration(true);
    String encoding = Config.instance().getJaxb_encoding();
    of.setEncoding(encoding);
    XMLSerializer serializer = new XMLSerializer(of);
    serializer.setOutputByteStream(os);
    return serializer;
}


这两个方法中,对encoding我们加上可配置的编码方式,可手动设置GBK(我的服务器上配置了GBK)、GB2312、UTF-8。


如此,会发现wechat4j的后台输出就不再是中文乱码了,但返回给用户的信息更乱了。




怎么能这样呢,耍我这枚程序员啊,真想吐两句脏话。但别怕啊,既然wechat4j的logger日志不再中文乱码,那么只能说是第1个环节又出现问题了。


调整嘛


response.setHeader("content-type", "text/html;charset=UTF-8");// 浏览器编码

response.getOutputStream().write(result.getBytes("UTF-8"));


注意,这里不能是GBK,只能是UTF-8,我表示不清楚为什么,微信的产品经理给出来解释下。


重点,JAXB和response合伙解决wechat4j中文乱码的 方法再次声明如下:


WeChatController.java,就是你配给微信公众开发平台的URL处,response调整如下


response.setHeader("content-type", "text/html;charset=UTF-8");// 浏览器编码

response.getOutputStream().write(result.getBytes("UTF-8"));


wechat4j的JaxbParser.java,分别调整toXML(Object obj)和getXMLSerializer(OutputStream os)方法:

public String toXML(Object obj) {
    String result = null;
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller m = context.createMarshaller();
        String encoding = Config.instance().getJaxb_encoding();// GBK
        logger.debug("toXML encoding " + encoding + "System file.encoding " + System.getProperty("file.encoding"));
        m.setProperty(Marshaller.JAXB_ENCODING, encoding);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉报文头
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        XMLSerializer serializer = getXMLSerializer(os);
        m.marshal(obj, serializer.asContentHandler());
        result = os.toString(encoding);
    } catch (Exception e) {
        e.printStackTrace();
    }
    logger.info("response text:" + result);
    return result;
}
private XMLSerializer getXMLSerializer(OutputStream os) {
    OutputFormat of = new OutputFormat();
    formatCDataTag();
    of.setCDataElements(cdataNode);
    of.setPreserveSpace(true);
    of.setIndenting(true);
    of.setOmitXMLDeclaration(true);
    String encoding = Config.instance().getJaxb_encoding();//GBK
    of.setEncoding(encoding);
    XMLSerializer serializer = new XMLSerializer(of);
    serializer.setOutputByteStream(os);
    return serializer;
}


好了,万事大吉了。

相关文章
|
2月前
|
Java
解决application.properties 中文乱码问题
解决application.properties 中文乱码问题
36 0
|
5月前
|
C++
VS Code 中文乱码及编码格式问题全解
VS Code 中文乱码及编码格式问题全解
2063 0
|
5月前
|
XML 数据格式
restTemplat发post请求报错Content type ‘application/xml;charset=UTF-8‘ not supported“
restTemplat发post请求报错Content type ‘application/xml;charset=UTF-8‘ not supported“
208 1
|
测试技术
JavaWeb - Hutool Bug HttpResponse body 方法中文乱码
JavaWeb - Hutool Bug HttpResponse body 方法中文乱码
748 0
解决使用Properties读取中文乱码问题
解决使用Properties读取中文乱码问题
637 0
|
JSON 前端开发 Java
SpringMVC:请求乱码问题处理和json乱码解决方案
SpringMVC:请求乱码问题处理和json乱码解决方案
SpringMVC:请求乱码问题处理和json乱码解决方案
JavaWeb - Request 中 Header 数据中文乱码解决方案(仅一句话)
JavaWeb - Request 中 Header 数据中文乱码解决方案(仅一句话)
1134 0
|
编解码 程序员
为JAXB和response设置编码,解决wechat4j中文乱码(1)
为JAXB和response设置编码,解决wechat4j中文乱码
156 0
为JAXB和response设置编码,解决wechat4j中文乱码(1)
|
Java
Java 技术篇 - ServerSocket接收http的url请求中包含中文的处理方法,URLDecode与URLEncode,url解码与编码
Java 技术篇 - ServerSocket接收http的url请求中包含中文的处理方法,URLDecode与URLEncode,url解码与编码
257 0
Java 技术篇 - ServerSocket接收http的url请求中包含中文的处理方法,URLDecode与URLEncode,url解码与编码