Return a file from Controller in Spring
May be this will help
@RequestMapping(method = RequestMethod.GET,value = "/getfile")
public @ResponseBody
void getReviewedFile(HttpServletRequest request, HttpServletResponse response, @RequestParam("fileName") String fileName)
{
//do other stuff
byte[] file = //get your file from the location and convert it to bytes
response.reset();
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setContentType("image/png"); //or whatever file type you want to send.
try {
response.getOutputStream().write(image);
} catch (IOException e) {
// Do something
}
}
Thanks to @JAR.JAR.beans. Here is the link: Downloading a file from spring controllers
@RequestMapping(value = "/files/{file_name}", method = RequestMethod.GET)
@ResponseBody
public FileSystemResource getFile(@PathVariable("file_name") String fileName) {
return new FileSystemResource(myService.getFileFor(fileName));
}