spring-boot-file-upload-download-rest-api-example
spring-boot-file-upload-download-rest-api-example copied to clipboard
Question about the controller
trafficstars
Sorry if I am completely wrong here but I have recently needed to learn the Spring Boot framework for work. I have Java experience but all of my web server stuff has been in Go.
That said I was looking at this line https://github.com/callicoder/spring-boot-file-upload-download-rest-api-example/blob/f3522aaa1d7dcc55e8526f1d1439e02f8ca982f7/src/main/java/com/example/filedemo/controller/FileController.java#L32-L51
Couldn't that be overloaded like this?
@PostMapping(value = "/upload", params = {"file"})
public UploadFileResponse uploadFile(@RequestParam MultipartFile file) {
FileInstance fileInstance = fileStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(fileInstance.getId())
.toUriString();
return new UploadFileResponse(fileInstance.getFileName(), fileDownloadUri,
file.getContentType(), file.getSize());
}
@PostMapping(value = "/upload", params = {"files"})
public List<UploadFileResponse> uploadFile(@RequestParam MultipartFile[] files) {
return Arrays.asList(files)
.stream()
.map(file -> uploadFile(file))
.collect(Collectors.toList());
}
If I am understanding things correctly that should allow a single endpoint to be used and it would auto use the correct upload?