问题描述:
1. Linux操作系统,weblogic服务器下部署应用,IE9浏览器从服务器取图片显示,相同src ,但图片不同,第一次读图片,能够显示。当后一张图片覆盖前一张图片后,再次读取图片,会显示不全或图片不显示。
2. 服务端报错
- Servlet failed with IOException
- java.io.IOException: failed to read '912' bytes from InputStream; clen: 21232 remaining: 14000 count: 7232
解决过程说明:
1. html标签如下,服务器中无图片时,显示images/pnLeft.jpg
<img id = 'signaturephoto' src = 'images/pnLeft.jpg'/><br/>
2. 假定,从服务器取图片路径为var imgurl = http://10.1.21.113:7001/VTA/businessPicture/2015/09/11/requestpicture.jpg
读取图片方式:$("#"+pictureTag).attr("src",imgurl); 会发现,第一次读取图片时,能够正常显示。但是当服务器图片改变,再次调用该方法读取图片时,会发现显示的还是之前的,这是因为读取图片的路径和图片名未发生变化,读取的图片是浏览器缓存中的。
读取方式改为:$("#signaturephoto").attr("src",imgurl+"?t="+Math.random());在路径后面加随机数,让浏览器检测到变化,从而绕过缓存读取服务器中的图片。
3. 更复杂的问题,通过上述做法,能够让浏览器获得改变的图片,但会显示图片不全或图片无法获取的问题。且服务端报错,如下:
- <BEA-101019> Servlet failed with IOException
- java.io.IOException: failed to read '912' bytes from InputStream; clen: 21232 remaining: 14000 count: 7232
- at weblogic.servlet.internal.ChunkOutput.writeStream(ChunkOutput.java:466)
- at weblogic.servlet.internal.ChunkOutputWrapper.writeStream(ChunkOutputWrapper.java:192)
- at weblogic.servlet.internal.ServletOutputStreamImpl.writeStream(ServletOutputStreamImpl.java:555)
- at weblogic.servlet.internal.ServletOutputStreamImpl.writeStream(ServletOutputStreamImpl.java:543)
- at weblogic.servlet.FileServlet.sendFile(FileServlet.java:410)
- at weblogic.servlet.FileServlet.doGetHeadPost(FileServlet.java:234)
- at weblogic.servlet.FileServlet.service(FileServlet.java:173)
- at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
- at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
- at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
- at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
- at weblogic.servlet.internal.TailFilter.doFilter(TailFilter.java:26)
- at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
- at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
- at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
- at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
- at weblogic.servlet.internal.RequestEventsFilter.doFilter(RequestEventsFilter.java:27)
- at weblogic.servlet.internal.FilterChainImpl.doFilter(FilterChainImpl.java:56)
- at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3730)
- at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3696)
- at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
- at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
- at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2273)
- at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2179)
- at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1490)
- at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
- at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
4. 遇到上述问题,在百度上搜了一大,均是不靠谱的答案,最后在java的stackoverflow社区找到了解决方案,还是正规途径靠谱,没有谷歌可坑苦了国内的开发猿们,嘻嘻。
如下,在weblogic.xml中添加如下代码,即可解决上述问题,给出链接,可自行查看:http://stackoverflow.com/questions/12001339/file-transfer-failing-with-weblogic-java-io-ioexception-failed-to-read-2607
- <jsp-descriptor>
- <page-check-seconds>0</page-check-seconds>
- </jsp-descriptor>
- <container-descriptor>
- <resource-reload-check-secs>0</resource-reload-check-secs>
- </container-descriptor>
weblogic.xml的完整配置如下:
- <?xml version="1.0" encoding="UTF-8" ?>
- <weblogic-web-app xmlns="http://xmlns.oracle.com/weblogic/weblogic-web-app">
- <context-root>/VTA</context-root>
- <jsp-descriptor>
- <page-check-seconds>0</page-check-seconds>
- </jsp-descriptor>
- <container-descriptor>
- <prefer-web-inf-classes>true</prefer-web-inf-classes>
- <resource-reload-check-secs>0</resource-reload-check-secs>
- </container-descriptor>
- </weblogic-web-app>
至此,该问题解决,花了一下午时间才解决这个问题,所以记录,同时希望能帮助遇到相同问题的苦逼网友。
============================================================================================
5. 再补充点东西
如果服务端不完整,也可能会遇到类似问题,贴出部分代码,关键代码标为红色,供参考:
- public void getPictureByName(String name,HttpServletRequest request,HttpServletResponse response){
- try {
- BufferedImage bi = fileService.getPicture( name );
- int index = name.lastIndexOf(".");
- String formate = name.substring(index+1);
- OutputStream os = response.getOutputStream();
- ImageIO.write(bi, formate, os);
- os.close();
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- }
- }
若上述代码仍无法解决问题,可参考链接
http://www.iteye.com/problems/48877