当前位置: 首页 > news >正文

Spring下载文件

1、controller

/*** 下载文件通过ID** @param auditInformationDTO 靓号稽核文件DTO* @param servletResponse 响应体*/
@GetMapping(value = "/downloadAuditFileByAuditFileId")
public void downloadAuditFileByAuditFileId(@ModelAttribute final GoodNumberAuditInformationDTO auditInformationDTO,final HttpServletResponse servletResponse) throws IOException {final GoodNumberAuditInformation auditInformation = goodNumberAuditManagerService.getAuditInformation(auditInformationDTO);if (Objects.isNull(auditInformation)){throw new DataNotFoundException("文件数据不存在!");}final String attachmentSize = auditInformation.getAttachmentSize();final String attachmentName = auditInformation.getAttachmentName();final String attachmentAddress = auditInformation.getAttachmentAddress();ResponseHeadUtils.setDownloadFileHead(attachmentName, NumberUtils.toInt(attachmentSize), servletResponse);try (final InputStream inputStream = fileOperationStrategy.getObjectInputStream(auditAttachmentBucketName, attachmentAddress)) {IOUtils.copy(inputStream, servletResponse.getOutputStream());} catch (final IOException ioException) {final String fileId = auditInformation.getAuditId();log.error("文件下载异常,出现IO异常,下载文件ID是:{}!", fileId, ioException);throw ioException;}
}

2、ResponseHeadUtils工具类

import com.mocha.order.constant.ContentTypeConstant;
import com.mocha.order.constant.RequestHeadConstant;
import com.mocha.order.exception.CustomException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.util.UriUtils;import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;/*** http响应头工具类* 用于设置不通的头部参数** @author yyy*/
@Slf4j
public class ResponseHeadUtils {private ResponseHeadUtils() {}/*** 最小文件大小*/private static final int MIN_FILE_SIZE = 0;/*** 设置响应头的ContentDisposition** @param fileName 文件名称* @param httpServletResponse http响应头* @throws IllegalArgumentException 如果文件名称为空*/public static void setContentDisposition(final String fileName, final HttpServletResponse httpServletResponse) {if (StringUtils.isBlank(fileName)) {log.error("设置响应头ContentDisposition异常:文件名称为空!");throw new IllegalArgumentException("文件名称为空!");}try {final String encodedFileName = UriUtils.encode(fileName, StandardCharsets.UTF_8.toString());httpServletResponse.setHeader(RequestHeadConstant.CONTENT_DISPOSITION,"attachment; filename=" + encodedFileName + ";filename*=UTF-8''" + encodedFileName);} catch (final UnsupportedEncodingException e) {log.error("设置响应头异常:不支持的编码异常!", e);throw new CustomException("不支持的编码异常!");}}/*** 设置下载文件响应头* <p>设置<code>reset()</code>是为了:</p>* <ul>*     <li>确保在设置新的响应头之前,清除任何可能存在的旧的状态或已设置的响应头,以避免因累积设置而产生错误</li>*     <li>避免在执行下载接口之前,拦截器、过滤器等对响应头有个性化的设置,可能导致下载的时候因为响应头设置错误下载异常</li>* </ul>** @param fileName 文件名称* @param fileSize 文件大小* @param httpServletResponse http响应头* @throws IllegalArgumentException 如果文件大小小于等于{@link #MIN_FILE_SIZE}*/public static void setDownloadFileHead(final String fileName, final int fileSize, final HttpServletResponse httpServletResponse) {if (fileSize <= MIN_FILE_SIZE) {log.error("设置响应头ContentLength异常:文件大小为空!");throw new IllegalArgumentException("文件大小为空!");}httpServletResponse.reset();setContentDisposition(fileName, httpServletResponse);httpServletResponse.setContentLength(fileSize);httpServletResponse.setContentType(ContentTypeConstant.APPLICATION_OCTET_STREAM);}
}

3、前端

/downloadFileById?id=' + id
http://www.lryc.cn/news/436945.html

相关文章:

  • 如何在数据库中备份表:操作指南与注意事项
  • 【数据结构】第八节:链式二叉树
  • Fair Graph RepresentationLearning via Diverse Mixture-of-Experts
  • 电机驱动开发之驱动板
  • STM32F1 HAL库笔记2_HAL 系统驱动程序
  • el-table实现当内容过多时,el-table显示滚动条,页面不显示滚动条
  • Java面试篇基础部分-Java中的异常以及异常处理
  • win11 MySQL的坑
  • stm32单片机个人学习笔记1(简单介绍)
  • python中@staticmethod、@classmethod用法
  • Harmony Next 文件命令操作(发送、读取、媒体文件查询)
  • Go语言中的链表与双向链表实现
  • 开始一个WPF项目时的记忆重载入
  • 用go语言实现树和哈希表算法
  • 基于SpringBoot+Vue+MySQL的校园健康驿站管理系统
  • 深入理解MATLAB中的事件处理机制
  • 线程--线程同步
  • 【QT】Qt窗口
  • 场外个股期权怎么给股票加杠杆?
  • 【Docker部署ELK】(7.15)
  • UE4_后期处理_后期处理材质及后期处理体积一
  • 【PyQt6 应用程序】基于QtDesigner做一个用户登录页面
  • Ollama—87.4k star 的开源大模型服务框架!!
  • MySQL表的操作与数据类型
  • mysql把某一个字段的值中的aa,替换成bb
  • 【系统架构设计师】原型模式详解
  • Spring @Async 深度解读:默认线程池执行器的配置与优化
  • 手把手教你用护核纪元地心护核者用服务器开服联机
  • Log4j 1.x如何升级到Log4j 2.x
  • CloudFlare问题与CDN问题