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.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 ?


Popular posts from this blog

codefights smooth sailing ( CommonCharacterCount)

How to remove 8443 port in URL in tomcat