mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-31 08:58:37 +08:00 
			
		
		
		
	feat(web): FileUploadUtils 新增文件下载方法
This commit is contained in:
		| @@ -18,17 +18,23 @@ package top.charles7c.continew.starter.web.util; | |||||||
|  |  | ||||||
| import cn.hutool.core.date.DatePattern; | import cn.hutool.core.date.DatePattern; | ||||||
| import cn.hutool.core.date.DateUtil; | import cn.hutool.core.date.DateUtil; | ||||||
|  | import cn.hutool.core.io.IoUtil; | ||||||
| import cn.hutool.core.io.file.FileNameUtil; | import cn.hutool.core.io.file.FileNameUtil; | ||||||
| import cn.hutool.core.util.IdUtil; | import cn.hutool.core.util.IdUtil; | ||||||
|  | import jakarta.servlet.http.HttpServletRequest; | ||||||
|  | import jakarta.servlet.http.HttpServletResponse; | ||||||
| import org.slf4j.Logger; | import org.slf4j.Logger; | ||||||
| import org.slf4j.LoggerFactory; | import org.slf4j.LoggerFactory; | ||||||
|  | import org.springframework.http.HttpHeaders; | ||||||
|  | import org.springframework.http.MediaType; | ||||||
| import org.springframework.web.multipart.MultipartFile; | import org.springframework.web.multipart.MultipartFile; | ||||||
|  |  | ||||||
| import java.io.File; | import java.io.File; | ||||||
|  | import java.io.FileInputStream; | ||||||
| import java.time.LocalDateTime; | import java.time.LocalDateTime; | ||||||
|  |  | ||||||
| /** | /** | ||||||
|  * 文件上传工具类 |  * 文件工具类 | ||||||
|  * |  * | ||||||
|  * @author Zheng Jie(<a href="https://gitee.com/elunez/eladmin">ELADMIN</a>) |  * @author Zheng Jie(<a href="https://gitee.com/elunez/eladmin">ELADMIN</a>) | ||||||
|  * @author Charles7c |  * @author Charles7c | ||||||
| @@ -41,7 +47,7 @@ public class FileUploadUtils { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * 上传文件 |      * 上传 | ||||||
|      * |      * | ||||||
|      * @param multipartFile          源文件对象 |      * @param multipartFile          源文件对象 | ||||||
|      * @param filePath               文件路径 |      * @param filePath               文件路径 | ||||||
| @@ -51,7 +57,6 @@ public class FileUploadUtils { | |||||||
|     public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) { |     public static File upload(MultipartFile multipartFile, String filePath, boolean isKeepOriginalFilename) { | ||||||
|         String originalFilename = multipartFile.getOriginalFilename(); |         String originalFilename = multipartFile.getOriginalFilename(); | ||||||
|         String extensionName = FileNameUtil.extName(originalFilename); |         String extensionName = FileNameUtil.extName(originalFilename); | ||||||
|  |  | ||||||
|         String fileName; |         String fileName; | ||||||
|         if (isKeepOriginalFilename) { |         if (isKeepOriginalFilename) { | ||||||
|             fileName = "%s-%s.%s".formatted(FileNameUtil.getPrefix(originalFilename), DateUtil.format(LocalDateTime |             fileName = "%s-%s.%s".formatted(FileNameUtil.getPrefix(originalFilename), DateUtil.format(LocalDateTime | ||||||
| @@ -59,7 +64,6 @@ public class FileUploadUtils { | |||||||
|         } else { |         } else { | ||||||
|             fileName = "%s.%s".formatted(IdUtil.fastSimpleUUID(), extensionName); |             fileName = "%s.%s".formatted(IdUtil.fastSimpleUUID(), extensionName); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         try { |         try { | ||||||
|             String pathname = filePath + fileName; |             String pathname = filePath + fileName; | ||||||
|             File dest = new File(pathname).getCanonicalFile(); |             File dest = new File(pathname).getCanonicalFile(); | ||||||
| @@ -71,8 +75,35 @@ public class FileUploadUtils { | |||||||
|             multipartFile.transferTo(dest); |             multipartFile.transferTo(dest); | ||||||
|             return dest; |             return dest; | ||||||
|         } catch (Exception e) { |         } catch (Exception e) { | ||||||
|             log.error("Upload file occurred an error: {}. fileName: {}.", e.getMessage(), fileName, e); |             log.error(e.getMessage(), e); | ||||||
|         } |         } | ||||||
|         return null; |         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(); | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user