以流的方式进行压缩文件
作者:互联网
这个工具类知道了已经要压缩的文件的路径,然后需要将这个路径下的文件进行压缩。
/** * 压缩下载照片 * * @param picUrl * @param response * @throws IOException */ public static void downloadPic(List<String> picUrl, HttpServletResponse response) throws IOException, AdminException { try { String downloadFilename = System.currentTimeMillis() + ".zip";//文件的名称 downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码 response.setContentType("application/octet-stream");// 指明response的返回对象是文件流 response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); String[] files = new String[picUrl.size()]; picUrl.toArray(files); for (int i = 0; i < files.length; i++) { String url = files[i]; zos.putNextEntry(new ZipEntry(downloadFilename + File.separator + i + ".jpg")); InputStream fis = new FileInputStream(new File(url)); byte[] buffer = new byte[1024]; int r = 0; while ((r = fis.read(buffer)) != -1) { zos.write(buffer, 0, r); } fis.close(); } zos.flush(); zos.close(); } catch (UnsupportedEncodingException e) { logger.error("不支持当前格式",e); } }
标签:String,方式,downloadFilename,以流,压缩文件,zos,picUrl,new,response 来源: https://www.cnblogs.com/qingmuchuanqi48/p/12338207.html