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

Categories

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

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

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...