ADD file via upload

pull/1/head
phfsut2ie 4 months ago
parent 6d29c3aa9c
commit eaaec072ae

@ -0,0 +1,159 @@
package com.controller; // 定义包路径
// 文件操作相关类
import java.io.File;
// 文件未找到异常
import java.io.FileNotFoundException;
// IO异常
import java.io.IOException;
// 数组工具类
import java.util.Arrays;
// 日期类
import java.util.Date;
// 哈希Map
import java.util.HashMap;
// 列表接口
import java.util.List;
// Map接口
import java.util.Map;
// 随机数类
import java.util.Random;
// UUID类
import java.util.UUID;
// Apache Commons IO工具类
import org.apache.commons.io.FileUtils;
// Apache Commons字符串工具
import org.apache.commons.lang3.StringUtils;
// Spring自动注入注解
import org.springframework.beans.factory.annotation.Autowired;
// HTTP头定义
import org.springframework.http.HttpHeaders;
// HTTP状态码
import org.springframework.http.HttpStatus;
// 媒体类型定义
import org.springframework.http.MediaType;
// ResponseEntity响应实体
import org.springframework.http.ResponseEntity;
// Spring资源工具
import org.springframework.util.ResourceUtils;
// Spring路径变量注解
import org.springframework.web.bind.annotation.PathVariable;
// Spring请求体注解
import org.springframework.web.bind.annotation.RequestBody;
// Spring请求映射注解
import org.springframework.web.bind.annotation.RequestMapping;
// Spring请求参数注解
import org.springframework.web.bind.annotation.RequestParam;
// Spring REST控制器注解
import org.springframework.web.bind.annotation.RestController;
// Spring文件上传类
import org.springframework.web.multipart.MultipartFile;
// 自定义忽略鉴权注解
import com.annotation.IgnoreAuth;
// MyBatis Plus实体包装器
import com.baomidou.mybatisplus.mapper.EntityWrapper;
// 配置实体类
import com.entity.ConfigEntity;
// 自定义异常类
import com.entity.EIException;
// 配置服务接口
import com.service.ConfigService;
// 自定义返回结果类
import com.utils.R;
//上传文件映射表
//文件上传下载控制器
@RestController // 声明为RESTful控制器
@RequestMapping("file") // 基础请求路径映射
@SuppressWarnings({"unchecked","rawtypes"}) // 抑制警告
public class FileController{
@Autowired // 自动注入配置服务
private ConfigService configService;
//上传文件
//@param file 上传的文件对象
//@param type 文件类型标识
//@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);
// 如果是类型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 要下载的文件名
// @return 文件下载响应
@IgnoreAuth // 忽略权限验证
@RequestMapping("/download") // 文件下载请求映射
public ResponseEntity<byte[]> 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()){ // 如果文件存在
// 设置HTTP响应头
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); // 设置内容类型为二进制流
headers.setContentDispositionFormData("attachment", fileName); // 设置内容处置为附件
// 返回文件内容和响应头
return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
} catch (IOException e) { // 捕获IO异常
e.printStackTrace(); // 打印异常堆栈
}
// 返回服务器错误响应
return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Loading…
Cancel
Save