Java Spring:已经为此响应调用了 getOutputStream()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21039471/
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
Spring: getOutputStream() has already been called for this response
提问by vauge
I know that there are many other posts dealing with the very same error, but all of them are either about JSP / GSP pages or for any other reason not very helpful in my case. I'm using Spring MVC with Thymeleaf. The following function is for downloading a file.
我知道还有很多其他帖子处理同样的错误,但所有这些帖子要么是关于 JSP/GSP 页面的,要么是出于任何其他原因对我来说不是很有帮助。我正在使用带有 Thymeleaf 的 Spring MVC。以下函数用于下载文件。
@RequestMapping(value = "/test/download/*", method = RequestMethod.GET)
public String getFile(HttpServletResponse response)
{
ServletOutputStream stream = null;
try
{
stream = response.getOutputStream();
MultipartFile f = test.getFile();
InputStream is = f.getInputStream();
IOUtils.copy(is, stream);
response.flushBuffer();
stream.flush();
stream.close();
} catch(Exception ex)
{
}
return "test";
}
It does actually work, so it's not too much of a problem, but in the console, I'm always getting the following error:
它确实有效,所以问题不大,但在控制台中,我总是收到以下错误:
2014-01-10T09:28:09.053+0100 SEVERE Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: getOutputStream() has already been called for this response] with root cause
java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:638)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:214)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:105)
at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.getWriter(SaveContextOnUpdateOrErrorResponseWrapper.java:125)
at org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
at org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:190)
[...]
As far as I see, getOutputStream()
is only called once. Also, the whole function is surrounded with a try-catch
-block. So I'd like to know, where does this error come from?
据我所知,getOutputStream()
只被调用一次。此外,整个函数被一个try-catch
-block包围。所以我想知道,这个错误是从哪里来的?
采纳答案by Jorge_B
If you return 'test', you are instructing your controller to send you to some view... after using the response outputStream to return a binary file. Here is an idea of how you should manage this:
如果您返回“test”,则是在使用响应 outputStream 返回二进制文件后,指示您的控制器将您发送到某个视图。以下是您应该如何管理此问题的想法:
回答by Dan Torrey
I just experienced this problem.
我刚刚遇到了这个问题。
The problem was caused by my controller method attempting return type of String (view name)when it exits. When the method would exit, a second response stream would be initiated.
问题是由我的控制器方法在退出时尝试返回字符串类型(视图名称)引起的。当该方法退出时,将启动第二个响应流。
Changing the controller method return type to voidresolved the problem.
将控制器方法返回类型更改为void解决了问题。
I hope this helps if anyone else experiences this problem.
如果其他人遇到此问题,我希望这会有所帮助。
回答by mo sean
If you want to send String
then you could change
如果你想发送,String
那么你可以改变
return "test";
to
到
return null;