运维开发网

java.lang.IllegalStateException:已经为此响应调用了getOutputStream()

运维开发网 https://www.qedev.com 2020-04-16 21:12 出处:网络 作者:运维开发网整理
参见英文答案 > getOutputStream() has already been called for this response                                    12个 当用户单击某个链接时,使用Web服务将文档从远程ECM系统传输到用户计算机. 所以我创建了servlet并从Query字符串中获取了URL中的参数. 根据参数属性,调用了几个与Web服
参见英文答案 > getOutputStream() has already been called for this response                                    12个

当用户单击某个链接时,使用Web服务将文档从远程ECM系统传输到用户计算机.

所以我创建了servlet并从Query字符串中获取了URL中的参数.

根据参数属性,调用了几个与Web服务相关的方法来获取文件详细信息和文件内容.现在调用servlet和用户系统之间的文件传输.

最大的担忧是,由于以下例外,代码片段工作正常.用户可以将文档保存在所需位置.我试图弄清楚为什么我会收到这个错误.

错误:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:611)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:198)
t  DownloadServlet.doGet(DownloadServlet.java:99)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:723)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)

代码## DownloadServlet.java

getDocument(HttpServletRequest request,HttpServletResponse response)
   {
 \\used Custom web services methods to get filename with extensions from external ECM system 
   File resultFile = content.getAsFile();
   response.setContentType("application/octet-stream");  
   ServletOutputStream outStream = response.getOutputStream();          
   try {
       response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
       byte[] byteBuffer = new byte[BUFSIZE];
       DataInputStream in = new DataInputStream(new FileInputStream(file));
          while ((in != null) && ((length = in.read(byteBuffer)) != -1)){
            outStream.write(byteBuffer,0,length);
            }
            in.close();
            outStream.flush();                              
      } catch (Exception e) {
          e.printStackTrace();
      }finally{
          outStream.close();
      }

 }
使用Struts 2框架工作时,使用Web应用程序时遇到了同样的问题.

我搜索了一会儿,我找到的解决方案对我有用.

我找到了某个地方:

Basic rule of HTTP: one request, one response.

You can only send back one thing to a request.

Either an HTML page, or a PDF document, or an image etc.

Java complains if you have already obtained a writer/outputstream as you should only be getting ONE of these.

在我的例子中,下载文件的动作类在文件下载后向请求返回一个字符串“sucess”,这导致了问题.我将方法的返回类型更改为void并解决了问题.

扫码领视频副本.gif

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号