java.lang.IllegalStateException: Can't overwrite cause webservice访问异常 解决方案

java.lang.IllegalStateException: Can't overwrite cause
	at java.lang.Throwable.initCause(Throwable.java:320)
	at com.ctc.wstx.compat.Jdk14Impl.setInitCause(Jdk14Impl.java:70)
	at com.ctc.wstx.exc.WstxException.(WstxException.java:46)
	at com.ctc.wstx.exc.WstxIOException.(WstxIOException.java:16)
	at com.ctc.wstx.sr.StreamScanner.throwFromIOE(StreamScanner.java:683)
	at com.ctc.wstx.sr.BasicStreamReader.next(BasicStreamReader.java:1086)
	at org.codehaus.xfire.soap.handler.ReadHeadersHandler.invoke(ReadHeadersHandler.java:44)
	at org.codehaus.xfire.handler.HandlerPipeline.invoke(HandlerPipeline.java:131)
	at org.codehaus.xfire.transport.DefaultEndpoint.onReceive(DefaultEndpoint.java:64)
	at org.codehaus.xfire.transport.AbstractChannel.receive(AbstractChannel.java:38)
	at org.codehaus.xfire.transport.http.XFireServletController.invoke(XFireServletController.java:304)
	at org.codehaus.xfire.transport.http.XFireServletController.doService(XFireServletController.java:129)
	at org.codehaus.xfire.spring.remoting.XFireServletControllerAdapter.handleRequest(XFireServletControllerAdapter.java:67)
	at org.codehaus.xfire.spring.remoting.XFireExporter.handleRequest(XFireExporter.java:48)
	at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:807)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:511)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2441)
	at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2430)
	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
	at java.lang.Thread.run(Thread.java:619)

今天遇到了这个比较麻烦的问题,主要是开始跑偏了。以为这个问题是由OutputStream和JspWriter冲突引起的,
具体分析详见:https://my.oschina.net/guhai2004/blog/187041,在这上面修改方法花了比较大的功夫。
但是还不好使,使用其他的方法调用的时候,发现有能够正常调用的,开始考虑是字符集的问题,在查看soap文件时
发现带有中文的会出现这个异常,分析是编码的问题。

编码转换方案,直接上代码吧

//中文字符处理
    public static String charSetConvert(String xmlRequest){
        String charSet = getEncoding(xmlRequest);
        try {
            byte[] b = xmlRequest.getBytes(charSet);
            xmlRequest = new String(b, "UTF-8");
        } catch (Exception e) {
            System.out.println("输入的内容不属于常见的编码格式,请再仔细核实"+e);
        }
        return xmlRequest;
        
    }
    //判断字符集类型
    public static String getEncoding(String str) {
        String encode = "GB2312";
		try {
            if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是GB2312
                String s = encode;
                return s; // 是的话,返回GB2312,以下代码同理
            }
        } catch (Exception e) {
        	 System.out.printf("getEncoding异常---GB2312", e);
        }
        encode = "ISO-8859-1";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是ISO-8859-1
                String s1 = encode;
                return s1;
            }
        } catch (Exception e) {
        	System.out.printf("getEncoding异常---ISO-8859-1", e);
        }
        encode = "UTF-8";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是UTF-8编码
                String s2 = encode;
                return s2;
            }
        } catch (Exception e) {
        	System.out.printf("getEncoding异常---UTF-8", e);
        }
        encode = "GBK";
        try {
            if (str.equals(new String(str.getBytes(encode), encode))) { // 判断是不是GBK
                String s3 = encode;
                return s3;
            }
        } catch (Exception e) {
        	System.out.printf("getEncoding异常---GBK", e);
        }
        return ""; // 到这一步,你就应该检查是不是其他编码啦
    }


你可能感兴趣的:(java.lang.IllegalStateException: Can't overwrite cause webservice访问异常 解决方案)