我使用类似于下面的代码来返回一个zip文件作为SpringMVC请求的附件。整个过程非常好,当我向localhost / app / getZip发出请求时,我能够下载一个名为hello.zip的文件。
我的问题是,如何提示用户输入文件名。目前在FireFox25.0上,它自动将名称设置为“hello.zip”,而无需在打开或保存选项上更改文件名。
@RequestMapping("getZip")
public void getZip(HttpServletResponse response)
{
OutputStream ouputStream;
try {
String content = "hello World";
String archive_name = "hello.zip";
ouputStream = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(ouputStream);
out.putNextEntry(new ZipEntry(“filename”));
out.write(content);
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename="+ archive_name);
out.finish();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
TL; DR:使用HttpServletResponse我希望用户提供一个文件名,而不是在Header中传递一个。
答案 0 :(得分:1)
使用RequestMethod.GET方法
网址:http://localhost/app/getZip?filename=hello.zip
@RequestMapping(value = "getZip/{filename}", method = RequestMethod.GET)
public void getZip(HttpServletResponse response, @PathVariable String filename)
{
OutputStream ouputStream;
try {
String content = "hello World";
String archive_name = "hello.zip";
ouputStream = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(ouputStream);
out.putNextEntry(new ZipEntry("filename"));
out.write(content);
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename="+ archive_name);
out.finish();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}