春季:已经为此响应调用了getOutputStream(). [英] Spring: getOutputStream() has already been called for this response
问题描述
我知道还有很多其他文章处理相同的错误,但是所有这些文章都是关于JSP/GSP页面的,或者由于其他原因对我的情况不是很有帮助.我在Thymeleaf中使用Spring MVC.以下功能用于下载文件.
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.
@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)
[...]
据我所知,getOutputStream()
仅被调用一次.同样,整个功能由try-catch
块包围.所以我想知道,这个错误是从哪里来的?
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?
推荐答案
如果返回'test',则指示您的控制器在使用响应outputStream返回二进制文件后将您发送到某些视图....这是您应如何管理的想法:
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:
这篇关于春季:已经为此响应调用了getOutputStream().的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!