java实现附件打包并下载

打包zip工具类

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {
    /**
     * 批量打包
     *
     * @param fileSaveRootPath 项目根目录
     * @return zip文件保存绝对路径
     */
    public String createZipAndReturnPath(List<Map<String,Object>> list,String filename, String fileSaveRootPath) {
        try {
            //生成打包下载后的zip文件:文件名.zip
            String papersZipName = filename+".zip";
            //zip文件保存路径
            String zipPath = fileSaveRootPath + papersZipName;
            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipPath));
            //遍历jsonArray列表获取所有JSONObject对象
            for (int i = 0; i < list.size(); i++) {
                Map<String,Object> map=list.get(i);
                //获得文件相对路径
                String relativePath = map.get("filepath").toString();
                //获得文件名
                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;
    }


    /**
     * 批量下载
     * @param request 请求
     * @param response 返回
     */
    public void batchDownloadFiles(List<Map<String,Object>> list,String filename,HttpServletRequest request, HttpServletResponse response) {
        //获取web项目根目录
        String fileSaveRootPath = request.getSession().getServletContext().getRealPath("/");
        //创建zip文件并返回zip文件路径
        String zipPath = new ZipUtils().createZipAndReturnPath(list, filename,fileSaveRootPath);
        try {
            response.reset();
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/zip;charset=utf-8");
            response.setHeader("Content-Disposition", "attachment;filename="+filename+".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();
        }
    }
}

controller层

//附件批量下载并打包
@RequestMapping(value = "fileBatchDownload", method = RequestMethod.GET)
public void fileBatchDownload(HttpServletRequest request,HttpServletResponse response){
    String id=request.getParameter("id");  //前端传的主表id
    String filename=request.getParameter("filename"); //压缩包名称
    List<Map<String,Object>> list=busAnnexMService.fileBatchDownload(id);    //查询id下的所有附件信息
    new ZipUtils().batchDownloadFiles(list,filename,request,response);  //调用工具类打包并下载
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值