简体   繁体   English

如何使用Java中的Spring MVC实现文件下载?

[英]How to implement file download with Spring MVC in Java?

There is a lot upload examples, but I couldn't find any for download. 有很多上传示例,但我找不到任何下载。 I'm using Spring 3.0 and I need to generate some text buffer and then let user download it as a file. 我正在使用Spring 3.0,我需要生成一些文本缓冲区,然后让用户将其作为文件下载。 (There is no static resource.) (没有静态资源。)

So far I've tried to use @ResponseBody and writing to response output stream. 到目前为止,我已经尝试使用@ResponseBody并写入响应输出流。 In both cases browser display content of the 'file' instead of showing Save dialog box. 在这两种情况下,浏览器显示“文件”的内容而不是显示“保存”对话框。 How do I make browser display a dialog box? 如何让浏览器显示对话框?

Thanks. 谢谢。

您必须在操作中设置Content-Disposition标头(而不仅仅是响应主体):

Content-Disposition: attachment; filename=your-file.pdf

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM