解决在weblogic上报java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '146592' bytes instead of stated: '462285' by

今天在查看weblogic服务器日志时后台报出了大量的错误,如下:

java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '146592' bytes instead of stated: '462285' bytes.
 at weblogic.servlet.internal.ServletOutputStreamImpl.ensureContentLength(ServletOutputStreamImpl.java:470)
 at weblogic.servlet.internal.ServletResponseImpl.ensureContentLength(ServletResponseImpl.java:1176)
 at weblogic.servlet.internal.ServletResponseImpl.send(ServletResponseImpl.java:1188)
 at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestImpl.java:2590)
 at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:197)
 at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:170)

经过多次测试后找到解决办法,主要是因为在图片下载时,

   bis = new BufferedInputStream(rs.getBinaryStream(img));

           response.setContentLength(rs.getInt(2));
           response.setBufferSize(1024);
           response.setContentType("application/x-msdownload");
           response.setHeader("Content-Disposition", "filename=" + InfoVariable.converu2a("img"));
           ServletOutputStream op = response.getOutputStream();

          byte[] b= new byte[1024];
          int size=0;
          while ((size = bis.read(b)) != -1)
     op.write(b, 0, size); 

导入到response中的字节数没有设定,可以通过增加如下设置解决此问题,

//其中rs.getInt(2)为在数据库中blob字段的大小,可以通过dbms_lob.getlength(img)函数取得

response.setContentLength(rs.getInt(2));    //注意cotentlength的大小为图片的大小

如果要是图片打不开,或者显示为乱码可以用response.resetBuffer();刷新一下缓存.

你可能感兴趣的:(技术文章)