Skip to content

Commit

Permalink
开发一个上传文件接口,将上传的文件保存到服务器中,并将文件信息:如文件名、文件类型、文件大小保存到数据库
Browse files Browse the repository at this point in the history
  • Loading branch information
charging-kuafuai committed Mar 15, 2024
1 parent 747eda9 commit 4ce0079
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 59 deletions.
55 changes: 55 additions & 0 deletions src/main/java/com/aiassistant/controller/FileController.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/aiassistant/mapper/FileMapper.java
Original file line number Diff line number Diff line change
@@ -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);
}
1 change: 1 addition & 0 deletions src/main/java/com/aiassistant/model/File.java
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
`com.aiassistant.model.File`文件中定义`File`
42 changes: 42 additions & 0 deletions src/main/java/com/aiassistant/service/FileService.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
108 changes: 49 additions & 59 deletions src/main/java/com/aiassistant/utils/ResultModel.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,81 @@
package com.aiassistant.utils;

import lombok.Data;

/**
* 控制层-对外返回数据结构
*
* @param <T>
*/
@Data
public class ResultModel<T> {

private Exception exception = null;
private T data = null;
private Exception exception;
private T data;
private String msg;
private Integer code;

public static <T> ResultModel ofResult(Integer code, String msg, T data) {
return ofResult(code, msg, data, null);
public static <T> ResultModel<T> ofResult(Integer code, String msg, T data) {
ResultModel<T> resultModel = new ResultModel<>();
resultModel.setCode(code);
resultModel.setMsg(msg);
resultModel.setData(data);
return resultModel;
}

public static <T> ResultModel ofResult(Integer code, String msg, T data, Exception exception) {
ResultModel<T> resultModel = new ResultModel();
public static <T> ResultModel<T> ofResult(Integer code, String msg, T data, Exception exception) {
ResultModel<T> 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 <T> ResultModel<T> ofSuccess() {
return ofResult(200, "success", null);
}

return ofSuccess(null);
public static <T> ResultModel<T> ofSuccess(T data) {
return ofResult(200, "success", data);
}

/**
* 创建成功返回-带返回数据
*
* @param data
* @param <T>
* @return
*/
public static <T> ResultModel ofSuccess(T data) {
public static <T> ResultModel<T> ofSuccess(String msg, T data) {
return ofResult(200, msg, data);
}

return ofSuccess(null, data);
public static <T> ResultModel<T> ofError() {
return ofResult(500, "error", null);
}

/**
* 创建成功返回-带返回信息和返回数据
*
* @param msg
* @param data
* @param <T>
* @return
*/
public static <T> ResultModel ofSuccess(String msg, T data) {
public static <T> ResultModel<T> ofError(String msg) {
return ofResult(500, msg, null);
}

public static <T> ResultModel<T> 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;
}
}

0 comments on commit 4ce0079

Please sign in to comment.