Spring: getOutputStream() has already been called

2019-02-20 14:52发布

问题:

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)
[...]

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?

回答1:

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:

Downloading a file from spring controllers



回答2:

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 void resolved the problem.

I hope this helps if anyone else experiences this problem.



回答3:

If you want to send String then you could change

return "test";

to

return null;