在.jar中发布xml而在eclpise中发布时,会出现“1字节UTF-8序列的无效字节1”

时间:2014-08-11 13:22:16

标签: java xml eclipse encoding utf-8

我有一个问题,我正在努力,我读了很多关于" 1字节UTF-8序列的无效字节1" , 如 XML Invalid byte 1 of 1-byte UTF-8 sequenceMalformedByteSequenceException Invalid byte 1 of 1-byte UTF-8 sequence。但它并没有解决我的问题。

我有一个Web应用程序(生活在paas云中),它可以正常处理来自移动客户端的请求(不是我自己开发的)。

为了测试服务器应用程序,我编写了一个基于Swing的客户端应用程序(比如test-client),通过HTTP将xml数据发布到我的服务器上。

奇怪的问题是,当我在eclipse中运行这个测试客户端时,它可以正常提交帖子并从我的服务器上获取消息。

但是当我将它导出到Runnable jar时,在我的服务器日志中发现异常" org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence."当我发布一些包含中文字符的xml数据时。

我相信这与我的电脑和日食之间的差异编码有关。

请注意

1.我没有要读取的xml,而是从对象

构造xml数据

2.my general / preference / workspace被编码为UTF-8。我的doPost中有request.setCharacterEncoding("UTF-8");;

3.我希望修改我的测试客户端代码以使其正常工作,因为服务器现在可以与移动用户一起正常生产。

以下是我发布xml数据的方法

        URL url = new URL(address);
        URLConnection uc = url.openConnection();
        HttpURLConnection conn = (HttpURLConnection) uc;
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-type", "text/xml");
        System.out.println("before POST:\n"+xmlstr);
        PrintWriter pw = new PrintWriter(conn.getOutputStream());
        pw.write(xmlstr);
        pw.close();

xmlstr来自下面

(RequestTextMessage是一个非常简单的类,只有getter,这个Class的一个字段将接受一个输入字符串,可能是中文):

 xmlStr= XMLRequest.textMessageToXml(msg);

public static String textMessageToXml(RequestTextMessage textMsg){
    xstream.alias("xml", textMsg.getClass());
    return xstream.toXML(textMsg);
}
   private static XStream xstream = new XStream(new XppDriver() {

        @Override
        public HierarchicalStreamWriter createWriter(Writer out) {  
            return new PrettyPrintWriter(out) {  
                boolean cdata = true;  

                protected void writeText(QuickWriter writer, String text) {  
                    if (cdata) {  
                        writer.write("<![CDATA[");  
                        writer.write(text);  
                        writer.write("]]>");  
                    } else {  
                        writer.write(text);  
                    }  
                }  
            };  
        }  
    });  

为了您的信息,服务器的例外情况如下(对不起,例外情况正好相反):

at java.lang.Thread.run(Thread.java:724)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at wodinow.weixin.jaskey.co.CoreServlet.doPost(CoreServlet.java:161)
at wodinow.weixin.jaskey.service.CommandService.generateResponseXML(CommandService.java:76)
at wodinow.weixin.jaskey.util.MessageUtil.parseXml(MessageUtil.java:52)
at org.dom4j.io.SAXReader.read(SAXReader.java:335)
at org.dom4j.io.SAXReader.read(SAXReader.java:439)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:568)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1210)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:123)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:835)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:489)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:116)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:607)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2947)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanCDATASection(XMLDocumentFragmentScannerImpl.java:1614)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.scanData(XMLEntityScanner.java:1252)
at com.sun.org.apache.xerces.internal.impl.XMLEntityScanner.load(XMLEntityScanner.java:1753)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.read(UTF8Reader.java:557)
at com.sun.org.apache.xerces.internal.impl.io.UTF8Reader.invalidByte(UTF8Reader.java:687)
com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.
Nested exception:
at java.lang.Thread.run(Thread.java:724)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at wodinow.weixin.jaskey.co.CoreServlet.doPost(CoreServlet.java:161)
at wodinow.weixin.jaskey.service.CommandService.generateResponseXML(CommandService.java:76)
at wodinow.weixin.jaskey.util.MessageUtil.parseXml(MessageUtil.java:52)
at org.dom4j.io.SAXReader.read(SAXReader.java:335)
at org.dom4j.io.SAXReader.read(SAXReader.java:458)
org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception:     Invalid byte 1 of 1-byte UTF-8 sequence. 

1 个答案:

答案 0 :(得分:3)

您需要选择PrintWriter使用的编码。在Eclipse之外,您的平台可能默认为UTF-8以外的其他东西。

试试这段代码:

PrintWriter pw = new PrintWriter(new OutputStreamWriter(
    conn.getOutputStream(), "UTF-8"));