在对该主题进行一些在线研究后,我目前设法使用此代码进行工作:
@RequestMapping(value = "/report040Generated", method = RequestMethod.GET)
public String index(Model model, HttpServletResponse response, HttpServletRequest request) throws IOException {
String myString = "Hello";
response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment;filename=myFile.txt");
ServletOutputStream out = response.getOutputStream();
out.println(myString);
out.flush();
out.close();
return "index";
}
我的问题是,当我单击 JSP 按钮时,文件会被下载,但该方法不会重定向到“index”.jsp View ,并给我一个 IllegalStateExcepton:
严重:servlet jsp 的 Servlet.service() 抛出异常 java.lang.IllegalStateException:已为此响应调用 getOutputStream()
对于可能导致此问题的原因有什么建议吗?
最佳答案
返回文件时无法重定向到另一个页面,因为文件本身是 http 响应。很好的解释在这里:Spring - download file and redirect
关于java - 通过 Spring MVC Web 应用程序向客户端发送文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30924049/