当前位置: 首页> 房产> 建筑 > Spring下载文件

Spring下载文件

时间:2025/7/28 21:34:27来源:https://blog.csdn.net/qq_41848006/article/details/142213068 浏览次数:1次

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
关键字:Spring下载文件

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: