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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25244315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 21:52:29  来源:igfitidea点击:

"Invalid byte 1 of 1-byte UTF-8 sequence" occurs when posting xml in .jar but not in eclpise

javaxmleclipseencodingutf-8

提问by Jaskey

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. But it does not solve my problem.

我在苦苦挣扎的地方遇到了一个问题,我阅读了许多关于“1 字节 UTF-8 序列的无效字节 1”的线程,例如 XML 无效字节 1 的 1 字节 UTF-8 序列MalformedByteSequenceException Invalid byte 1 of 1 -byte UTF-8 序列。但这并不能解决我的问题。

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 中运行这个测试客户端时,提交帖子并从我的服务器取回消息可以正常工作。

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. And I have request.setCharacterEncoding("UTF-8");in my doPost;

2.我的general/preference/workspace 被编码为UTF-8。我request.setCharacterEncoding("UTF-8");在我的 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,这个Class的一个字段会接受一个输入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. 

回答by Duncan Jones

You need to choose the encoding used by your PrintWriter. Outside of Eclipse, your platform is presumably defaulting to something other than UTF-8.

您需要选择您的PrintWriter. 在 Eclipse 之外,您的平台可能默认为 UTF-8 以外的其他格式。

Try this code:

试试这个代码:

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