From 171040b674ae18dd6560f1501f7c0d47a1f4b8ba Mon Sep 17 00:00:00 2001 From: Charles7c Date: Wed, 13 Mar 2024 23:43:01 +0800 Subject: [PATCH] =?UTF-8?q?feat(web):=20FileUploadUtils=20=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E6=96=87=E4=BB=B6=E4=B8=8B=E8=BD=BD=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/web/util/FileUploadUtils.java | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/util/FileUploadUtils.java b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/util/FileUploadUtils.java index cb3eec79..b4529e25 100644 --- a/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/util/FileUploadUtils.java +++ b/continew-starter-web/src/main/java/top/charles7c/continew/starter/web/util/FileUploadUtils.java @@ -18,17 +18,23 @@ package top.charles7c.continew.starter.web.util; import cn.hutool.core.date.DatePattern; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.IoUtil; import cn.hutool.core.io.file.FileNameUtil; import cn.hutool.core.util.IdUtil; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; import org.springframework.web.multipart.MultipartFile; import java.io.File; +import java.io.FileInputStream; import java.time.LocalDateTime; /** - * 文件上传工具类 + * 文件工具类 * * @author Zheng Jie(ELADMIN) * @author Charles7c @@ -41,7 +47,7 @@ public class FileUploadUtils { } /** - * 上传文件 + * 上传 * * @param multipartFile 源文件对象 * @param filePath 文件路径 @@ -51,7 +57,6 @@ public class FileUploadUtils { public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) { String originalFilename = multipartFile.getOriginalFilename(); String extensionName = FileNameUtil.extName(originalFilename); - String fileName; if (isKeepOriginalFilename) { fileName = "%s-%s.%s".formatted(FileNameUtil.getPrefix(originalFilename), DateUtil.format(LocalDateTime @@ -59,7 +64,6 @@ public class FileUploadUtils { } else { fileName = "%s.%s".formatted(IdUtil.fastSimpleUUID(), extensionName); } - try { String pathname = filePath + fileName; File dest = new File(pathname).getCanonicalFile(); @@ -71,8 +75,35 @@ public class FileUploadUtils { multipartFile.transferTo(dest); return dest; } catch (Exception e) { - log.error("Upload file occurred an error: {}. fileName: {}.", e.getMessage(), fileName, e); + log.error(e.getMessage(), e); } return null; } + + /** + * 下载 + * + * @param request 请求对象 + * @param response 响应对象 + * @param file 文件 + * @param autoDelete 下载后自动删除 + */ + public static void download(HttpServletRequest request, + HttpServletResponse response, + File file, + boolean autoDelete) { + response.setCharacterEncoding(request.getCharacterEncoding()); + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName()); + try (FileInputStream fis = new FileInputStream(file)) { + IoUtil.copy(fis, response.getOutputStream()); + response.flushBuffer(); + } catch (Exception e) { + log.error(e.getMessage(), e); + } finally { + if (autoDelete) { + file.deleteOnExit(); + } + } + } }