java下载边下载边打包,在加载中增加文件夹

一、预览
1、文件在服务器上全在一个文件夹下
2、下载的时候是这种结构
aaa.zip 中包含文件夹 b、c、d
将文件下载到a、b、c中
二、代码

public void downloadReportFiles(HttpServletResponse response,String tids){
        try{
            response.setContentType("application/x-msdownload");
            response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode("ReportSource.zip", "utf-8"));
     
            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
            BufferedOutputStream bos = new BufferedOutputStream(zos);
            //查询出ftp的配置
    		TimeFTPconfig timeFTPconfig = sendReportDao.getFtpConfig();
    		
            //取出需要下载的会议
            String[] mettingids = tids.split(",");
            //遍历出需要下载得文件
            for(int i = 0; i < mettingids.length; i++){
            	//获取文件名称
            	TimeReportDo timeMeetingReportDo = getReport(Integer.valueOf(mettingids[i]));
            	// 获取到一个会议下的附件
            	TimeReportDo timeReportDo = getFile(Integer.valueOf(mettingids[i]));
            	//添加一个文件夹 文件名称位会议主题
            	String directoryname = timeMeetingReportDo.getMeetingtitle()+"_"+i;
            	zos.putNextEntry(new ZipEntry(directoryname+"/"));
            	//在此文件夹下增添具体的录音
            	if(!(timeReportDo.getRecord()==null)){
            		InputStream bis = FtpClientUtil.load(timeReportDo.getRecord(), timeFTPconfig);
            		// 如果服务器上不存在此文件,则跳过(理论上无此可能)
            		if(bis == null){
            			continue;
            		}
                    zos.putNextEntry(new ZipEntry(directoryname+"/"+timeReportDo.getRecordold()));
                    IOUtils.copy(bis, bos);
                    bis.close();
                    bos.flush();
            	}
            	//在此文件下增添具体的报告
            	if(!(timeReportDo.getReport()==null)) {
            		InputStream bis = FtpClientUtil.load(timeReportDo.getReport(), timeFTPconfig);
            		// 如果服务器上不存在此文件,则跳过(理论上无此可能)
            		if(bis == null){
            			continue;
            		}
                    zos.putNextEntry(new ZipEntry(directoryname+"/"+timeReportDo.getReportold()));
                    IOUtils.copy(bis, bos);
                    bis.close();
                    bos.flush();
            	}
            	//在此文件下增添具体的速记
            	if(!(timeReportDo.getShorthand()==null)) {
            		InputStream bis = FtpClientUtil.load(timeReportDo.getShorthand(), timeFTPconfig);
            		// 如果服务器上不存在此文件,则跳过(理论上无此可能)
            		if(bis == null){
            			continue;
            		}
            		zos.putNextEntry(new ZipEntry(directoryname+"/"+timeReportDo.getShorthandold()));
            		IOUtils.copy(bis, bos);
            		bis.close();
            		bos.flush();
            	}
            }
            bos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }

三、知识补充
思路:从服务器上取读文件传入到输入流里面,创建zip输出流,

zos.putNextEntry(new ZipEntry(directoryname+"/"));

增加一个文件夹需要加“/”,后续写入此文件夹中得时候,只需要带着“directoryname/”就可以写入到里面了

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值