Friday, May 3, 2013

Spring File Download View

File DownLoad View
I wanted to make dailog and download some file when i request special url.
I searched on internet and finally i get to solution that is very easy to use
This soultion is a kind of many way, Just keep in mind~
@RequestMapping(value = "/imageDown", method = RequestMethod.GET)
 public void handleFileDownload(HttpServletResponse res) throws FileNotFoundException, IOException {

  final String fn = "D://test.jpg";

  final File f = new File(fn);
  logger.debug("Loading file " + fn + "(" + f.getAbsolutePath() + ")");
  if (f.exists()) {
   res.setContentType("application/octet-stream");
   res.setContentLength(new Long(f.length()).intValue());
   res.setHeader("Content-Disposition", "attachment; filename=Test.jpg");
   FileCopyUtils.copy(new FileInputStream(f), res.getOutputStream());
  } else {
   logger.debug("File" + fn + "(" + f.getAbsolutePath() + ") does not exist");
  }
 }


Awesome ?

No comments:

Post a Comment