Reputation: 497
I have created an API call to download file using spring boot as follows :
@GetMapping("/files/{filename:.+}")
@ResponseBody
public ResponseEntity<Resource> getFile(@PathVariable String filename) {
Resource file = fileStorageUtils.load(null, filename);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; "
+ "filename=\"" + file.getFilename() + "\"").body(file);
}
It works perfectly when the file exists. But when the file doesn't exist then an end of file exception is thrown and the postman(I am using postman to make API requests) request is waiting for the resource for a long time. Is there any way to handle this case so that the client will get a proper response if the file doesn't exist or the requests resource is a folder.
Upvotes: 1
Views: 851
Reputation: 28
Check if file exists and if not return something like:
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
Upvotes: 1