Java實現檔案下載__Java

來源:互聯網
上載者:User

一、前台通過a標籤開啟介面,傳入檔案id

<a href="/cdc/announcement/downloadFile/1">下載</a>

二、後台接收id,尋找對應檔案,進行下載

    @RequestMapping(value = "downloadFile/{id}", method = RequestMethod.GET)    @PreAuthorize("hasAuthority('view')")    @ResponseBody    public void downloadFile(HttpServletRequest req, HttpServletResponse resp, @PathVariable("id") Long id) {        AnnouncementAnnex announcementAnnex = announcementAnnexService.selectById(id);        //真實檔案名稱        String name = announcementAnnex.getAnnexUrl();        String downloadName=announcementAnnex.getAnnexName();//        進行轉碼後的檔案名稱,用來下載之後的檔案名稱        PublicController.download(resp,name,downloadName);    }

其中download方法

 /**     * @param resp     * @param name         檔案真實名字     * @param downloadName 檔案下載時名字     */    public static void download(HttpServletResponse resp, String name, String downloadName) {        String fileName = null;        try {            fileName = new String(downloadName.getBytes("GBK"), "ISO-8859-1");        } catch (UnsupportedEncodingException e) {            e.printStackTrace();        }        ///home/tomcat/apache-tomcat-9.0.1/files        String realPath = "D:" + File.separator + "apache-tomcat-8.5.15" + File.separator + "files";//        String realPath=File.separator+"home"+File.separator+"tomcat"+File.separator+"apache-tomcat-9.0.1"+File.separator+"files";        String path = realPath + File.separator + name;        File file = new File(path);        resp.reset();        resp.setContentType("application/octet-stream");        resp.setCharacterEncoding("utf-8");        resp.setContentLength((int) file.length());        resp.setHeader("Content-Disposition", "attachment;filename=" + fileName);        byte[] buff = new byte[1024];        BufferedInputStream bis = null;        OutputStream os = null;        try {            os = resp.getOutputStream();            bis = new BufferedInputStream(new FileInputStream(file));            int i = 0;            while ((i = bis.read(buff)) != -1) {                os.write(buff, 0, i);                os.flush();            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                bis.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }
fileName是檔案下載之後的名字,filePath是檔案所在檔案夾地址,path是檔案地址,注意設定的響應類型和編碼方式

其中File.separator為路徑分隔字元,他能自動識別是哪個作業系統而使用不同的路徑分隔字元(windows是‘\’,linux是‘/’)。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.