From 2a8c4d6805691a07cb51f53c3af4b1ceb23f2623 Mon Sep 17 00:00:00 2001 From: gitee Date: Fri, 18 Jul 2025 14:44:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=96=87=E4=BB=B6=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E5=8A=9F=E8=83=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FileController.java | 43 ++++++++++++++++ .../com/supervision/domain/SysByteArray.java | 10 ++++ .../com/supervision/service/FileService.java | 23 +++++++++ .../service/impl/SysFileServiceImpl.java | 51 +++++++++++++++++++ .../resources/mapper/SysByteArrayMapper.xml | 4 +- 5 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/supervision/controller/FileController.java create mode 100644 src/main/java/com/supervision/service/FileService.java create mode 100644 src/main/java/com/supervision/service/impl/SysFileServiceImpl.java diff --git a/src/main/java/com/supervision/controller/FileController.java b/src/main/java/com/supervision/controller/FileController.java new file mode 100644 index 0000000..bee2244 --- /dev/null +++ b/src/main/java/com/supervision/controller/FileController.java @@ -0,0 +1,43 @@ +package com.supervision.controller; + +import com.supervision.dto.R; +import com.supervision.service.FileService; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + +/** + * 文件管理 + */ +@RestController +@RequestMapping("/file") +@RequiredArgsConstructor +public class FileController { + + private final FileService fileService; + + /** + * 上传文件 + * @param file 文件 + * @return + */ + @PostMapping("/upload") + public R upload(@RequestPart("file") MultipartFile file) throws IOException { + + String fileId = fileService.upload(file); + return R.ok(fileId); + } + + + /** + * 下载文件 + * @param fileId + * @param response + */ + @GetMapping("/download") + public void downLoad(@RequestParam("fileId") String fileId, HttpServletResponse response) { + fileService.downLoad(fileId,response); + } +} diff --git a/src/main/java/com/supervision/domain/SysByteArray.java b/src/main/java/com/supervision/domain/SysByteArray.java index 64e1ff8..091cb5c 100644 --- a/src/main/java/com/supervision/domain/SysByteArray.java +++ b/src/main/java/com/supervision/domain/SysByteArray.java @@ -25,6 +25,16 @@ public class SysByteArray implements Serializable { private String contentType; + /** + * 文件大小 + */ + private Long size; + + /** + * 文件名 + */ + private String fileName; + /** * 创建时间 */ diff --git a/src/main/java/com/supervision/service/FileService.java b/src/main/java/com/supervision/service/FileService.java new file mode 100644 index 0000000..d45ca22 --- /dev/null +++ b/src/main/java/com/supervision/service/FileService.java @@ -0,0 +1,23 @@ +package com.supervision.service; + +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + +public interface FileService { + + /** + * 上传文件 + * @param file 文件 + * @return 文件ID + */ + String upload(MultipartFile file) throws IOException; + + + /** + * 下载文件 + * @param fileId 文件ID + * @param response HTTP响应对象 + */ + void downLoad(String fileId, HttpServletResponse response); +} diff --git a/src/main/java/com/supervision/service/impl/SysFileServiceImpl.java b/src/main/java/com/supervision/service/impl/SysFileServiceImpl.java new file mode 100644 index 0000000..88714de --- /dev/null +++ b/src/main/java/com/supervision/service/impl/SysFileServiceImpl.java @@ -0,0 +1,51 @@ +package com.supervision.service.impl; + +import cn.hutool.core.lang.Assert; +import com.supervision.domain.SysByteArray; +import com.supervision.service.FileService; +import com.supervision.service.SysByteArrayService; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; +import java.io.IOException; + +@Slf4j +@Service +@RequiredArgsConstructor +public class SysFileServiceImpl implements FileService { + + private final SysByteArrayService sysByteArrayService; + @Override + public String upload(MultipartFile file) throws IOException { + + SysByteArray byteArray = new SysByteArray(); + byteArray.setFileName(file.getOriginalFilename()); + byteArray.setBytes(file.getBytes()); + byteArray.setSize(file.getSize()); + byteArray.setContentType(file.getContentType()); + sysByteArrayService.save(byteArray); + return byteArray.getId(); + } + + @Override + public void downLoad(String fileId, HttpServletResponse response) { + Assert.notEmpty(fileId, "文件ID不能为空"); + SysByteArray byteArray = sysByteArrayService.getById(fileId); + Assert.notNull(byteArray, "文件不存在或已被删除"); + response.setContentType(byteArray.getContentType()); + response.setHeader("Content-Disposition", "attachment; filename=\"" + byteArray.getFileName() + "\""); + try { + response.getOutputStream().write(byteArray.getBytes()); + response.getOutputStream().flush(); + } catch (IOException e) { + log.error("文件下载失败: {}", e.getMessage(), e); + throw new RuntimeException("文件下载失败", e); + } + } +} + + + + diff --git a/src/main/resources/mapper/SysByteArrayMapper.xml b/src/main/resources/mapper/SysByteArrayMapper.xml index 8c04a42..1c57136 100644 --- a/src/main/resources/mapper/SysByteArrayMapper.xml +++ b/src/main/resources/mapper/SysByteArrayMapper.xml @@ -8,12 +8,14 @@ + + - id,bytes,create_time,content_type, + id,bytes,create_time,content_type,size,file_name, update_time