I created a system that uploads several files, however I ran into the following problem: I need to create a way to download according to the id of the element in the database. I wonder if anyone has an idea that I can take advantage of to do this function. I created a method in my controller, but it did not work as I wanted.
follows the method I created:
@RequestMapping(value = "/download/{file_name}", method = RequestMethod.GET)
public ModelAndView downloadFile(@PathVariable("fileName") String fileName, HttpServletResponse response){
Path arquivo = Paths.get(fileName + ".pdf");
if(Files.exists(arquivo)){
response.setHeader(" Content-Disposition","attachment, filename=\"" + fileName + ".pdf" + "\"");
response.setContentType(" application/pdf");
try {
Files.copy(arquivo, response.getOutputStream());
response.getOutputStream().flush();
} catch (Exception e) {
e.printStackTrace();
}
}
return new ModelAndView(REQUEST_MAPPING_PAGE_PESQUISAR_ITO);
}