java实现批量下载 多文件打包成zip格式下载

发布时间:2021-04-15 10:56:20 作者:小新
来源:亿速云 阅读:336

这篇文章给大家分享的是有关java实现批量下载 多文件打包成zip格式下载的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体内容如下

现在的需求的:

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

pic包是zip压缩文件;

t_product表:

java实现批量下载 多文件打包成zip格式下载

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

t_imagefile表:

java实现批量下载 多文件打包成zip格式下载

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

代码:

ProductController.java:

 /**
 * 根据产品族、产品类型下载照片包
 */
 @RequestMapping("/downloadwBatch")
 public void downloadwBatch(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<String, String> params = new HashMap<String, String>();
 params.put("productFamily", productFamily);
 params.put("productType", productType);
 List<String> packageNames = productService.getPackageNamesByFamilyAndType(params);
 
 //根据包名获取待下载的文件 文件名-字节数组的形式
 Map<String, byte[]> files = new HashMap<String, byte[]>();
 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();
 }
 return bag;
 }
 
 
 /**
 * 根据产品族、产品类型 获取待下载的包名
 * @param params
 * @return
 */
 public List<String> getPackageNamesByFamilyAndType(Map<String, String> params) {
 List<String> packageNames = productMapper.getPackageNamesByFamilyAndType(params);
 
 return packageNames;
 }
 
 /**
 * 根据文件,进行压缩,批量下载
 * @param response
 * @param files
 * @throws Exception 
 */
 public void downloadBatchByFile(HttpServletResponse response, Map<String, byte[]> files, String zipName){
 try{
 response.setContentType("application/x-msdownload");
 response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(zipName, "utf-8"));
 
 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
 BufferedOutputStream bos = new BufferedOutputStream(zos);
 
 for(Entry<String, byte[]> entry : files.entrySet()){
 String fileName = entry.getKey(); //每个zip文件名
 byte[] file = entry.getValue(); //这个zip文件的字节
 
 BufferedInputStream bis = new BufferedInputStream(new ByteArrayInputStream(file));
 zos.putNextEntry(new ZipEntry(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:

/**
 * 根据包名获取文件
 */
public ImageFile getPackage(String packageName) throws Exception;
 
/**
 * 据产品族、产品类型 获取待下载的包名
 */
public List<String> getPackageNamesByFamilyAndType(Map<String, String> params);

ProductMapper.xml:

<!-- 根据包名获取文件 -->
 <select id="getPackage" parameterType="java.lang.String" resultType="com.cy.model.ImageFile">
 select * from t_imagefile where packageName = #{packageName}
 </select>
 
 <!-- 跟据产品族、产品类型 获取待下载的包名 -->
 <select id="getPackageNamesByFamilyAndType" parameterType="java.util.Map" resultType="java.lang.String">
 select packageName from t_imagefile m join t_product p on m.packageName = p.downloadPic
 where p.productFamily = #{productFamily} and p.productType = #{productType}
 </select>

测试:

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

下载结果如下:

java实现批量下载 多文件打包成zip格式下载

简单的demo

package com.msznyl;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class Download {
 public static void main(String[] args) {
 //需要压缩的文件--包括文件地址和文件名
 String [] path ={"E:\\360DocProtect\\01.txt","E:\\360DocProtect\\02.docx"};
 // 要生成的压缩文件地址和文件名称
 String desPath = "D:\\DOWNLOADS\\new.zip";
 File zipFile = new File(desPath);
 ZipOutputStream zipStream = null;
 FileInputStream zipSource = null;
 BufferedInputStream bufferStream = null;
 try {
 //构造最终压缩包的输出流
 zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
 for(int i =0;i<path.length;i++){
 File file = new File(path[i]);
 //将需要压缩的文件格式化为输入流
 zipSource = new FileInputStream(file);
 //压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
 ZipEntry zipEntry = new ZipEntry(file.getName());
 //定位该压缩条目位置,开始写入文件到压缩包中
 zipStream.putNextEntry(zipEntry);
 //输入缓冲流
 bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
 int read = 0;
 //创建读写缓冲区
 byte[] buf = new byte[1024 * 10];
 while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
 {
 zipStream.write(buf, 0, read);
 }
 }
 
 } catch (Exception e) {
 e.printStackTrace();
 } finally {
 //关闭流
 try {
  if(null != bufferStream) bufferStream.close();
  if(null != zipStream) zipStream.close();
  if(null != zipSource) zipSource.close();
 } catch (IOException e) {
  e.printStackTrace();
 }
 }
 }
}

感谢各位的阅读!关于“java实现批量下载 多文件打包成zip格式下载”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. Node如何实现批量下载文件到本地
  2. 使用Vue如何批量下载文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java如何实现学生信息管理系统

下一篇:java如何实现多线程卖票功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》