One way is to have a controller, that takes the file name as an argument. And then this controller returns the file (and not a rendered jsp like the most other ones).
@RequestMapping(value = "/download", method = RequestMethod.GET)
public ResponseEntity<byte[]> thumbnail(@RequestParam(value = "name") String name ) {
File file = new File(BASEPATH + "/" + name);
if (file.exists)) {
byte[] content = org.apache.commons.io.FileUtils.readFileToByteArray(file);
HttpHeaders headers = new HttpHeaders();
headers.setContentType("yourcontentType");
headers.setContentLength(thumbnailContent.length);
return new ResponseEntity<byte[]>(content, headers, HttpStatus.OK);
} else {
return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
}
}
Attention: this simple example is a strong security issue, a user could use this to download every file from the server where the tomcat has read access. Is is not restricted to the BASEPATH. (The attacker could send for example: name = "....\differentDir\secret.txt") -- One way to handle is is to use a whitelist of alloed chars (only A..Z, a..z, 0..9)