SpringBoot返回文件让前端下载的几种方式

embedded/2025/3/4 23:01:14/
01 背景

后端开发中,通常会有文件下载的需求,常用的解决方案有两种:

  1. 不通过后端应用,直接使用nginx直接转发文件地址下载(适用于一些公开的文件,因为这里不需要授权)
  2. 通过后端进行下载,同时进行一些业务处理

本篇主要以方法2进行介绍,方法2的原理步骤如下:

  1. 读取文件,得到文件的字节流
  2. 将字节流写入到响应输出流中
02 一次性读取到内存,通过响应输出流输出到前端
    @GetMapping("/file/download")public void fileDownload(HttpServletResponse response, @RequestParam("filePath") String filePath) {File file = new File(filePath);if (!file.exists()) {throw new BusinessException("当前下载的文件不存在,请检查路径是否正确");}// 将文件写入输入流try (InputStream is = new BufferedInputStream(Files.newInputStream(file.toPath()))) {// 一次性读取到内存中byte[] buffer = new byte[is.available()];int read = is.read(buffer);// 清空 responseresponse.reset();response.setCharacterEncoding("UTF-8");// Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存// attachment表示以附件方式下载   inline表示在线打开   "Content-Disposition: inline; filename=文件名.mp3"// filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));// 告知浏览器文件的大小response.addHeader("Content-Length", "" + file.length());OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());response.setContentType("application/octet-stream");outputStream.write(buffer);outputStream.flush();outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}

适用于小文件,如果文件过大,一次性读取到内存中可能会出现oom的问题

02 将文件流通过循环写入到响应输出流中(推荐)
    @GetMapping("/file/download")public void fileDownload(HttpServletResponse response, @RequestParam("filePath") String filePath) {File file = new File(filePath);if (!file.exists()) {throw new BusinessException("当前下载的文件不存在,请检查路径是否正确");}// 清空 responseresponse.reset();response.setCharacterEncoding("UTF-8");response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));response.setContentType("application/octet-stream");// 将文件读到输入流中try (InputStream is = new BufferedInputStream(Files.newInputStream(file.toPath()))) {OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());byte[] buffer = new byte[1024];int len;//从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1while((len = is.read(buffer)) > 0){outputStream.write(buffer, 0, len);}outputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
03 从网络上获取文件并返回给前端
    @GetMapping("/net/download")public void netDownload(HttpServletResponse response, @RequestParam("fileAddress") String fileAddress, @RequestParam("filename") String filename) {try {URL url = new URL(fileAddress);URLConnection conn = url.openConnection();InputStream inputStream = conn.getInputStream();response.reset();response.setContentType(conn.getContentType());response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));byte[] buffer = new byte[1024];int len;OutputStream outputStream = response.getOutputStream();while ((len = inputStream.read(buffer)) > 0) {outputStream.write(buffer, 0, len);}inputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
04 从网络上获取文本并下载到本地
    @GetMapping("/netDownloadLocal")public void downloadNet(@RequestParam("netAddress") String netAddress, @RequestParam("filepath") String filepath) {try {URL url = new URL(netAddress);URLConnection conn = url.openConnection();InputStream inputStream = conn.getInputStream();FileOutputStream fileOutputStream = new FileOutputStream(filepath);int byteread;byte[] buffer = new byte[1024];while ((byteread = inputStream.read(buffer)) != -1) {fileOutputStream.write(buffer, 0, byteread);}fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}
05 总结

一定要搞清楚InputStreamOutputStream的区别,如果搞不清楚的,可以和字符流进行映射,InputStream -> Reader,OutPutStream -> Writer,换成这样你就知道读取内容需要使用Reader,写入需要使用Writer了。

返回给前端的是输出流,不需要你显示的去返回(return response;),这样会报错


http://www.ppmy.cn/embedded/170026.html

相关文章

统计有序矩阵中的负数

题目链接 统计有序矩阵中的负数 题目描述 注意点 1 < m, n < 100-100 < grid[i][j] < 100矩阵中的元素无论是按行还是按列&#xff0c;都以非严格递减顺序排列 解答思路 第一种思路是遍历每一行&#xff0c;再对每行进行二分查找找到每一行第一个负数的位置&a…

探秘基带算法:从原理到5G时代的通信变革【四】Polar 编解码(一)

文章目录 2.3 Polar 编解码2.3.1 Polar 码简介与发展背景2.3.2 信道极化理论基础对称容量与巴氏参数对称容量 I ( W ) I(W) I(W)巴氏参数 Z ( W ) Z(W) Z(W)常见信道信道联合信道分裂信道极化 本博客为系列博客&#xff0c;主要讲解各基带算法的原理与应用&#xff0c;包括&…

以太坊基金会换帅,资本市场砸盘

Vitalik力挺Aya升任EF主席&#xff0c;理想主义冬日发芽&#xff1f; 作者&#xff1a;Wenser&#xff1b;编辑&#xff1a;秦晓峰 出品 | Odaily星球日报&#xff08;ID&#xff1a;o-daily&#xff09; 2 月 27 日&#xff0c;Bybit 15 亿资金被盗事件的最新调查结果将以太坊…

企业数据挖掘建模平台哪家好?

在企业数字化转型中&#xff0c;数据建模是实现数据驱动决策的重要步骤。选择一个强大的数据建模平台&#xff0c;能够帮助企业高效地整合和分析数据&#xff0c;提供深刻的业务洞察。泰迪Tipdm数据挖掘建模平台以其强大的功能和灵活性&#xff0c;受不少企业的青睐。 数据挖掘…

【PCIE737】基于全高PCIe x8总线的KU115 FPGA高性能硬件加速卡

产品概述 PCIE737是一款基于PCIE总线架构的KU115 FPGA的12路光纤通道处理平台&#xff0c;该板卡具有1个PCIe Gen3x8主机接口、3个QSFP 40G光纤接口&#xff0c;可以实现3路QSFP 40G光纤的数据实时采集、实时缓存与PCIE高速传输。 该板卡采用Xilinx的高性能Kintex UltraScal…

JAVA面经2

ConcurrentHashMap 并发程序出现问题的根本原因 线程池 线程池的执行原理&#xff08;核心参数&#xff09; 线程池的常见阻塞队列 ArrayBlockingQueue插入和删除数据&#xff0c;只采用了一个lock&#xff0c;而LinkedBlockingQueue则是在插入和删除分别采用了putLock和takeL…

C++20 标准化有符号整数:迈向更可预测的整数运算

文章目录 一、背景&#xff1a;为什么需要标准化&#xff1f;二、2 的补码&#xff1a;原理与优势&#xff08;一&#xff09;2 的补码原理&#xff08;二&#xff09;2 的补码的优势 三、C20 的变化&#xff1a;明确 2 的补码四、如何利用这一特性优化代码&#xff08;一&…

私有化部署DeepSeek并SpringBoot集成使用(附UI界面使用教程-支持语音、图片)

私有化部署DeepSeek并SpringBoot集成使用&#xff08;附UI界面使用教程-支持语音、图片&#xff09; windows部署ollama Ollama 是一个开源框架&#xff0c;专为在本地机器上便捷部署和运行大型语言模型&#xff08;LLM&#xff09;而设计 下载ollama 下载地址&#xff08;…