2014-02-12 96 views
2

我想要顯示特定文件夾中的圖像。當我從服務器發送請求,在客戶端上我得到以下異常「**的getOutputStream()已經被調用,這種響應」IllegalStateException:getOutputStream()已經被調用這個響應檢索圖像

這是HTML代碼

<td><img id="imagePreview" src="../catalogue/displayCatalogueImage.action?catalogueTitleNumber=${imageDisplay}" alt="Item Image" width="96" height="80"/></td> 

聽到「catalogueTitleNumber」是獨一無二的圖像的名稱

這是我的控制器

@RequestMapping("/catalogue/displayCatalogueImage.action") 
    public String getDisplayImage(String catalogueTitleNumber, HttpServletRequest request) { 

     try { 

      System.out.println("Image Display Title number = > "+catalogueTitleNumber); 

      if(catalogueTitleNumber!= null && !"".equals(catalogueTitleNumber)){ 

       File image = new File("images/"+catalogueTitleNumber); 

       BufferedImage originalImage=ImageIO.read(image); 

       ByteArrayOutputStream baos=new ByteArrayOutputStream(); 

       ImageIO.write(originalImage, "jpg", baos); 

       byte[] imageInByte = baos.toByteArray(); 

       request.setAttribute("imageInByte", imageInByte); 

       baos.close(); 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return "catalogue/displayCatalogueImage"; 
    } 

這是「displayCatalogueImage.JSP」頁面,幫助寫的圖像要求img標籤

<%@page import="java.io.OutputStream"%> 
<%@page import="java.sql.Blob"%> 
<% 

    try{ 

     byte[] image = (byte[]) request.getAttribute("imageInByte"); 

     response.setContentType("image/png"); 
     OutputStream os = response.getOutputStream(); 
     os.write(image); 

     os.flush(); 
     os.close(); 
     out.clear(); 

     out=pageContext.pushBody(); 

    } catch(Exception ex) { 

     out.println("Unable To Display image"); 
     out.println("Image Display Error = " + ex.getMessage()); 

    } 

%> 

在開始(僅一次)它顯示圖像的話我是不斷收到此異常

後聽到你可以說我打電話'的通知.write'兩次在控制器中,另一個在jsp頁面中(在jsp中.write幫助寫入字節碼,並在控制器中幫助獲取圖像的字節碼)我試圖避免在控制器中寫入'.write'它沒有給出任何錯誤,但沒有用... ...!

private byte[] image_byte_data(BufferedImage image) 
{ 
    WritableRaster raster = image.getRaster(); 
    DataBufferByte buffer = (DataBufferByte)raster.getDataBuffer(); 
    return buffer.getData(); 
} 

我被控制器調用上述方法試圖和初始化爲byte []連字節變量得到的圖像,但jsp頁面代碼的字節碼沒有寫......?

任何幫助將不勝感激,謝謝。


堆棧跟蹤

java.lang.IllegalStateException: getOutputStream() has already been called for this response 
    at org.apache.catalina.connector.Response.getWriter(Response.java:639) 
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214) 
    at com.opensymphony.module.sitemesh.filter.PageResponseWrapper$5.activateDestination(PageResponseWrapper.java:95) 
    at com.opensymphony.module.sitemesh.filter.RoutablePrintWriter.getDestination(RoutablePrintWriter.java:38) 
    at com.opensymphony.module.sitemesh.filter.RoutablePrintWriter.write(RoutablePrintWriter.java:144) 
    at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper$SaveContextPrintWriter.write(SaveContextOnUpdateOrErrorResponseWrapper.java:214) 
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:119) 
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:190) 
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:126) 
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:80) 
    at org.apache.jsp.WEB_002dINF.pages.catalogue.displayCatalogueImage_jsp._jspService(displayCatalogueImage_jsp.java:97) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:108) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:64) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:65) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:166) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:59) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) 
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487) 
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412) 
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339) 
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238) 
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262) 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1180) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:950) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118) 
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:146) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.www.BasicAuthenticationFilter.doFilter(BasicAuthenticationFilter.java:150) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) 
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) 
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) 
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) 
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) 
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:59) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) 
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487) 
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412) 
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339) 
    at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195) 
    at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159) 
    at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141) 
    at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90) 
    at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.easylib.elibrary.webapp.filter.LocaleFilter.doFilterInternal(LocaleFilter.java:67) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129) 
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:679) 
+0

你可以添加stacktrace嗎? – Fildor

+0

請認真檢查一下我添加了堆棧跟蹤... –

回答

1

如果您只是試圖發回圖像數據以響應客戶端圖像請求,請勿使用JSP。它沒有任何意義,因爲你沒有返回任何標記。只需將圖像數據直接寫入控制器的輸出流即可。這裏有一個例子,重構一點:

@RequestMapping("/catalogue/displayCatalogueImage.action") 
public ResponseEntity getDisplayImage(String catalogueTitleNumber, HttpServletResponse response) throws Exception { 

    System.out.println("Image Display Title number = > "+catalogueTitleNumber); 

    if (StringUtils.isNotEmpty(catalogueTitleNumber)) { 

     response.setHeader("Content-Type", "image/png"); 
     FileInputStream fin = new FileInputStream("images/"+catalogueTitleNumber); 
     ServletOutputStream out = response.getOutputStream(); 

     IOUtils.copy(fin, out); 
     IOUtils.closeQuietly(fin); 

     response.flushBuffer(); 

     return new ResponseEntity(HttpStatus.OK); 
    } 

    return new ResponseEntity(HttpStatus.NOT_FOUND); 
} 
+0

感謝您的寶貴答案它工作正常... –

0

你是第一個與周圍的Writerout幾行後檢索OutputStream然後仍然混亂。 你不能同時兼任。 只需刪除out調用和事情應該沒事,

+0

我按照您的說法移除了,但仍然出現相同的錯誤... –

+0

兩個調用都被刪除了嗎?即clear()和pushBody()? – hendrik

+0

雅我刪除了兩個電話 –

相關問題