You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ass/src/main/java/com/controller/FileController.java

115 lines
4.9 KiB

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;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
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 上传的文件,通过 @RequestParam 注解绑定到 MultipartFile 对象
// @param type 文件类型标识,用于判断是否需要更新配置信息
// @param request HTTP 请求对象,用于获取服务器相关路径信息
// @return R 响应对象,包含操作结果和上传后的文件名
// @throws Exception 可能抛出的异常,如文件上传过程中的异常
@RequestMapping("/upload")
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;
// 创建目标文件对象,指定文件保存路径为服务器的 /upload 目录下
File dest = new File(request.getSession().getServletContext().getRealPath("/upload") + "/" + fileName);
// 将上传的文件保存到目标路径
file.transferTo(dest);
// 如果 type 不为空且等于 "1",则进行配置信息的更新操作
if (StringUtils.isNotBlank(type) && type.equals("1")) {
// 根据配置名称 "faceFile" 查询配置实体
ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().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 要下载的文件名,通过 @RequestParam 注解获取
// @param request HTTP 请求对象,用于获取服务器相关路径信息
// @param response HTTP 响应对象,用于设置响应头和输出文件内容
@IgnoreAuth
@RequestMapping("/download")
public void download(@RequestParam String fileName, HttpServletRequest request, HttpServletResponse response) {
try {
// 创建要下载的文件对象,指定文件路径为服务器的 /upload 目录下
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) {
// 如果发生 I/O 异常,打印异常堆栈信息
e.printStackTrace();
}
}
}