微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

关于weblogic下Did not meet stated content length of OutputStream异常

这段时间做上海公共地理信息服务平台的时候,卡在了webservice在C#中的调用上,因为我们的服务发布要进行控制,所以就做了个代理,在代理里分析了当前服务的权限后再经过后台请求转发原服务地址,代码如下:

public Document loadByUrlAndDoc(HttpServletRequest req,String url,Document doc) {

  Document resultDoc = null;
  SAXReader saxReader = new SAXReader();
  HttpURLConnection connection = null;
  InputStream cin = null;
  OutputStream out = null;
  XMLWriter writer = null;
  try {

   URL httpURL = new URL(url);
   connection = (HttpURLConnection) httpURL.openConnection();
   connection.setDoInput(true);
   connection.setDoOutput(true);
   
   if(doc != null){
    connection.setRequestMethod("POST");
    connection.setRequestProperty("SOAPAction",
    "http://www.example.org/TopoAnalyse/DownStream");
   }
   else{
    connection.setRequestMethod("GET");
   }
   
   connection.setRequestProperty("","");
   connection.setConnectTimeout(30000);
   connection.setReadTimeout(30000);
   

//此处将表头中的所有属性都加载到转发请求中了
   for(Enumeration enum1 = req.getHeaderNames(); enum1.hasMoreElements(); ){
    String header = (String)enum1.nextElement();
    String value = req.getHeader(header);
    connection.setRequestProperty(header,value);
    log.info("header:"+header+" value:"+value);
   }
   
   if(doc != null){
    
    out = connection.getoutputStream();

    OutputFormat format = OutputFormat.createCompactFormat();
    format.setEncoding("UTF-8");

    writer = new XMLWriter(out,format);
    writer.write(doc);
    writer.flush();
    
   }
   
   cin = connection.getInputStream();

   BufferedInputStream bci = new BufferedInputStream(cin);
   resultDoc = saxReader.read(bci);

  } catch (Exception e) {
   log.info(e);
  } finally {
   if (out != null) {
    try {
     out.close();
    } catch (IOException e) {
     log.info(e);
    }
   }
   if (writer != null) {
    try {
     writer.close();
    } catch (IOException e) {
     log.info(e);
    }
   }

   if (cin != null) {
    try {
     cin.close();
    } catch (IOException e) {
     log.info(e);
    }
   }
   
   if(connection != null) {
    connection.disconnect();
   }
  }
  return resultDoc;
 }

 

之后再WebLogic中报以下错误(在tomcat中没问题)

Did not meet stated content length of OutputStream:  you wrote 0 bytes and I was expecting  you to write exactly 187 bytes!!!
分析了很久最后发现是在装载表头的时候把ContentLength也设置进去了

最后将contentlength从表头中去除就OK乐

去掉表头中contentlength的方法如下:

for(Enumeration enum1 = req.getHeaderNames(); enum1.hasMoreElements(); ){
    String header = (String)enum1.nextElement();
    String value = req.getHeader(header);

   if(header !="content-length")
    connection.setRequestProperty(header,value);
    log.info("header:"+header+" value:"+value);
   }

 

还有一种方法,如下:

请检查web应用中的servlet代码查看是否设置了长度。也可以做下面的修改解决这个问题:

response.setContentLength(0);

……

……

response.flushBuffer();

return;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐