我在 Spring 3 框架 webapp 中实现了一个下载控制器,它应该被调用以在调用时启动一个打开/保存对话框。
它确实启动了打开/保存对话框并且保存工作正常。但是,当我单击“打开”时,它会在我的文件名后附加一个“.htm”并打开它全部错误。为什么?
控制器:
@RequestMapping("download/downloadFile")
@ResponseBody
public byte[] downloadFile(@RequestParam String fileID, HttpServletResponse response) {
response.setContentType("application/octet-stream");
File file = getFileByID(fileID);
byte[] bytes = FileCopyUtils.copyToByteArray(file);
response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
response.setContentLength(bytes.length);
return bytes;
}
当我看到响应标头时,我看到内容类型是 text/html,就像它忽略了我将其设置为 application/octet-stream!
Cache-Control no-cache, no-store
Connection close
Content-Disposition attachment; filename="test.txt"
Content-Length 22071
Content-Type text/html
Date Tue, 05 Feb 2013 21:12:20 GMT
Expires Thu, 01 Jan 1970 00:00:00 GMT
Pragma no-cache
Server Apache/2.2.10 (Linux/SUSE)
X-Powered-By Servlet/2.5 JSP/2.1
X-UA-Compatible IE=8, chrome=1
可能是因为我在 web.xml 中设置 Spring MVC 的方式?
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
一切都映射到以“.html”结尾的 URL 并且浏览器正在查看它?