当前位置:首页 > 编程笔记 > 正文
已解决

【java打包下载zip树形结构】打包的时候在zip里创建文件夹自定义路径

来自网友在路上 162862提问 提问时间:2023-09-27 04:52:19阅读次数: 62

最佳答案 问答题库628位专家为你答疑解惑

在这里插入图片描述

1、测试类

import lombok.Data;@Data
public class ZipVo {//文件路径:开始和结束都要/斜杠,生成属性文件夹private String pathName;//文件数据private String data;private String type;//后缀suffixprivate String suffix;
}

2、控制器

import com.ekkcole.utils.Func;
import org.apache.logging.log4j.util.Base64Util;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** 树形层级下载打包下载zip*/
@RestController
public class TestZip {/*** 树形层级下载打包下载zip* 参数自定义*/@GetMapping("/generateFileGroupDownZip")public void generateFileGroupDownZip(HttpServletResponse response) {//TODO 设置测试数据List<ZipVo> zipVoList = new ArrayList<>();ZipVo bean = new ZipVo();bean.setData("http://localhost:8999/machine/upload/image/20230922/29909950475117.docx");
//        bean.setData("C:\\Users\\Administrator\\Desktop\\zhuomian\\测试图\\1.jpg");bean.setPathName("/常州/武进/");bean.setType(".jpg");zipVoList.add(bean);ZipVo bean1 = new ZipVo();bean1.setData("http://localhost:8999/machine/upload/image/20230922/30035456012377.zip");
//        bean1.setData("C:\\Users\\Administrator\\Desktop\\zhuomian\\测试图\\1.jpg");bean1.setPathName("/常州/武进/湖塘/");bean1.setType(".jpg");zipVoList.add(bean1);ZipVo bean2 = new ZipVo();bean2.setData("http://localhost:8999/machine/upload/image/20230922/30895400564085.docx");
//        bean2.setData("C:\\Users\\Administrator\\Desktop\\zhuomian\\测试图\\1.jpg");bean2.setPathName("/徐州/睢宁/庆安/");bean2.setType(".jpg");zipVoList.add(bean2);//设置最终输出zip文件名,后缀记得加String zipFileName = "测试" + ".zip";// 定义zip输出流ZipOutputStream zipStream = null;BufferedInputStream bufferStream = null;File zipFile = new File(zipFileName);try {//构造最终压缩包的输出流zipStream = new ZipOutputStream(new FileOutputStream(zipFile));// 通过lambda表达式分组Map<String, List<ZipVo>> collect = zipVoList.stream().collect(Collectors.groupingBy(ZipVo::getPathName));// 遍历分组for (Map.Entry<String, List<ZipVo>> zipMap : collect.entrySet()) {// 获取分组键String key = zipMap.getKey();// 获取分组值List<ZipVo> zipMapValueList = zipMap.getValue();// 如果分组值不为空则遍历数组if (zipMapValueList.size() > 0) {// 遍历分组值列表for (ZipVo zipVo : zipMapValueList) {// 获取文件的数据String attachUrl = zipVo.getData();// 获取后缀String suffix = zipVo.getType();//TODO 以下方式来自上面测类data里面的数据,根据自己填写的类型使用对应的//方式一:--------------------根据base64获取文件InputStream流---------------
//                        BASE64Decoder decoder = new BASE64Decoder();// Base64解码,对字节数组字符串进行Base64解码并生成文件
//                        byte[] byt = decoder.decodeBuffer(attachUrl);
//                        for (int a = 0, len = byt.length; a < len; ++a) {
//                            // 调整异常数据
//                            if (byt[a] < 0) {
//                                byt[a] += 256;
//                            }
//                        }
//                        InputStream input = new ByteArrayInputStream(byt);//方式二:------------------------根据url获取------------------InputStream input = getInputStream(attachUrl);//方式三:-----------------------根据指定路径获取文件----------------------
//                        File file = new File(attachUrl);
//                        InputStream input=new FileInputStream(file);/*** 文件和文件夹命名* 文件:名称 + 后缀  * 文件夹:"\\" + 文件名 + "\\"* 组合使用: 文件夹 + 文件*/ZipEntry zipEntry = new ZipEntry("\\" + key + "\\" + System.currentTimeMillis() + suffix);zipStream.putNextEntry(zipEntry);bufferStream = new BufferedInputStream(input, 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.flush();zipStream.close();}} catch (IOException e) {e.printStackTrace();}}//判断系统压缩文件是否存在:true-把该压缩文件通过流输出给客户端后删除该压缩文件  false-未处理if (zipFile.exists()) {// 调用下载方法groupDownZip(response, zipFileName);zipFile.delete();}}/*** @param response http相应* @param filename 文件名*/private void groupDownZip(HttpServletResponse response, String filename) {if (filename != null) {FileInputStream inputStream = null;BufferedInputStream bufferedInputStream = null;OutputStream outputStream = null;try {File file = new File(filename);if (file.exists()) {//设置Headersresponse.setHeader("Content-Type", "application/octet-stream");//设置下载的文件的名称-该方式已解决中文乱码问题response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "ISO-8859-1"));inputStream = new FileInputStream(file);bufferedInputStream = new BufferedInputStream(inputStream);outputStream = response.getOutputStream();byte[] buffer = new byte[1024];int lenth = 0;while ((lenth = bufferedInputStream.read(buffer)) != -1) {outputStream.write(buffer, 0, lenth);}} else {String error = Base64Util.encode("文件内容为空");}} catch (IOException ex) {ex.printStackTrace();} finally {try {if (inputStream != null) {inputStream.close();}if (bufferedInputStream != null) {bufferedInputStream.close();}if (outputStream != null) {outputStream.flush();outputStream.close();}} catch (IOException e) {e.printStackTrace();}}}}private InputStream getInputStream(String attachUrl) {URL url = null;InputStream is = null;ByteArrayOutputStream outStream = null;HttpURLConnection httpUrl = null;try {if (Func.isNotEmpty(attachUrl)) {//处理中文名String[] args = attachUrl.split("/");String name1 = args[args.length - 1];String name2 = java.net.URLEncoder.encode(name1, "utf-8");String newUrl = attachUrl.replace(name1, name2);url = new URL(newUrl);httpUrl = (HttpURLConnection) url.openConnection();// 设置超时时间,避免连接超时影响接口速度httpUrl.setConnectTimeout(900);httpUrl.setReadTimeout(900);httpUrl.connect();is = httpUrl.getInputStream();return is;}} catch (Exception e) {}return null;}
}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"【java打包下载zip树形结构】打包的时候在zip里创建文件夹自定义路径":http://eshow365.cn/6-14409-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!