Java web實現文件打包下載功能(轉載)

轉自:https://blog.csdn.net/onesilver/article/details/78181745

首先需要實現一個文件打包下載的輔助類

public class CustomFileUtil {

    /** 
     *  
     * @param inputFileName 
     *            輸入一個文件夾 
     * @param zipFileName 
     *            輸出一個壓縮文件夾,打包後文件名字 
     * @throws Exception 
     */  
    public static void zip(String inputFileName, String zipFileName) throws Exception {  
        zip(zipFileName, new File(inputFileName));  
    }  

    private static void zip(String zipFileName, File inputFile) throws Exception {  
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));  
        zip(out, inputFile, "");  
        out.close();  
    }  

    private static void zip(ZipOutputStream out, File f, String base) throws Exception {  
        if (f.isDirectory()) { // 判斷是否爲目錄  
            File[] fl = f.listFiles();  
            out.putNextEntry(new ZipEntry(base + "/"));  
            base = base.length() == 0 ? "" : base + "/";  
            for (int i = 0; i < fl.length; i++) {  
                zip(out, fl[i], base + fl[i].getName());  
            }  
        } else { // 壓縮目錄中的所有文件  
            out.putNextEntry(new ZipEntry(base));  
            FileInputStream in = new FileInputStream(f);  
            int b;  
            while ((b = in.read()) != -1) {  
                out.write(b);
            }  
            in.close();  
        }  
    }  

    /** 
     * 下載文件 
     *  
     * @param file 文件路徑
     * @param response 
     * @param isDelete 是否刪除生成的壓縮包
     */  
    public static void downloadFile(File file,HttpServletResponse response,boolean isDelete) {
        try {
            // 以流的形式下載文件。
            BufferedInputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("UTF-8"),"ISO-8859-1"));
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
            if(isDelete)
            {
                file.delete();        //是否將生成的服務器端文件刪除
            }
         } 
         catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void zipFile(List<File> files, ZipOutputStream outputStream) throws IOException, ServletException {
        try {
            int size = files.size();
            // 壓縮列表中的文件
            for (int i = 0; i < size; i++) {
                File file = (File) files.get(i);
                zipFile(file, outputStream);
            }
        } catch (IOException e) {
            throw e;
        }
    }
    public static void zipFile(File inputFile, ZipOutputStream outputstream) throws IOException, ServletException {
        try {
            if (inputFile.exists()) {
                if (inputFile.isFile()) {
                    FileInputStream inStream = new FileInputStream(inputFile);
                    BufferedInputStream bInStream = new BufferedInputStream(inStream);
                    ZipEntry entry = new ZipEntry(inputFile.getName());
                    outputstream.putNextEntry(entry);

                    final int MAX_BYTE = 10 * 1024 * 1024; // 最大的流爲10M
                    long streamTotal = 0; // 接受流的容量
                    int streamNum = 0; // 流需要分開的數量
                    int leaveByte = 0; // 文件剩下的字符數
                    byte[] inOutbyte; // byte數組接受文件的數據

                    streamTotal = bInStream.available(); // 通過available方法取得流的最大字符數
                    streamNum = (int) Math.floor(streamTotal / MAX_BYTE); // 取得流文件需要分開的數量
                    leaveByte = (int) streamTotal % MAX_BYTE; // 分開文件之後,剩餘的數量

                    if (streamNum > 0) {
                        for (int j = 0; j < streamNum; ++j) {
                            inOutbyte = new byte[MAX_BYTE];
                            // 讀入流,保存在byte數組
                            bInStream.read(inOutbyte, 0, MAX_BYTE);
                            outputstream.write(inOutbyte, 0, MAX_BYTE); // 寫出流
                        }
                    }
                    // 寫出剩下的流數據
                    inOutbyte = new byte[leaveByte];
                    bInStream.read(inOutbyte, 0, leaveByte);
                    outputstream.write(inOutbyte);
                    outputstream.closeEntry(); // Closes the current ZIP entry
                    // and positions the stream for
                    // writing the next entry
                    bInStream.close(); // 關閉
                    inStream.close();
                }
            } else {
                throw new ServletException("文件不存在!");
            }
        } catch (IOException e) {
            throw e;
        }
    }
}

控制器實現下載功能,其中File.separatorChar是File類的靜態字段,由於windows系統和linux系統下的路徑分割符是不一樣的,所以在此使用File.separatorChar,來默認在windows中爲\分隔符,Linux下爲/分割符

@RequestMapping(value ="/BatchDownload", method = RequestMethod.GET)
@ResponseBody
public void BatchDownload(
    @RequestParam(value = "ids", required = true) String ids,
    HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException{
    //文件路徑最後帶分隔符"/"
    String inputFileName = request.getSession().getServletContext().getRealPath("upload")+File.separatorChar+"qrcode"+File.separatorChar+ids+File.separatorChar; 
       List<File> files = new ArrayList<File>();
       File Allfile = new File(inputFileName);
       if (Allfile.exists()) {
           File[] fileArr = Allfile.listFiles();
           for (File file2 : fileArr) {
               files.add(file2);
           }
       } 
       //壓縮包名字
       String fileName =this_ResListService.selectById(ids).getName()+".zip";
       // 在服務器端創建打包下載的臨時文件
       String outFilePath = inputFileName;
       File fileZip = new File(outFilePath + fileName);
       FileOutputStream outStream = new FileOutputStream(fileZip); // 文件輸出流
       ZipOutputStream toClient = new ZipOutputStream(outStream);// 壓縮流
       CustomFileUtil.zipFile(files, toClient);
       outStream.close();
       CustomFileUtil.downloadFile(fileZip, response, true);   
}

最後需要在前臺請求打包下載,在這也把代碼貼上

/*批量下載二維碼*/
function BatchDownload()
{
    var url='/ResSeat/BatchDownload.do?ids='+resrantid;
    if(url){
       location.href = url;
    }
}

前臺請求時一定注意,一定要寫上

location.href = url;  //如果不寫這一行代碼,打包的文件只會緩存在Response裏,不會下載下來

貼代碼的原因,也是如此,作爲一個新手,我第一次實現打包下載功能時,前臺是用ajax請求的,並且沒有跳轉,來來去去修改了好幾次打包下載的後臺代碼,最後打開瀏覽器查看才發現,文件是緩存到瀏覽器了,但是沒有跳轉下載。
至此,打包下載的功能已完全實現,適用於所有文件。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章