英文:
Spring Boot: trigger InputStream Download in Browser
问题
我正在尝试下载一个文件:
import org.apache.commons.io.IOUtils;
@GetMapping("/file")
public void file(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
InputStream inStream = fmService.downloadFile("https://" + path);
IOUtils.copy(inStream, response.getOutputStream());
response.flushBuffer();
}
但是当我访问 localhost:8080/file?path=/Documents... 时,下载不会被触发。
如果我在路径中使用一个txt文件,则其中的内容会在浏览器中显示出来,但是当我使用一个pdf文件时,什么都不会发生。响应代码是200,但没有下载。
我该如何使用spring-boot来下载这些文件?
下一步将是从Angular前端调用此spring-boot API端点并进行下载。
我想输出内容也必须不同,是吗?
英文:
I'm trying to download a file:
import org.apache.commons.io.IOUtils;
@GetMapping("/file")
public void file(@RequestParam("path") String path, HttpServletResponse response) throws IOException {
InputStream inStream = fmService.downloadFile("https://" + path);
IOUtils.copy(inStream, response.getOutputStream());
response.flushBuffer();
}
But when I visit localhost:8080/file?path=/Documents... a download gets not triggered.
If I use a txt file in the path, then the content of it is shown to me in the browser, but when I'm using a pdf file nothing really happens. Having 200 response code but no download.
How can I use spring-boot to download the files?
And the next step would be to call this spring-boot api endpoint and download from an Angular frontend.
I think the output must be different too then or?
答案1
得分: 2
你需要设置以下标头,以便浏览器知道从返回的响应中下载文件,而不是在浏览器上显示。请参阅链接:
https://github.com/gtiwari333/spring-boot-blog-app/blob/master/src/main/java/gt/app/modules/file/FileDownloadUtil.java
response.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
一个有效的 GetMapping 示例:
@GetMapping
void d(HttpServletResponse r) throws IOException {
r.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
r.getOutputStream().print("ABCD");
r.getOutputStream().flush();
}
// 或者返回输入流:
@GetMapping("/is")
void d(HttpServletResponse r) throws IOException {
r.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
InputStream is = new ByteArrayInputStream("Some test string".getBytes()); // 或任何输入流
org.apache.commons.io.IOUtils.copy(is, r.getOutputStream());
r.getOutputStream().flush();
}
从 Angular 应用程序下载文件:
这会变得复杂,需要分三个步骤完成:
- 第一次调用以获得文件的直接链接
- 在锚点标签中呈现链接
<a href=
- 用户点击链接以执行文件下载
你可以参考这个链接:https://blog.gtiwari333.com/2017/01/angularjs-download-file-from-server.html
英文:
You need to set the following header so that the browser knows to download a file from the returned response instead of displaying on browser. See
https://github.com/gtiwari333/spring-boot-blog-app/blob/master/src/main/java/gt/app/modules/file/FileDownloadUtil.java
response.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
A working GetMapping:
@GetMapping
void d(HttpServletResponse r) throws IOException {
r.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
r.getOutputStream().print("ABCD");
r.getOutputStream().flush();
}
//Or return input stream:
@GetMapping("/is")
void d(HttpServletResponse r) throws IOException {
r.setHeader("Content-Disposition", "attachment; filename=" + "a.txt");
InputStream is = new ByteArrayInputStream("Some test string".getBytes()); //or any input stream
org.apache.commons.io.IOUtils.copy(is, r.getOutputStream());
r.getOutputStream().flush();
}
Downloading file from angular app:
This gets tricky and needs to be done in three steps:
- a first call to get direct link to the file
- render the link in anchor tag
<a href=
- have user click the link to do the file download
You can refer to this: https://blog.gtiwari333.com/2017/01/angularjs-download-file-from-server.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论