I have the following method
@GetMapping("/{fileName}")
public Object downloadFile(@PathVariable String fileName) {
// Load file from database
errors.clear();
DBFile dbFile;
try {
dbFile = dBFileStorageService.getFileByName(fileName);
} catch (MyFileNotFoundException ex) {
logger.info("File has not been found.");
errors.add(ex.getMessage());
return new ModelAndView("redirect:/");
}
logger.info("Delivering file");
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(dbFile.getFileType()))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + dbFile.getFileName() + "\"")
.body(new ByteArrayResource(dbFile.getData()));
}
Instead of returning Object I would like to return ResponseEntity<Resource>
if it possible to return the file or ModelAndView("redirect:/")
otherwise.
I tried:
HttpHeaders headers = new HttpHeaders();
headers.add("Location", "/member/uploadImage");
return new ResponseEntity<>(headers,HttpStatus.FOUND);
But instead of redirection I got message that the file I am trying to download is corrupted. Summing up I would like to change method signature to:
public ResponseEntiry<Resource> downloadFile(@PathVariable String fileName)