Java 将BuffereImage转换为jsp成功,但几秒钟后它消失了

Java 将BuffereImage转换为jsp成功,但几秒钟后它消失了,java,jsp,Java,Jsp,在java中: //str is binary String from database //from Binary convert binary to byteArray byte[] bytes = fromBinary(str); try { String name = "userAvatar"; response.setContentType("image/jpeg"); response

在java中:

   //str is binary String from database
    //from Binary convert binary to byteArray           
    byte[] bytes = fromBinary(str);
    try {
        String name = "userAvatar";
        response.setContentType("image/jpeg");
        response.setContentLength(bytes.length);
        response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        input = new BufferedInputStream(new ByteArrayInputStream(bytes));
        output = new BufferedOutputStream(response.getOutputStream());
        byte[] buffer = new byte[8192];
        int length;
        while ((length = input.read(buffer)) > 0) {
           output.write(buffer, 0, length);
        }
   }catch(Exception e){
    }
在jsp中:

<img alt="" src="${basePath }action/download/img?action=..." >

当我点击此页面时,图像显示正确,但几秒钟后,图像消失,控制台告诉我内容错误\u长度不匹配


为什么?

您缺少结束语句:

  output.write(buffer, 0, length);
  output.close(); //missing
}

您缺少关闭语句:

  output.write(buffer, 0, length);
  output.close(); //missing
}

在((length=input.read(buffer))!=-1)时尝试
,同时确保关闭流
output.close()
@Arvind谢谢当我关闭流时它不再存在至少到目前为止,我应该了解更多关于itTry
的信息,同时((length=input.read(buffer))!=-1)
,还要确保关闭流
output.close()
@Arvind谢谢当我关闭这个流时它已经不存在了至少到目前为止,我应该了解更多