Spring Boot:在浏览器中触发 InputStream 下载

huangapple go评论80阅读模式
英文:

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(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + &quot;a.txt&quot;);

A working GetMapping:

@GetMapping
void d(HttpServletResponse r) throws IOException {
  r.setHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + &quot;a.txt&quot;);
  r.getOutputStream().print(&quot;ABCD&quot;);
  r.getOutputStream().flush();
}


//Or return input stream:
 
@GetMapping(&quot;/is&quot;)
void d(HttpServletResponse r) throws IOException {
    r.setHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + &quot;a.txt&quot;);

    InputStream is = new ByteArrayInputStream(&quot;Some test string&quot;.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 &lt;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

huangapple
  • 本文由 发表于 2020年10月20日 21:52:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64446626.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定