JAVA实现文件批量打包下载

JAVA实现文件批量打包下载

实现

1.打包工具类的实现

/**
 * @author zhouxuan
 * @since 2019/4/19
 */
public class ZipUtils {

  /**
 * @author zhouxuan
 * @since 2019/4/19
 */
public class ZipUtils {

    /**
     * 批量打包
     *
     * @param jsonString json格式字符串数据
     * @param fileSaveRootPath 项目根目录
     * @return zip文件保存绝对路径
     */
    public String createZipAndReturnPath(String jsonString, String fileSaveRootPath) {

        //生成jsonArray列表
        JSONArray jsonArray = JSONArray.fromObject(jsonString);

        try {
            //生成打包下载后的zip文件:Papers.zip
            String papersZipName = "Papers.zip";

            //zip文件保存路径
            String zipPath = fileSaveRootPath + papersZipName;

            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));

            //遍历jsonArray列表获取所有JSONObject对象
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                
                //获得文件相对路径
				String relativePath = jsonObject.getString("file_Path");
				
				//获得文件名
	            String fileName = relativePath.substring(relativePath.lastIndexOf("/")+1);
	            
         	    //获得下载文件完整路径
                String downloadPath = fileSaveRootPath + relativePath;

                //以论文标题为每个文件命名
                FileInputStream fis = new FileInputStream(downloadPath);
                out.putNextEntry(new ZipEntry(fileName));

                //写入压缩包
                int len;
                byte[] buffer = new byte[1024];
                while ((len = fis.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
                out.closeEntry();
                fis.close();
            }
            out.close();
            out.flush();
            return zipPath;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

2.zip文件包的下载

 /**
   * 批量下载
   * @param request 请求
   * @param response 返回
   */
public void batchDownloadFiles(HttpServletRequest request,HttpServletResponse response) {

	//读取前端传来json字段
	String jsonString = request.getParameter("paperInfo");
	
	//获取web项目根目录
	String fileSaveRootPath = request.getSession().getServletContext().getRealPath("/");

	//创建zip文件并返回zip文件路径
	String zipPath = new ZipUtils().createZipAndReturnPath(jsonString, fileSaveRootPath);

	try {
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/zip;charset=utf-8");
		response.setHeader("Content-Disposition", "attachment;filename=Papers.zip");
		System.out.println(response.getHeader("Content-Disposition"));

		//开始下载
		BufferedInputStream is = new BufferedInputStream(new FileInputStream(new File(zipPath)));
		BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

		byte[] buff = new byte[1024];
		int len = 0;
		while ((len = is.read(buff, 0, buff.length)) != -1) {
			out.write(buff, 0, len);
		}
		out.close();
		out.flush();
		is.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
}
  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值