package com.controller; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Random; import java.util.UUID; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.ResourceUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import com.annotation.IgnoreAuth; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.entity.ConfigEntity; import com.entity.EIException; import com.service.ConfigService; import com.utils.R; /** * 文件上传与下载控制器 */ @RestController @RequestMapping("file") @SuppressWarnings({"unchecked","rawtypes"}) public class FileController { @Autowired private ConfigService configService; // 注入配置服务类 /** * 文件上传接口 * @param file 文件对象 * @param type 类型标识(1表示人脸文件) * @return 返回上传成功的文件名 * @throws Exception 异常处理 */ @RequestMapping("/upload") public R upload(@RequestParam("file") MultipartFile file, String type) throws Exception { if (file.isEmpty()) { // 检查文件是否为空 throw new EIException("上传文件不能为空"); // 抛出异常 } String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); // 获取文件扩展名 File path = new File(ResourceUtils.getURL("classpath:static").getPath()); // 获取静态资源路径 if (!path.exists()) { // 如果路径不存在 path = new File(""); // 设置默认路径 } File upload = new File(path.getAbsolutePath(), "/upload/"); // 定义上传目录 if (!upload.exists()) { // 如果上传目录不存在 upload.mkdirs(); // 创建目录 } String fileName = new Date().getTime() + "." + fileExt; // 生成唯一的文件名 File dest = new File(upload.getAbsolutePath() + "/" + fileName); // 定义目标文件路径 file.transferTo(dest); // 将文件保存到目标位置 /** * 如果使用IDE(如IntelliJ IDEA或Eclipse)重启项目时发现上传的文件丢失: * 1. 注释掉此段代码中的文件复制逻辑。 * 2. 替换路径为本地项目的实际路径(例如:"D:\\\\springbootq33sd\\\\src\\\\main\\\\resources\\\\static\\\\upload")。 * 3. 确保路径中不含中文、空格等特殊字符。 */ // FileUtils.copyFile(dest, new File("D:\\\\springbootq33sd\\\\src\\\\main\\\\resources\\\\static\\\\upload\\\\" + fileName)); // 复制文件到指定路径 if (StringUtils.isNotBlank(type) && type.equals("1")) { // 如果类型为1(人脸文件) ConfigEntity configEntity = configService.selectOne(new EntityWrapper().eq("name", "faceFile")); // 查询配置项 if (configEntity == null) { // 如果配置项不存在 configEntity = new ConfigEntity(); // 创建新配置项 configEntity.setName("faceFile"); // 设置配置项名称 configEntity.setValue(fileName); // 设置文件名 } else { configEntity.setValue(fileName); // 更新文件名 } configService.insertOrUpdate(configEntity); // 插入或更新配置项 } return R.ok().put("file", fileName); // 返回成功结果 } /** * 文件下载接口 * @param fileName 文件名 * @return 返回文件流 */ @IgnoreAuth @RequestMapping("/download") public ResponseEntity download(@RequestParam String fileName) { try { File path = new File(ResourceUtils.getURL("classpath:static").getPath()); // 获取静态资源路径 if (!path.exists()) { // 如果路径不存在 path = new File(""); // 设置默认路径 } File upload = new File(path.getAbsolutePath(), "/upload/"); // 定义上传目录 if (!upload.exists()) { // 如果上传目录不存在 upload.mkdirs(); // 创建目录 } File file = new File(upload.getAbsolutePath() + "/" + fileName); // 定义目标文件路径 if (file.exists()) { // 如果文件存在 HttpHeaders headers = new HttpHeaders(); // 设置响应头 headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 设置内容类型 headers.setContentDispositionFormData("attachment", fileName); // 设置附件下载 return new ResponseEntity(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK); // 返回文件流 } } catch (IOException e) { e.printStackTrace(); // 打印异常日志 } return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR); // 返回错误状态码 } }