java 把文件打包成zip文件_java批量下载,将多文件打包成zip格式下载

现在的需求的:

根据产品族、产品类型,下载该产品族、产品类型下面的pic包;

pic包是zip压缩文件;

t_product表:

f00e8862ce2378170029244e3ae0b4d9.png

这些包以blob形式存在另一张表中:

t_imagefile表:

0b068b31f64c316be2a312bdb0ea3a04.png

现在要做的是:将接入网、OLT下面的两个包downloadPIC:MA5800系列-pic.zip 和 MA5900-pic.rar一起打包成zip压缩文件下载下来;

代码:

ProductController.java:

/*** 根据产品族、产品类型下载照片包*/@RequestMapping("/downloadwBatch")public voiddownloadwBatch(HttpServletRequest request, HttpServletResponse response, String productFamily, String productType){//http://localhost:8080/MySSM/downloadwBatch?productFamily=接入网&productType=OLT

try{

productFamily= new String(productFamily.getBytes("iso-8859-1"), "utf-8");

productType= new String(productType.getBytes("iso-8859-1"), "utf-8");

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}//获取要下载的照片包名

Map params = new HashMap();

params.put("productFamily", productFamily);

params.put("productType", productType);

List packageNames =productService.getPackageNamesByFamilyAndType(params);//根据包名获取待下载的文件 文件名-字节数组的形式

Map files = new HashMap();for(String packageName : packageNames){byte[] f =productService.getPackage(packageName);if(f!=null){

files.put(packageName, f);

}

}//设置下载的压缩包名

String zipName = productFamily + "_"+ productType + ".zip";//根据文件,进行压缩,批量下载

if(files.size() > 0){

productService.downloadBatchByFile(response, files, zipName);

}

}

ProductService.java:

/*** 根据包名获取文件*/

public byte[] getPackage(String packageName){byte[] bag = null;try{

ImageFile m=productMapper.getPackage(packageName);if(m!=null){

bag=m.getPicture();

}

}catch(Exception e){

e.printStackTrace();

}returnbag;

}/*** 根据产品族、产品类型 获取待下载的包名

*@paramparams

*@return

*/

public List getPackageNamesByFamilyAndType(Mapparams) {

List packageNames =productMapper.getPackageNamesByFamilyAndType(params);returnpackageNames;

}/*** 根据文件,进行压缩,批量下载

*@paramresponse

*@paramfiles

*@throwsException*/

public void downloadBatchByFile(HttpServletResponse response, Mapfiles, String zipName){try{

response.setContentType("application/x-msdownload");

response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(zipName, "utf-8"));

ZipOutputStream zos= newZipOutputStream(response.getOutputStream());

BufferedOutputStream bos= newBufferedOutputStream(zos);for(Entryentry : files.entrySet()){

String fileName= entry.getKey(); //每个zip文件名

byte[] file = entry.getValue(); //这个zip文件的字节

BufferedInputStream bis= new BufferedInputStream(newByteArrayInputStream(file));

zos.putNextEntry(newZipEntry(fileName));int len = 0;byte[] buf = new byte[10 * 1024];while( (len=bis.read(buf, 0, buf.length)) != -1){

bos.write(buf,0, len);

}

bis.close();

bos.flush();

}

bos.close();

}catch(Exception e){

e.printStackTrace();

}

}

ProductMapper.java:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

/*** 根据包名获取文件*/

public ImageFile getPackage(String packageName) throwsException;/*** 据产品族、产品类型 获取待下载的包名*/

public List getPackageNamesByFamilyAndType(Map params);

View Code

ProductMapper.xml:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

select * from t_imagefile where packageName = #{packageName}

select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPic

where p.productFamily = #{productFamily} and p.productType = #{productType}

View Code

测试:

在浏览器中输入:http://localhost:8080/MySSM/downloadwBatch?productFamily=接入网&productType=OLT

下载结果如下:

da92f7630dd13813d59d65768c87d8aa.png

---------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值