在将代码保存到文件系统之前,我想在其中重命名该代码。我在堆栈溢出中尝试了其他问题,但不适用于我。希望您能帮我这是我的代码。
@PostMapping("/api/file/upload")
public @ResponseBody String uploadMultipartFile(@RequestParam("uploadfile") MultipartFile file) {
try {
fileStorage.store(file);
return "File uploaded successfully! -> filename = " + file.getOriginalFilename();
} catch (Exception e) {
return "Error -> message = " + e.getMessage();
}
}
这是我的商店功能:
@Override
public void store(MultipartFile file){
try {
Files.copy(file.getInputStream(), this.rootLocation.resolve(file.getOriginalFilename()));
} catch (Exception e) {
throw new RuntimeException("FAIL2! -> message2 = " + e.getMessage());
}
}
我尝试重命名原始文件,但是它不起作用。
希望您能帮助我。非常感谢!!!
答案 0 :(得分:1)
下面的代码段在这里和那里几乎没有修改:
@PostMapping(value = "/api/file/upload", headers = {"content-type=multipart/*"})
public @ResponseBody String uploadMultipartFile(@RequestParam("uploadfile") MultipartFile file) {
Path TO = Paths.get("/Users/myusername/Desktop/newfileName");
try {
try {
Files.copy(file.getInputStream(), TO);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("FAIL2! -> message2 = " + e.getMessage());
}
return "File uploaded successfully! -> filename = " + file.getOriginalFilename();
} catch (Exception e) {
e.printStackTrace();
return "Error -> message = " + e.getMessage();
}
}
输出屏幕: