java后台下載方法


    public void fileDownload() throws UnsupportedEncodingException {
        String fileidd= this.getParameter("獲取ID");
        SysFile sysFile = sysFileManager.getById(根據id(fileidd)查詢);
        File file = new File(AppUtil.getAppAbsolutePath()+sysFile.getPicpath()//獲取路徑);
        if (file.exists()) {
            this.getResponse().setContentType("application/force-download");// 設置強制下載不打開
            this.getResponse().addHeader("Content-Disposition", "attachment;fileName="+ java.net.URLEncoder.encode(sysFile.getFilename()//獲取文件名, "UTF-8"));// 設置文件名

            byte[] buffer = new byte[1024];
            FileInputStream fis = null;
            BufferedInputStream bis = null;
            try {
                fis = new FileInputStream(file);
                bis = new BufferedInputStream(fis);
                OutputStream os = this.getResponse().getOutputStream();
                int i = bis.read(buffer);
                while (i != -1) {
                    os.write(buffer, 0, i);
                    i = bis.read(buffer);
                }
                System.out.println("success");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM