I have this code where I wanted to rename the before saving it to the file system. I tried other questions here in stack overflow but it doesn't apply for me. Hoping you could help me this is my code.
@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();
}
}
This is my store function:
@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());
}
}
I tried renaming the original file but it doesn't work
Multipart object contains data about the upload. Altering its data will not do anything useful.
What you can do is to create your own file pointer, with the name you like, then save the content of your multipart upload to that file. With that, you can transfer the file contents to a file, or you can stream the file contents to the created file.