java多文件以压缩文件夹的方式导出

java多文件以压缩文件夹的方式导出

多文件导出的时候,不是一个个导出。把多个文件放到一个压缩文件夹里面,直接导出压缩文件夹

一、前端多选文件

function DownAll() {
	//hdIdValue 为多文件的路径,以逗号分开
	var hdIdValue = $("#hdIdValue").val();
	if(hdIdValue=="")
		alert("请选择数据!");
	else
		location.href = "downloadFiles.do?recordpath="+window.encodeURI(hdIdValue);
}

二、downloadFiles.do后端代码

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		req.setCharacterEncoding("UTF-8");
		String recordpathss=req.getParameter("recordpath");//传过来的路径们,以逗号分开
		String[] recordpaths=recordpathss.split(",");
		try {
			String downloadFilename = URLEncoder.encode("录音文件_"+String.valueOf(System.currentTimeMillis())+".zip", "UTF-8");
			ZipOutputStream zos = new ZipOutputStream(resp.getOutputStream());
			
			FileInputStream ips = null;
            File file = new File("");
            byte[] buffer = new byte[1024];
            int r = 0;
            resp.setContentType("application/octet-stream");
			resp.setHeader("Content-Disposition", "attachment;filename=\""+downloadFilename+"\"");
			for(int i=0;i<recordpaths.length;i++) {
				String filename = recordpaths[i].split("/")[recordpaths[i].split("/").length-1];
				zos.putNextEntry(new ZipEntry(filename));
				file = new File (recordpaths[i]);
				if(file.exists()){
					ips = new FileInputStream(file);
					resp.setContentType("multipart/form-data");
                    while ((r = ips.read(buffer)) != -1) {
                        zos.write(buffer, 0, r);
                    }
                    if (null != ips) {
                        ips.close();
                    }
				}else{
					logger.info(filename+"@@@找不到文件");
				}
			}
			zos.flush();
            zos.close();
		} catch (Exception e) {
            e.printStackTrace();
        }
	}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值