Reputation: 73
I'm trying to change the name of MultipartFile.
I'm using MultipartFile on my controller to call rest service:
@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file)
{
...
}
Have you please any idea about changing the OriginalFilename of the uploaded file ?.
Big thanks.
Upvotes: 4
Views: 13640
Reputation: 1
if you "forward" your file to another server, you can try the following code:
import jakarta.validation.constraints.NotNull;
import org.springframework.core.io.Resource;
import org.springframework.lang.Nullable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
public class MultipartFileResourceNameDecorator implements Resource {
private final Resource resource;
private final String newFileName;
public MultipartFileResourceNameDecorator(@NotNull Resource resource, @NotNull String newFileName) {
this.resource = resource;
this.newFileName = newFileName;
}
@Override
@Nullable
public String getFilename() {
return newFileName;
}
// delegate another methods
}
Caller:
private String sendByFile(Resource fileResource, String newFileName) {
var resource = new MultipartFileResourceNameDecorator(fileResource, newFileName);
log.info("Upload file: {}", resource.getFilename());
try {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
// Create the request body with the file
var body = new LinkedMultiValueMap<String, Object>();
body.add("file", resource);
// Create the HttpEntity
var entity = new HttpEntity<>(body, headers);
// Make the POST request
var response = restTemplate.postForEntity(urlUpload, entity, String.class);
// Your logic here with response
} catch (Exception e) {
log.error("Upload fail", e);
return null;
}
}
Upvotes: 0
Reputation: 13777
You can achieve renaming of a file as below. To verify go the uploadDir and you will have a file with "renameTest"
.
You can append clientId + uploadTime
to a filename to avoid the same filenames in database
@PostMapping(value = "/post")
public String renameMultipartFile(@RequestParam("file") MultipartFile file) {
String uploadDir = "yourPath";
String filename = "renameTest";
Path saveTO = Paths.get(uploadDir + filename);
try {
try {
Files.copy(file.getInputStream(), saveTO);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error : " + e.getMessage());
}
return "File uploaded successfully";
} catch (Exception e) {
e.printStackTrace();
return "Error : " + e.getMessage();
}
}
Upvotes: 0
Reputation: 1894
You can try the following code.
@PostMapping("/post")
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file)
{
try {
String filename = "random_filename.pdf"; // Give a random filename here.
byte[] bytes = file.getBytes();
String insPath = <DIRECTORY PATH> + filename // Directory path where you want to save ;
Files.write(Paths.get(insPath), bytes);
return ResponseEntity.ok(filename);
}
catch (IOException e) {
// Handle exception here
}
}
You have to remember to add a random string to the file name. If you just hard code the file name, every time you upload a file, the previous file will be replaced.
Upvotes: 2