@Slf4j
@RestController
@RequestMapping("/upload")
public class UploadController {
@PostMapping
public Result upload(String username, Integer age, MultipartFile image) throws Exception{
log.info("文件上传:{},{},{}",username,age,image);
//获取原始文件名
String originalFilename = image.getOriginalFilename();
//构造唯一的文件名(不能重复)-uuid(通用唯一标识码)
int index = originalFilename.lastIndexOf(".");
String extname = originalFilename.substring(index);
String newFileName = UUID.randomUUID().toString() + extname;
log.info("新的文件名:",newFileName);
//将文件存储在服务器的磁盘目录中
image.transferTo(new File("D:\\软件\\study\\IntelliJ IDEA 2023.2.4\\learn\\tlias-web-management\\" +
"stockpile\\" + newFileName));
return Result.success();
}
}
利用PostMan工具进行测试
生成的文件名是随机的