java下载导出多个文件,修改文件名,并且压缩成zip格式

java下载导出多个文件,修改文件名,并且压缩成zip格式


下载多张图片,或者其他文件,并且修改文件名,且压缩成zip格式

1.contorller 代码片.

/**
* 实现功能描述:导出 
* 传入id
* @param ProjectInstanceId 
* @return net.ourway.cf3.CallResult
*/
@RequestMapping(value = "/toPendingTopicZip", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
public void toPendingTopicZip(@Param("ProjectInstanceId") String ProjectInstanceId,HttpServletRequest request,HttpServletResponse response) throws Exception {
   _IProjectPendingService.toPendingTopicZip(ProjectInstanceId,request,response);
}

ZipUtils 工具类 代码片.

package net.ourway.common.utils;

import java.io.*;
import java.net.URLEncoder;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.servlet.http.HttpServletResponse;

public class ZipUtils {
    /**
     * 导出
     * @param response
     * @param files 要进行压缩的文件数组
     * @param fileNames 压缩的文件要修改成别的名子数组
     * @param zipName 压缩包名称
     * @return zip文件
     *
     */
    public static void createZipFile(HttpServletResponse response, File [] files,List<String> fileNames,String zipName)  {
       // 输出响应
        ZipOutputStream zipStream = null;
        response.setContentType("application/x-zip-compressed");
        response.setHeader("Cache-Control", "max-age=0");
        try {
        response.setHeader("Content-Disposition ", "attachment; filename=" +URLEncoder.encode(zipName+".zip", "UTF-8") );
            //1:定义为输出流
            OutputStream os = response.getOutputStream();
            //2.把输出流给压缩流
            zipStream = new ZipOutputStream(os);
            //3.把file文件,和要修改的名称 循环写入到 FileInputStream 输入流里面
            for (int i=0;i<  files.length;i++){
                File file=files[i];
                compressZip(file, zipStream, fileNames.get(i));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if (zipStream != null) {
                    zipStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
   
    private static void compressZip(File file, ZipOutputStream zipStream,String alias) throws Exception{
        FileInputStream input = null;
        try {
            //3.1.把file文件 写入到 FileInputStream 输入流里面
            input = new FileInputStream(file);
            //3.2.把 FileInputStream 输入流 写入到 byte[] 字节中
            zip(input, zipStream, alias+"."+file.getName().substring(file.getName().lastIndexOf(".")+1));
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(input != null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void zip(InputStream input, ZipOutputStream zipStream, String zipEntryName) throws Exception{
        //byte[] 字节
        byte[] bytes = null;
        BufferedInputStream bufferStream = null;
        try {
            if(input == null) {
                throw new Exception("获取压缩的数据项失败! 数据项名为:" + zipEntryName);
            }
            // 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
            ZipEntry zipEntry = new ZipEntry(zipEntryName);
            // 定位到该压缩条目位置,开始写入文件到压缩包中
            zipStream.putNextEntry(zipEntry);
            bytes = new byte[1024 * 5];// 读写缓冲区
            bufferStream = new BufferedInputStream(input);// 输入缓冲流
            int read = 0;
            while ((read = bufferStream.read(bytes)) != -1) {
            	//压缩输出流写入bytes[] 数据
                zipStream.write(bytes, 0, read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != bufferStream)
                    bufferStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

imp实现类 代码片.


  @Override
    public void toPendingTopicZip(String PendingId, HttpServletRequest request, HttpServletResponse response) {
    	//查询数据
        List<Map<String, Object>> fileList = _IcsProjectPendingTopicMapper.getTocList(PendingId);
        //导出时,你自己定义的文件名称,写入list里面
        List<Map<String, Object>> fileNameList = ......;


        File[] tempList =new File[fileList.size()] ;
        for (int i=0;i<fileList.size();i++){
        	//获取文件路径: fileList.get(i).get("fullFilename").toString() 
        	//(假设文件路径是全路径:c:\**.jpg)
        	//把文件添加到 File文件数组中 
            tempList[i]=new File(fileList.get(i).get("fullFilename").toString());
        }

        //加密打包文件开始
        ZipUtils.createZipFile(response, tempList,fileNameList,"Zip压缩包名称");

    }


前端导出按钮 代码片.

     <el-button
              v-access:create
              type="primary"
              size="small"
              @click="exportTopicZip">导出附件</el-button>

前端请求方法代码片.

    exportTopicZip() {
        window.location.href="/api/ics/projectPending/toPendingTopicZip?ProjectInstanceId=" + this.ProjectInstanceId;
      },
  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值