方法详解
使用实例
@Value("${dirPath}")
private String dirPath;
@RequestMapping("/upload")
public String upload(MultipartFile picFile) throws IOException {
System.out.println("picFile = " + picFile);
String fileName = picFile.getOriginalFilename();
System.out.println(fileName);
String suffix = fileName.substring(fileName.lastIndexOf("."));
fileName= UUID.randomUUID()+suffix;
System.out.println("新文件名=" + fileName);
File dirFile = new File(dirPath);
if (!dirFile.exists()){
dirFile.mkdirs();
}
String filePath = dirPath + fileName;
picFile.transferTo(new File(filePath));
return fileName;
}
@RequestMapping("/remove")
public void remove(String fileName){
File dirFile = new File(dirPath + fileName);
dirFile.delete();
}