Tip : This is my personal IT resource website, you can go in and have a look
After uploading the file in the project, you need to rename the file to avoid duplication of file names. Let’s organize it
below. Two packages need to be introduced. The following are dependencies. If it is not a maven project, you can go to the maven official website to download the jar package.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-mock</artifactId>
<version>2.0.8</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.3.1</version>
</dependency>
We generally use the MultipartFile type to receive files, but the file type is needed to modify the name, so we need to convert it. After renaming, if it needs to be converted to MultipartFile and then converted to MultipartFile
String originalFilename = multipartFile.getOriginalFilename();
String[] filename = originalFilename.split("\\.");
//临时文件
File file=File.createTempFile(filename[0], "."+filename[1]);
multipartFile.transferTo(file);
//重命名
file = FileUtil.rename(file, RandomUtil.simpleUUID(), true, true);
//File转换成MultipartFile
multipartFile = new MockMultipartFile(file.getName(),
//程序退出后删除临时文件
file.deleteOnExit();