前端調用接口,使用java 生成zip文件並完成瀏覽器導出(下載)

CreateZipAndDownload .java

package com.wei.utils;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class CreateZipAndDownload {
	public byte[] createZip(String srcSource) throws Exception{
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);
        //將目標文件打包成zip導出
        File file = new File(srcSource); 
        a(zip,file,"");
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }
	public void a(ZipOutputStream zip, File file, String dir) throws Exception {
        //如果當前的是文件夾,則進行進一步處理
        if (file.isDirectory()) {
            //得到文件列表信息
            File[] files = file.listFiles();
            //將文件夾添加到下一級打包目錄
            zip.putNextEntry(new ZipEntry(dir + "/"));
            dir = dir.length() == 0 ? "" : dir + "/";
            //循環將文件夾中的文件打包
            for (int i = 0; i < files.length; i++) {
                a(zip, files[i], dir + files[i].getName());         //遞歸處理
            }
        } else {   //當前的是文件,打包處理
            //文件輸入流
           BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
           ZipEntry entry = new ZipEntry(dir);
           zip.putNextEntry(entry);
           zip.write(FileUtils.readFileToByteArray(file));
           IOUtils.closeQuietly(bis);
           zip.flush();
           zip.closeEntry();
        }
}
	public void CreateZipAndDownload(HttpServletRequest request,
			HttpServletResponse response) {
		String downloadName = "files.zip";
//      downloadName = BrowserCharCodeUtils.browserCharCodeFun(request, downloadName);//下載文件名亂碼問題解決
      
      //將文件進行打包下載
      try {
          OutputStream out = response.getOutputStream();
  		String ZIP_DIRECTORY = "files"; // 文件存儲相對路徑
  		// 這個路徑相對當前應用的目錄
  		String realPath = request.getServletContext().getRealPath("/");
  		String ZIPPath = realPath +  ZIP_DIRECTORY; // 本地磁盤目錄
//          byte[] data = createZip("/fileStorage/download");//服務器存儲地址
          byte[] data = createZip(ZIPPath);//服務器存儲地址
          response.reset();
          response.setHeader("Content-Disposition","attachment;fileName="+downloadName);
          response.addHeader("Content-Length", ""+data.length);
          response.setContentType("application/octet-stream;charset=UTF-8");
          IOUtils.write(data, out);
          out.flush();
          out.close();
      } catch (Exception e) {
          e.printStackTrace();
      }
		
	}
}

調用

	// 3.將files文件目錄下的所有文件打包成爲.zip
	CreateZipAndDownload zipDown = new CreateZipAndDownload();
	zipDown.CreateZipAndDownload(request,response);

參考

https://www.cnblogs.com/mr-wuxiansheng/p/8166846.html

https://www.cnblogs.com/xu308240814/p/10765508.html

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