我一直在尝试使用相对路径和随机String重命名文件。问题是我无法更改多部分文件的名称。
@RequestMapping("/upload")
public String upload(Model model, @RequestParam("files")MultipartFile[] files) {
StringBuilder fileNames = new StringBuilder();
String URL = Util.IMAGE_URL + Util.generateString();
// Mock
Post post = postService.getPostById(2L);
Picture picture = new Picture();
char[] chars = URL.toCharArray();
for (MultipartFile file : files) {
Path fileNameAndPAth = Paths.get(Util.UPLOAD_DIRECTORY, file.getOriginalFilename());
fileNames.append(file.getOriginalFilename());
// This didn't work.
fileNames.replace(0, fileNames.length(), URL);
System.out.println(fileNames);
System.out.println(fileNameAndPAth);
try {
Files.write(fileNameAndPAth, file.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
model.addAttribute("message", "Successfully uploaded files " + fileNames.toString());
// mock
model.addAttribute("picture", postService.getPostById(post.getId()).getPictures());
return "test/status";
}