文件上传
一、文件上传介绍
文件上传,也称为upload,是指将本地图片、视频、音频等文件上传到服务器上,可以供其他用户浏览或下载的过程。文件上传在项目中应用非常广泛,我们经常发微博、发微信朋友圈都用到了文件上传功能。
服务端要接收客户端页面上传的文件,通常都会使用Apache的两个组件:
commons-fileupload commons-io
Spring框架在spring-web包中对文件上传进行了封装,大大简化了服务端代码,我们只需要在Controller的方法中声明一个MultipartFile类型的参数即可接收上传的文件,例如:
二、 文件上传代码实现
1. 编写数据表 file
Sql:
CREATE TABLE `sys_file` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`name` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '文件名称',
`type` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '文件类型',
`size` bigint(20) DEFAULT NULL COMMENT '文件大小(kb)',
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '下载链接',
`md5` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '文件md5',
`is_delete` tinyint(1) DEFAULT '0' COMMENT '是否删除',
`enable` tinyint(1) DEFAULT '1' COMMENT '是否禁用链接',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
表结构:
其中is_delete和enable的默认值分别是0和1
2. 后端代码编写
文件上传的代码逻辑
增加一个控制层类FileController.java:
在配置文件application.yml添加文件上传到的位置:
files:
upload:
path: F:/后台管理系统/files/
文件磁盘路径:
2.1 文件上传代码一
@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
@Value("${files.upload.path}")
private String fileUploadPath;
@PostMapping("/upload")
public Result upload(@RequestParam MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
String type = FileUtil.extName(originalFilename);
log.info("文件类型是:" + type);
long size = file.getSize();
File uploadParentFile = new File(fileUploadPath);
if(!uploadParentFile.exists()) {
uploadParentFile.mkdirs();
}
String uuid = UUID.randomUUID().toString();
File uploadFile = new File(fileUploadPath + uuid + StrUtil.DOT + type);
file.transferTo(uploadFile);
return Result.success("");
}
}
2.1.1 关键代码
1. 将临时文件转存到指定位置
file.transferTo(new File(basePath + fileName));
2. 注意:文件后缀(类型)要加上
String type = FileUtil.extName(originalFilename);
log.info("文件类型是:" + type);
StrUtil.DOT表示"."
2.1.2 补充
FileUtil 类是一个工具类,需要配置hutool包的依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.20</version>
</dependency>
2.1.3 测试
我们现在apifox里面进行测试:
文件已经创建:
2.2 文件上传代码二(存储至数据库)
2.2.1 编写实体类File.java
@Getter
@Setter
@TableName("sys_file")
@ApiModel(value = "File对象", description = "")
public class File implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty("id")
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@ApiModelProperty("文件名称")
private String name;
@ApiModelProperty("文件类型")
private String type;
@ApiModelProperty("文件大小")
private Long size;
@ApiModelProperty("下载链接")
private String url;
@ApiModelProperty("是否删除")
private Boolean isDelete;
@ApiModelProperty("是否禁用链接")
private Boolean enable;
}
2.2.2 FileController.java
@PostMapping("/upload")
public String upload(@RequestParam MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
String type = FileUtil.extName(originalFilename);
log.info("文件类型是:" + type);
long size = file.getSize();
File uploadParentFile = new File(fileUploadPath);
if(!uploadParentFile.exists()) {
uploadParentFile.mkdirs();
}
String uuid = UUID.randomUUID().toString();
String fileUUID = uuid + StrUtil.DOT + type;
File uploadFile = new File(fileUploadPath + fileUUID);
file.transferTo(uploadFile);
String url = "http://localhost:9090/file/" + fileUUID;
Files saveFile = new Files();
saveFile.setName(originalFilename);
saveFile.setType(type);
saveFile.setSize(size/1024);
saveFile.setUrl(url);
fileMapper.insert(saveFile);
return url;
}
2.3 优化代码
由于我们上传上去的图片有重复的,所以我们需要去重,只让他们共享一个图像
去重的思路为:将文件的二进制流转换为MD5编码,每当我们上传一个文件就将其二进制流MD5与数据库当中已保存的文件的二进制流MD5进行比较,相同就舍弃,不相同就将文件的信息保存至数据库,文件内容上传至文件夹。
@PostMapping("/upload")
public String upload(@RequestParam MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
String type = FileUtil.extName(originalFilename);
log.info("文件类型是:" + type);
long size = file.getSize();
File uploadParentFile = new File(fileUploadPath);
if(!uploadParentFile.exists()) {
uploadParentFile.mkdirs();
}
String uuid = UUID.randomUUID().toString();
String fileUUID = uuid + StrUtil.DOT + type;
File uploadFile = new File(fileUploadPath + fileUUID);
String url;
String md5 = SecureUtil.md5(file.getInputStream());
Files dbFiles = fileService.getFileByMd5(md5);
if (dbFiles != null) {
url = dbFiles.getUrl();
} else {
file.transferTo(uploadFile);
url = "http://localhost:9090/file/" + fileUUID;
}
Files saveFile = new Files();
saveFile.setName(originalFilename);
saveFile.setType(type);
saveFile.setSize(size/1024);
saveFile.setUrl(url);
saveFile.setMd5(md5);
fileService.save(saveFile);
return url;
}
FileServiceImpl.java
@Service
public class FileServiceImpl extends ServiceImpl<FileMapper, Files> implements IFileService {
@Override
public Files getFileByMd5(String md5) {
LambdaQueryWrapper<Files> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Files::getMd5, md5);
List<Files> list = this.list(queryWrapper);
return list.size() == 0 ? null : list.get(0);
}
}
|