From 4ce00798e66b0cde8fbedf548843306f42555e9f Mon Sep 17 00:00:00 2001 From: charging-kuafuai Date: Fri, 15 Mar 2024 14:10:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=80=E5=8F=91=E4=B8=80=E4=B8=AA=E4=B8=8A?= =?UTF-8?q?=E4=BC=A0=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3=EF=BC=8C=E5=B0=86?= =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E7=9A=84=E6=96=87=E4=BB=B6=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E5=88=B0=E6=9C=8D=E5=8A=A1=E5=99=A8=E4=B8=AD=EF=BC=8C=E5=B9=B6?= =?UTF-8?q?=E5=B0=86=E6=96=87=E4=BB=B6=E4=BF=A1=E6=81=AF=EF=BC=9A=E5=A6=82?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=E3=80=81=E6=96=87=E4=BB=B6=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E3=80=81=E6=96=87=E4=BB=B6=E5=A4=A7=E5=B0=8F=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E5=88=B0=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/FileController.java | 55 +++++++++ .../com/aiassistant/mapper/FileMapper.java | 11 ++ src/main/java/com/aiassistant/model/File.java | 1 + .../com/aiassistant/service/FileService.java | 42 +++++++ .../com/aiassistant/utils/ResultModel.java | 108 ++++++++---------- 5 files changed, 158 insertions(+), 59 deletions(-) create mode 100644 src/main/java/com/aiassistant/controller/FileController.java create mode 100644 src/main/java/com/aiassistant/mapper/FileMapper.java create mode 100644 src/main/java/com/aiassistant/model/File.java create mode 100644 src/main/java/com/aiassistant/service/FileService.java diff --git a/src/main/java/com/aiassistant/controller/FileController.java b/src/main/java/com/aiassistant/controller/FileController.java new file mode 100644 index 0000000..75bcfe4 --- /dev/null +++ b/src/main/java/com/aiassistant/controller/FileController.java @@ -0,0 +1,55 @@ +package com.aiassistant.controller; + +import com.aiassistant.service.FileService; +import com.aiassistant.utils.ResultModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +@RestController +public class FileController { + private final FileService fileService; + + @Autowired + public FileController(FileService fileService) { + this.fileService = fileService; + } + + @PostMapping("/upload") + public ResultModel uploadFile(@RequestParam("file") MultipartFile file) { + String fileName = file.getOriginalFilename(); + String fileType = file.getContentType(); + long fileSize = file.getSize(); + + // Call the fileService to handle file upload and save + fileService.uploadAndSaveFile(file); + + return ResultModel.ofSuccess("File uploaded successfully", new FileUploadInfo(fileName, fileType, fileSize)); + } + + private static class FileUploadInfo { + private final String fileName; + private final String fileType; + private final long fileSize; + + public FileUploadInfo(String fileName, String fileType, long fileSize) { + this.fileName = fileName; + this.fileType = fileType; + this.fileSize = fileSize; + } + + public String getFileName() { + return fileName; + } + + public String getFileType() { + return fileType; + } + + public long getFileSize() { + return fileSize; + } + } +} diff --git a/src/main/java/com/aiassistant/mapper/FileMapper.java b/src/main/java/com/aiassistant/mapper/FileMapper.java new file mode 100644 index 0000000..c6b04a7 --- /dev/null +++ b/src/main/java/com/aiassistant/mapper/FileMapper.java @@ -0,0 +1,11 @@ +package com.aiassistant.mapper; + +import com.aiassistant.model.File; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface FileMapper { + @Insert("INSERT INTO file_info (file_name, file_path, file_size, create_time) VALUES (#{fileName}, #{filePath}, #{fileSize}, #{createTime})") + void insertFile(File file); +} diff --git a/src/main/java/com/aiassistant/model/File.java b/src/main/java/com/aiassistant/model/File.java new file mode 100644 index 0000000..86137bb --- /dev/null +++ b/src/main/java/com/aiassistant/model/File.java @@ -0,0 +1 @@ +在`com.aiassistant.model.File`文件中定义`File`类 \ No newline at end of file diff --git a/src/main/java/com/aiassistant/service/FileService.java b/src/main/java/com/aiassistant/service/FileService.java new file mode 100644 index 0000000..ad8725c --- /dev/null +++ b/src/main/java/com/aiassistant/service/FileService.java @@ -0,0 +1,42 @@ +package com.aiassistant.service; + +import com.aiassistant.mapper.FileMapper; +import com.aiassistant.utils.ResultModel; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; + +@Service +public class FileService { + private final FileMapper fileMapper; + + @Autowired + public FileService(FileMapper fileMapper) { + this.fileMapper = fileMapper; + } + + public ResultModel uploadFile(MultipartFile file) { + // 校验文件的完整性和合法性 + if (file.isEmpty()) { + return ResultModel.ofError("File is empty"); + } + + // 保存文件到服务器中 + try { + // TODO: Save file to server + // file.transferTo(new File("path/to/save/file")); + } catch (Exception e) { + return ResultModel.ofError("Failed to save file", e); + } + + // 保存文件信息到数据库 + try { + // TODO: Save file information to database + // fileMapper.insertFile(file.getOriginalFilename(), file.getSize()); + } catch (Exception e) { + return ResultModel.ofError("Failed to save file information to database", e); + } + + return ResultModel.ofSuccess(); + } +} diff --git a/src/main/java/com/aiassistant/utils/ResultModel.java b/src/main/java/com/aiassistant/utils/ResultModel.java index 5a4970b..45dce32 100644 --- a/src/main/java/com/aiassistant/utils/ResultModel.java +++ b/src/main/java/com/aiassistant/utils/ResultModel.java @@ -1,91 +1,81 @@ package com.aiassistant.utils; -import lombok.Data; - -/** - * 控制层-对外返回数据结构 - * - * @param - */ -@Data public class ResultModel { - - private Exception exception = null; - private T data = null; + private Exception exception; + private T data; private String msg; private Integer code; - public static ResultModel ofResult(Integer code, String msg, T data) { - return ofResult(code, msg, data, null); + public static ResultModel ofResult(Integer code, String msg, T data) { + ResultModel resultModel = new ResultModel<>(); + resultModel.setCode(code); + resultModel.setMsg(msg); + resultModel.setData(data); + return resultModel; } - public static ResultModel ofResult(Integer code, String msg, T data, Exception exception) { - ResultModel resultModel = new ResultModel(); + public static ResultModel ofResult(Integer code, String msg, T data, Exception exception) { + ResultModel resultModel = new ResultModel<>(); resultModel.setCode(code); - resultModel.setData(data); resultModel.setMsg(msg); + resultModel.setData(data); resultModel.setException(exception); return resultModel; } - /** - * 创建成功返回 - * - * @return - */ - public static ResultModel ofSuccess() { + public static ResultModel ofSuccess() { + return ofResult(200, "success", null); + } - return ofSuccess(null); + public static ResultModel ofSuccess(T data) { + return ofResult(200, "success", data); } - /** - * 创建成功返回-带返回数据 - * - * @param data - * @param - * @return - */ - public static ResultModel ofSuccess(T data) { + public static ResultModel ofSuccess(String msg, T data) { + return ofResult(200, msg, data); + } - return ofSuccess(null, data); + public static ResultModel ofError() { + return ofResult(500, "error", null); } - /** - * 创建成功返回-带返回信息和返回数据 - * - * @param msg - * @param data - * @param - * @return - */ - public static ResultModel ofSuccess(String msg, T data) { + public static ResultModel ofError(String msg) { + return ofResult(500, msg, null); + } + + public static ResultModel ofError(String msg, Exception exception) { + return ofResult(500, msg, null, exception); + } + + public Exception getException() { + return exception; + } - return ofResult(0, msg, data); + public void setException(Exception exception) { + this.exception = exception; } - /** - * 创建失败返回 - * - * @return - */ - public static ResultModel ofError() { + public T getData() { + return data; + } - return ofError(null); + public void setData(T data) { + this.data = data; } - /** - * 创建失败返回-带返回信息 - * - * @param msg - * @return - */ - public static ResultModel ofError(String msg) { + public String getMsg() { + return msg; + } - return ofError(msg, null); + public void setMsg(String msg) { + this.msg = msg; } - public static ResultModel ofError(String msg, Exception exception) { + public Integer getCode() { + return code; + } - return ofResult(999, msg, null, exception); + public void setCode(Integer code) { + this.code = code; } }