diff --git a/tamguo-mms/src/main/java/com/tamguo/web/FileController.java b/tamguo-mms/src/main/java/com/tamguo/web/FileController.java index 5452a76..0774691 100644 --- a/tamguo-mms/src/main/java/com/tamguo/web/FileController.java +++ b/tamguo-mms/src/main/java/com/tamguo/web/FileController.java @@ -1,73 +1,81 @@ package com.tamguo.web; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.util.Date; +import java.io.File; // 文件类 +import java.io.FileInputStream; // 文件输入流类 +import java.io.IOException; // 输入输出异常类 +import java.util.Date; // 日期类 -import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequest; // HTTP 请求类 -import org.apache.commons.io.FileUtils; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.multipart.MultipartFile; +import org.apache.commons.io.FileUtils; // 文件工具类 +import org.springframework.beans.factory.annotation.Autowired; // 自动注入注解 +import org.springframework.beans.factory.annotation.Value; // 值注入注解 +import org.springframework.stereotype.Controller; // 控制器类 +import org.springframework.web.bind.annotation.RequestMapping; // 请求映射 +import org.springframework.web.bind.annotation.RequestMethod; // 请求方法 +import org.springframework.web.bind.annotation.RequestParam; // 请求参数 +import org.springframework.web.bind.annotation.ResponseBody; // 返回响应体 -import com.baomidou.mybatisplus.mapper.Condition; -import com.tamguo.common.utils.DateUtil; -import com.tamguo.common.utils.Result; -import com.tamguo.modules.book.model.FileEntity; -import com.tamguo.modules.book.service.IFileEntityService; -import com.tamguo.utils.FileMd5Utils; +import com.baomidou.mybatisplus.mapper.Condition; // 条件类 +import com.tamguo.common.utils.DateUtil; // 日期工具类 +import com.tamguo.common.utils.Result; // 通用结果类 +import com.tamguo.modules.book.model.FileEntity; // 文件实体类 +import com.tamguo.modules.book.service.IFileEntityService; // 文件服务接口 +/** + * FileController 类,处理文件上传相关的请求 + */ @Controller public class FileController { - - @Value("${file.storage.path}") + + @Value("${file.storage.path}") // 获取配置文件中文件存储路径的值 private String fileStoragePath; - @Value("${tamguo.domain.name}") + + @Value("${tamguo.domain.name}") // 获取配置文件中域名的值 private String domainName; - @Autowired + + @Autowired // 自动注入文件服务实例 private IFileEntityService iFileEntityService; + /** + * 处理上传图片的请求 + * @param file 上传的文件 + * @param request HTTP 请求对象 + * @return 响应结果 + */ @SuppressWarnings("unchecked") - @RequestMapping(value = "uploadImage" , method = RequestMethod.POST) + @RequestMapping(value = "uploadImage", method = RequestMethod.POST) @ResponseBody public Result uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) { try { - String fileMd5 = FileMd5Utils.getMD5((FileInputStream)file.getInputStream()); - FileEntity sysFile = iFileEntityService.selectOne(Condition.create().eq("file_md5", fileMd5)); - if(sysFile != null) { - sysFile.setFilePath(domainName + "files/" + sysFile.getFilePath()); - return Result.successResult(sysFile); + String fileMd5 = FileMd5Utils.getMD5((FileInputStream)file.getInputStream()); // 获取文件的 MD5 值 + FileEntity sysFile = iFileEntityService.selectOne(Condition.create().eq("file_md5", fileMd5)); // 根据文件 MD5 查找文件实体 + if (sysFile!= null) { // 如果找到文件实体 + sysFile.setFilePath(domainName + "files/" + sysFile.getFilePath()); // 设置文件路径 + return Result.successResult(sysFile); // 返回文件实体 } - String filePath = fileStoragePath + DateUtil.fomatDate(new Date(), "yyyyMM") + "/avatar"; - File dest = new File(filePath); - if(!dest.exists()) { - dest.mkdirs(); + String filePath = fileStoragePath + DateUtil.fomatDate(new Date(), "yyyyMM") + "/avatar"; // 设置文件存储路径 + File dest = new File(filePath); // 创建文件对象 + if (!dest.exists()) { // 如果文件不存在 + dest.mkdirs(); // 创建文件夹 } - // save 文件 - FileUtils.writeByteArrayToFile(new File(filePath + "/" + file.getOriginalFilename()) , file.getBytes()); - - FileEntity fileEntity = new FileEntity(); - fileEntity.setFileContentType(file.getContentType()); - fileEntity.setFileExtension(file.getOriginalFilename()); - fileEntity.setFileMd5(FileMd5Utils.getMD5((FileInputStream)file.getInputStream())); - fileEntity.setFileSize(file.getSize()); - fileEntity.setFilePath(DateUtil.fomatDate(new Date(), "yyyyMM") + "/avatar/" + file.getOriginalFilename()); - iFileEntityService.insert(fileEntity); - - fileEntity.setFilePath(domainName + "files/" + fileEntity.getFilePath()); - - return Result.successResult(fileEntity); + // 保存文件 + FileUtils.writeByteArrayToFile(new File(filePath + "/" + file.getOriginalFilename()), file.getBytes()); + + FileEntity fileEntity = new FileEntity(); // 创建文件实体对象 + fileEntity.setFileContentType(file.getContentType()); // 设置文件类型 + fileEntity.setFileExtension(file.getOriginalFilename()); // 设置文件扩展名 + fileEntity.setFileMd5(FileMd5Utils.getMD5((FileInputStream)file.getInputStream())); // 设置文件 MD5 值 + fileEntity.setFileSize(file.getSize()); // 设置文件大小 + fileEntity.setFilePath(DateUtil.fomatDate(new Date(), "yyyyMM") + "/avatar/" + file.getOriginalFilename()); // 设置文件路径 + iFileEntityService.insert(fileEntity); // 插入文件实体到数据库 + + fileEntity.setFilePath(domainName + "files/" + fileEntity.getFilePath()); // 设置文件路径 + + return Result.successResult(fileEntity); // 返回文件实体 } catch (IOException e) { - e.printStackTrace(); - return Result.failResult("上传失败"); + e.printStackTrace(); // 打印异常信息 + return Result.failResult("上传失败"); // 返回上传失败的结果 } } - -} +} \ No newline at end of file