SpringBoot's Class directory saves uploaded files
package cn.js.Controller;
import org.apache.commons.codec.digest.DigestUtils;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.net.URL;
import java.util.UUID;
@RequestMapping("/filesave")
@RestController
public class FileUpdateTest {
@PostMapping("/supdate")
public String savefile(@RequestParam("file") MultipartFile file) throws IOException{
String filename=file.getOriginalFilename();
URL resourceUrl = getClass().getResource("/");
File directory = new File(resourceUrl.getFile() + "/saved_files");
directory.mkdirs();
UUID uuid = UUID.randomUUID();
String uuidStr = uuid.toString().replace("-", "");
String hash = DigestUtils.md5Hex(uuidStr);
String uid= hash.substring(0, 8);
String path = directory.getPath();
System.out.println("文件路径:"+path);
File fileToSave = new File(directory.getPath() +File.separator + uid+filename);
FileOutputStream fos = new FileOutputStream(fileToSave);
FileInputStream fis =(FileInputStream) file.getInputStream();
byte[] buf = new byte[1024];
int len;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
File feile=new File(path+File.separator + uid+filename);
feile.delete();
return "保存成功";
}
}