package com.controller; import java.io.File; import java.io.IOException; import java.util.Date; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; // 导入Spring的依赖注入注解 import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; // 导入Spring的REST控制器注解 import org.springframework.web.multipart.MultipartFile; // 导入Spring的文件上传接口 import com.annotation.IgnoreAuth; // 导入自定义注解,表示忽略身份验证 import com.baomidou.mybatisplus.mapper.EntityWrapper; // 导入MyBatis-Plus的EntityWrapper,用于构建查询条件 import com.entity.ConfigEntity; import com.entity.EIException; import com.service.ConfigService; import com.utils.R; /** * 上传文件映射表 */ @RestController // 标记该类为REST控制器 @RequestMapping("file") // 设置基本请求路径为"/file" @SuppressWarnings({"unchecked","rawtypes"}) // 忽略编译警告 public class FileController { @Autowired // 自动注入ConfigService服务 private ConfigService configService; /** * 上传文件 * @param file 上传的文件 * @param type 文件类型标识 * @param request HttpServletRequest对象 * @return 上传结果 */ @RequestMapping("/upload") // 映射请求路径为"/upload" @IgnoreAuth // 忽略身份验证 public R upload(@RequestParam("file") MultipartFile file, String type, HttpServletRequest request) throws Exception { // 检查上传的文件是否为空 if (file.isEmpty()) { throw new EIException("上传文件不能为空"); } // 获取文件扩展名 String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1); String fileName = new Date().getTime() + "." + fileExt; // 定义文件保存的目标路径 File dest = new File(request.getSession().getServletContext().getRealPath("/upload") + "/" + fileName); file.transferTo(dest); /** * 如果使用IDEA或者Eclipse重启项目后发现之前上传的文件丢失, * 请将下面一行代码注释打开 * 并将"D:\\ssmpiv99\\src\\main\\webapp\\upload"替换为你本地项目的upload路径, * 确保项目路径中不存在中文、空格等特殊字符 */ // FileUtils.copyFile(dest, new File("D:\\ssmpiv99\\src\\main\\webapp\\upload" + "/" + fileName)); /** 修改了路径后请将该行最前面的//注释去掉 **/ // 如果type不为空且等于"1",则更新或保存配置信息 if (StringUtils.isNotBlank(type) && type.equals("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 文件名 * @param request HttpServletRequest对象 * @param response HttpServletResponse对象 */ @IgnoreAuth // 忽略身份验证 @RequestMapping("/download") // 映射请求路径为"/download" public void download(@RequestParam String fileName, HttpServletRequest request, HttpServletResponse response) { try { // 定义文件的完整路径 File file = new File(request.getSession().getServletContext().getRealPath("/upload") + "/" + fileName); // 检查文件是否存在 if (file.exists()) { response.reset(); // 重置响应 // 设置响应头,指示文件下载 response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Access-Control-Allow-Credentials", "true"); response.setContentType("application/octet-stream; charset=UTF-8"); // 设置内容类型 // 将文件内容写入响应输出流 IOUtils.write(FileUtils.readFileToByteArray(file), response.getOutputStream()); } } catch (IOException e) { e.printStackTrace(); // 打印异常信息 } } }