1

I want to download a file which is available in the server itself

AppName\resources\attachemnts\file.extension(this can be anything) i have got the code from the net the following is the same.

@RequestMapping(value = "/fileDownload", method = RequestMethod.GET)
        public void handleFileDownload(@RequestParam("fileLocation") String fileLocation,HttpServletResponse res) {
            try {

                URL url = getClass().getResource(fileLocation);
                File f = new File(url.toURI());
                System.out.println("Loading file "+fileLocation+"("+f.getAbsolutePath()+")");
                if (f.exists()) {
                    res.setContentType("application/xls");
                    res.setContentLength(new Long(f.length()).intValue());
                    res.setHeader("Content-Disposition", "attachment; filename=Test.xls");
                    FileCopyUtils.copy(new FileInputStream(f), res.getOutputStream());
                } else {
                    System.out.println("File"+fileLocation+"("+f.getAbsolutePath()+") does not exist");
                }
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }

i have to set res.setContentType("application/xls"); dynamic since the file type is going to be different each time.

How to get file type?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Java Questions
  • 7,813
  • 41
  • 118
  • 176
  • Hi @Balu C what was the reason for deleting your post :). I was helpful to some extend also hope you are the one who down voted for my post, Great – Java Questions Jan 09 '13 at 14:27

1 Answers1

1

For determining type of file, you may use any library which checks file's magick numbers and returns its filetype. For example:

See also related questions:

Community
  • 1
  • 1
Slava Semushin
  • 14,904
  • 7
  • 53
  • 69