简体   繁体   English

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

[英]“Invalid byte 1 of 1-byte UTF-8 sequence” occurs when posting xml in .jar but not in eclpise

I have a problem where I am struggling, and I read many thread about "Invalid byte 1 of 1-byte UTF-8 sequence" , such as XML Invalid byte 1 of 1-byte UTF-8 sequence , MalformedByteSequenceException Invalid byte 1 of 1-byte UTF-8 sequence . 我在挣扎中遇到问题,我读了许多有关“ 1字节UTF-8序列的无效字节1”的线程,例如1字节UTF-8序列的XML无效字节1MalformedByteSequenceException无效的1的字节1个字节的UTF-8序列 But it does not solve my problem. 但这不能解决我的问题。

I have a web application(living in a paas cloud), and it is working fine to handle the request from the mobile clients(not developed by myself). 我有一个Web应用程序(生活在Paas云中),并且可以很好地处理来自移动客户端(不是我自己开发的)的请求。

In order to test the server application, I write a client application(say test-client)based on Swing to post the xml data through HTTP to my server. 为了测试服务器应用程序,我编写了一个基于Swing的客户端应用程序(例如test-client),以通过HTTP将xml数据发布到我的服务器。

The strange problem is that when I run this test-client in eclipse , it works fine to submit the post and get the message from my server back. 奇怪的问题是,当我在eclipse中运行此test-client时,提交帖子并从服务器获取消息可以很好地工作。

But when I export it into Runnable jar, the exception is found in my server logs that " org.dom4j.DocumentException: Invalid byte 1 of 1-byte UTF-8 sequence. Nested exception: Invalid byte 1 of 1-byte UTF-8 sequence. " when I post some xml data contains Chinese Character. 但是,当我将其导出到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数据时。

I believe this is relative to the difference encoding between my computer and eclipse. 我相信这与我的计算机和Eclipse之间的编码差异有关。

Please note that 请注意

1.I do not have xml to read,instead I construct the xml data from an object 1.我没有要读取的xml,而是从一个对象构造xml数据

2.my general/preference/workspace is encoded as UTF-8. 2.my general / preference / workspace编码为UTF-8。 And I have request.setCharacterEncoding("UTF-8"); 而且我有request.setCharacterEncoding("UTF-8"); in my doPost; 在我的doPost中;

3.I hope to modify my test-client code to let it works fine since the server is now working fine in production with the mobile user. 3.我希望修改我的测试客户端代码以使其正常工作,因为服务器现在可以与移动用户正常工作。

Below is how I post the xml data 以下是我发布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();

And the xmlstr comes from below xmlstr来自下面

(RequestTextMessage is a very easy class which only has getter, and one field of this Class will accept a input String, which may be Chinese): (RequestTextMessage是一个非常简单的类,只有getter,并且该类的一个字段将接受输入String,该字符串可能是中文):

 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);  
                    }  
                }  
            };  
        }  
    });  

For your information the exception from the server is below(I am sorry the exception is reverse): 供您参考,下面是服务器的异常(很抱歉,异常是相反的):

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. 

You need to choose the encoding used by your PrintWriter . 您需要选择PrintWriter使用的编码。 Outside of Eclipse, your platform is presumably defaulting to something other than UTF-8. 在Eclipse之外,您的平台可能默认使用UTF-8以外的其他名称。

Try this code: 试试这个代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM