gpt4 book ai didi

java - Spring:已经为此响应调用了 getOutputStream()

转载 作者:行者123 更新时间:2023-11-29 09:54:06 25 4
gpt4 key购买 nike

我知道还有许多其他帖子处理同样的错误,但所有这些帖子要么是关于 JSP/GSP 页面的,要么是出于任何其他原因,对我的情况不是很有帮助。我将 Spring MVC 与 Thymeleaf 一起使用。以下函数用于下载文件。

@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";

}

它确实有效,所以问题不大,但在控制台中,我总是收到以下错误:

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 block 中。所以我想知道,这个错误是从哪里来的?

最佳答案

如果您返回“test”,则表示您在使用响应 outputStream 返回二进制文件后指示您的 Controller 将您发送到某个 View ...。以下是您应该如何管理它的想法:

Downloading a file from spring controllers

关于java - Spring:已经为此响应调用了 getOutputStream(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com