java.lang.IllegalStateException:已经为此响应调用了 getOutputStream()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25909657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.IllegalStateException: getOutputStream() has already been called for this response
提问by rakesh choudhury
When user clicks a certain link ,Using web services transfer document from A Remote ECM system to user machine.
当用户单击某个链接时,使用 Web 服务将文档从远程 ECM 系统传输到用户机器。
So I have created servlet and from Query string and get parameters from URL .
所以我创建了 servlet 和 Query string 并从 URL 获取参数。
Depending on parameters attributes several web services related methods were invoked to get file details and file content. now invoke file transfer between servlet and user system.
根据参数属性,调用了几个与 Web 服务相关的方法来获取文件详细信息和文件内容。现在调用 servlet 和用户系统之间的文件传输。
Biggest concern is that with the below exceptions , code fragment works fine. user can able to save document in desired location. I was trying to figure out why I am getting this error.
最大的担忧是,除了以下例外,代码片段工作正常。用户可以将文档保存在所需的位置。我试图弄清楚为什么我会收到这个错误。
Errors:
错误:
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)
Code## DownloadServlet.java
代码## 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();
}
}
回答by Janny
You have issue with response.
So use this two line.
您有响应问题。
所以用这两条线。
response.getOutputStream().flush();
response.getOutputStream().close();
回答by Joe
I had same problem while working with a web application using Struts 2 frame work. I searched for a while, and the solution that I found worked for me. I found somewhere that:
我在使用 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.
HTTP 的基本规则:一个请求,一个响应。您只能向请求发送一件事。HTML 页面、PDF 文档或图像等。如果您已经获得了编写器/输出流,Java 会抱怨,因为您应该只获得其中之一。
In my case, the action class which downloads the file was returning back a String "sucess" to the request after the file download, and that caused the problem. I changed the return type of the method to void and issue resolved.
在我的情况下,下载文件的操作类在文件下载后向请求返回一个字符串“成功”,这导致了问题。我将方法的返回类型更改为 void 并解决了问题。