When you want to deliver a file to a user with SpringMVC you can use the built in FileCopyUtils to put the file into the OutputStream of the response.
This is an example of the method in your controller class.
@RequestMapping(value = "/yourURLHere") public void handleFileDownload(HttpServletResponse response) { File file = myFileService.getFile(); response.setContentType("application/xls"); //in my example this was an xls file response.setContentLength(new Long(file.length()).intValue()); response.setHeader("Content-Disposition","attachment; filename=MyFile.csv"); try { FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream()); } catch (IOException e) { e.printStackTrace(); } return; }
RSS feed for comments on this post · TrackBack URI
Leave a reply