View Code? Open in Web Editor
NEW
Spring Boot File Upload / Download Rest API Example
Home Page: https://www.callicoder.com/spring-boot-file-upload-download-rest-api-example/
Java 53.24%
CSS 13.39%
HTML 12.43%
JavaScript 20.94%
spring-boot-file-upload-download-rest-api-example's Issues
do we have db schema defined?
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
@ PostMapping ("/uploadFile" )
public UploadFileResponse uploadFile (@ RequestParam ("file" ) MultipartFile file ) {
DBFile dbFile = DBFileStorageService .storeFile (file );
String fileDownloadUri = ServletUriComponentsBuilder .fromCurrentContextPath ()
.path ("/downloadFile/" )
.path (dbFile .getId ())
.toUriString ();
return new UploadFileResponse (dbFile .getFileName (), fileDownloadUri ,
file .getContentType (), file .getSize ());
}
@ PostMapping ("/uploadMultipleFiles" )
public List <UploadFileResponse > uploadMultipleFiles (@ RequestParam ("files" ) MultipartFile [] files ) {
return Arrays .asList (files )
.stream ()
.map (file -> uploadFile (file ))
.collect (Collectors .toList ());
}
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?