2015-08-13 61 views
1

我正在與春季項目工作,我有這個問題,我谷歌的錯誤消息,我確實找到解決方案甚至所有關於這個相同的問題SO帖子「getOutputStream()」已被調用此回覆

java.lang.IllegalStateException: "getOutputStream()" has already been called for this response 
    at org.apache.catalina.connector.Response.getWriter(Response.java:578) 
    at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:212) 
    at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:109) 
    at org.apache.jasper.runtime.JspWriterImpl.initOut(JspWriterImpl.java:115) 
    at org.apache.jasper.runtime.JspWriterImpl.flushBuffer(JspWriterImpl.java:108) 
    at org.apache.jasper.runtime.PageContextImpl.release(PageContextImpl.java:175) 
    at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext(JspFactoryImpl.java:120) 
    at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext(JspFactoryImpl.java:75) 
    at org.apache.jsp.WEB_002dINF.Pages.generate_jsp._jspService(generate_jsp.java:209) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438) 
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396) 
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:721) 
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:584) 
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:523) 
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:230) 
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250) 
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060) 
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798) 
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716) 
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:647) 
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:552) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) 
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518) 
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091) 
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521) 
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) 
    at java.lang.Thread.run(Unknown Source) 

這是我的控制器:

@RequestMapping(method = RequestMethod.GET, value = "/GenerateM") 
    public String genererGet(HttpServletRequest request, ModelMap model, HttpServletResponse response) throws Exception { 
    // Some code ... 

     OutputStream out; 
     try { 
      response.setContentType("rtf"); 
      String filename = name + ".out.rtf"; 
      response.setHeader("Content-Disposition", "attachment; filename = " + filename); 
      out = response.getOutputStream(); 
      FileInputStream in = new FileInputStream(new File(rtf.getRtfOutput())); 
      byte[] buffer = new byte[4096]; 
      int length; 
      while ((length = in.read(buffer)) > 0) { 
       out.write(buffer, 0, length); 
      } 
      in.close(); 
      out.flush(); 
      out.close(); 
     } catch (IOException a) { 
      a.printStackTrace(); 
     } 
     return "generate"; 
    } 

我看到很多所以像帖子:THISTHISTHIS但對我來說沒有解決辦法

有人可以幫忙嗎?

回答

5

您首先通過自己編寫響應,然後返回視圖的名稱,從而告訴Spring它應該使用generate.jsp生成HTML頁面。

使您的方法返回void而不是String。一個HTTP響應只能有一個主體:它是一個文件附件或一個HTML頁面,但不能同時存在。

+0

現在它給了我一個'org.apache.catalina.connector.ClientAbortException:java.io.IOException:一個已建立的連接被您的主機中的軟件中止'錯誤信息像[this](http:// stackoverflow .com/questions/31303241/java-io-ioexception-an-established-connection-was-aborted-by-the-software-in-you) – yb3prod

+2

不要關閉響應OutputStream。這是集裝箱的工作。 –

+0

感謝您的幫助 – yb3prod

相關問題