Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
770 views
in Technique[技术] by (71.8m points)

java - Spring: getOutputStream() has already been called for this response

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...