controller类
@GetMapping("zip") @ApiOperation(value = "压缩包", notes = "适用web端") public Result uploadZip(HttpServletResponse response) throws Exception { response.setContentType("application/octet-stream"); //下载文件的默认名称 response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode("二维码", "UTF-8") + ".zip"); //编码 response.setCharacterEncoding("UTF-8"); //zip输出流-网页响应输出流 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); //这里获取base64集合 List<PhoneInfo> list = getPhoneInfos(); Integer index = 0; for (PhoneInfo phoneInfo : list) { index++; if (phoneInfo.getCodeImg() != null) { InputStream in = TestZip.getIn(phoneInfo.getCodeImg().substring(22)); zos.putNextEntry(new ZipEntry(index + ".jpg")); zos.setComment("www.mldnjava.cn"); int temp = 0; while ((temp = in.read()) != -1) { zos.write(temp); } in.close(); } } if (null != zos) { zos.flush(); zos.close(); return null; } return Result.successResult(200, "success"); }TestZip工具类
package com.eastyx.authentication.utils; import java.io.*; import java.util.List; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import sun.misc.BASE64Decoder; /** * @user: xiaobao * @date: 2019/4/11 * @description 压缩zip工具类 */ public class TestZip { public static InputStream getIn(String base64)throws Exception{ BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes = decoder.decodeBuffer(base64); for (int i =0;i < bytes.length;++i){ if (bytes[i] < 0) bytes[i] += 256; } ByteArrayInputStream in = new ByteArrayInputStream(bytes); return in; } }