-
因为最近在工作中遇到要将多个xml文件压缩成zip包并输出到浏览器,所以特地记录下来,以备下次不时之需。废话不多说直接上代码…
-
ZipController 我这里是直接在本地模拟多个xml字符串数据的
package com.xwfu.zip.controller; import com.xwfu.zip.util.ZipUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Controller public class ZipController { @Autowired ZipUtil zipUtil; @GetMapping("/zip") public void testZip(HttpServletRequest request, HttpServletResponse response) throws IOException { List<String> files = new ArrayList<String>(); for (int i = 0; i < 3; i++) { String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<Document>\n" + " <Events>\n" + " <Event Name=\"PurchaseWareHouseIn\" MainAction=\"WareHouseIn\">\n" + " <DataField>\n" + " <Data Code=\"83406830007604149938\" CorpOrderID=\"RFWL-RE210628000174\" ActDate=\"2021-06-29\" ToCorp=\"\" Actor=\"詹念\" ToCorpID=\"\"/>\n" + " <Data Code=\"83406830007605542766\" CorpOrderID=\"RFWL-RE210628000174\" ActDate=\"2021-06-29\" ToCorp=\"\" Actor=\"詹念\" ToCorpID=\"\"/>\n" + " </DataField>\n" + " </Event>\n" + " </Events>\n" + "</Document>"; files.add(str); } zipUtil.zipFiles(files, "testZip", request, response); } }
-
ZipUtil 生成压缩文件的工具类
package com.xwfu.zip.util; import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.UUID; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @Component public class ZipUtil { public void zipFiles(List<String> srcFile, String zipFileName, HttpServletRequest request, HttpServletResponse response) throws IOException { try { List<File> srcFiles = new ArrayList<File>(); for (String str : srcFile) { String path = UUID.randomUUID().toString().replaceAll("-", "") + ".xml"; File file = new File(path); OutputStreamWriter osw = null; BufferedWriter bw = null; try { osw = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); bw = new BufferedWriter(osw); bw.write(str); bw.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bw != null) bw.close(); if (osw != null) osw.close(); } srcFiles.add(file); } // 设置文件编码及相应消息头,防止中文乱码 String userAgent = request.getHeader("user-agent").toLowerCase(); //String exportFileName = "testZip"; if (userAgent.contains("msie") || userAgent.contains("like gecko")) { // win10 ie edge 浏览器 和其他系统的ie zipFileName = URLEncoder.encode(zipFileName, "UTF-8"); } else { zipFileName = new String(zipFileName.getBytes("UTF-8"), "ISO-8859-1"); } response.setCharacterEncoding("UTF-8"); response.setContentType("multipart/form-data"); response.setHeader("content-disposition", "attachment;filename=" + zipFileName + ".zip"); //获取系统文件 /*List<File> srcFiles = new LinkedList<File>(); File fileGc = new File("D:\\GoogleDownloads\\2.txt"); File fileJt = new File("D:\\GoogleDownloads\\1.txt"); srcFiles.add(fileGc); srcFiles.add(fileJt);*/ ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); try { for (int i = 0; i < srcFiles.size(); i++) { File file = srcFiles.get(i); byte[] buf = new byte[1024]; out.putNextEntry(new ZipEntry(file.getName())); int len; FileInputStream input = new FileInputStream(srcFiles.get(i)); while ((len = input.read(buf)) != -1) { out.write(buf, 0, len); out.flush(); } input.close(); out.closeEntry(); } out.close(); } catch (Exception e) { throw new RuntimeException("zipFile error from ZipUtils", e); } finally { if (out != null) { try { out.close(); } catch (IOException e) { System.out.println("ZipUtil toZip close exception" + e); } } } } catch (Exception e) { System.out.println(e.getMessage()); } } }
将多个xml字符串文件 压缩成zip包输出到浏览器
最新推荐文章于 2021-08-23 10:36:35 发布