1

サーバー自体で利用可能なファイルをダウンロードしたい

AppName\resources\attachemnts\file.extension(this can be anything)私はネットからコードを入手しました。以下は同じです。

@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());
            }
        }

res.setContentType("application/xls");ファイルタイプは毎回異なるので、動的に設定する必要があります。

ファイルタイプを取得する方法は?

4

1 に答える 1

1

ファイルのタイプを判別するために、ファイルのマジック番号をチェックしてそのファイルタイプを返す任意のライブラリを使用できます。例えば:

関連する質問も参照してください。

于 2013-01-09T14:02:57.277 に答える