多文件导出为ZIP文件
来源:互联网 发布:网络剧 有毒 第一集 编辑:程序博客网 时间:2024/06/08 16:44
即上次单文件导出为zip压缩包文件,增加多文件导出到同一zip文件中,接口调用,获取图片byte字节数组后,保存为图片文件,存入zip文件中。
/** * 压缩多个文件到一个zip压缩包中 * @description : * @date 2016-11-4 * @author liucong * @param request * @param response * @throws Exception * * */ @RequestMapping("/zipMoreFiles") public void zipMoreFiles(HttpServletRequest request,HttpServletResponse response) throws Exception{ //定义根路径 String rootPath = request.getRealPath("/"); //创建文件 File file = new File(rootPath+"temp_download"); //判断文件是否存在,如果不存在,则创建此文件夹 if(!file.exists()){ file.mkdir(); } String name = "图片压缩包下载"; String fileName = name+new Date().getTime(); String zipFileName = fileName + ".zip"; File zipFile = null; String path = rootPath + "temp_download"; //获取本地文件夹中图片集合,事先准备好图片 File files = new File("F:\\images\\"); //获取该文件夹中的图片文件数组 File[] filelist = files.listFiles(); // 打成压缩包 zipFile = new File(path + "/" + zipFileName); FileOutputStream zipFos = new FileOutputStream(zipFile); ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos); if (archOut instanceof ZipArchiveOutputStream) { ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut; for(File imageFile : filelist){ ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName()); zos.putArchiveEntry(zipEntry); zos.write(FileUtils.readFileToByteArray(imageFile)); zos.closeArchiveEntry(); zos.flush(); } zos.close(); } // 输出到客户端 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/octet-stream; charset=utf-8"); response.setCharacterEncoding("UTF-8"); out.write(FileUtils.readFileToByteArray(zipFile)); out.flush(); out.close(); // 输出客户端结束后,删除压缩包 if (zipFile.exists()) { zipFile.delete(); } }
调用接口获取图片保存为ZIP文件
/** * 思路: * 如果ex_enFlag : 1---查询出口流水列表 * 0---查询入库哦流水列表 * 1、获取出口获取入口流水信息列表 * 2、如果是出口,判断路段编号是否相同,如果相同,则调用路段接口获取,如果不相同,则调用平台接口获取 * 3、如果是入口,则直接调用路段入口图像接口获取。 * * @description :出入口导出图片公用方法 * @date 2016-11-2 * @author liucong * @param request * @param response * @param sqlString : 高级查询中的sql语句 * @param cpucardsnno : 通行卡号 * @param laneserialno : 出口流水号,入口流水号 * @param ex_enFlag 代表出入口标识,ex---代表出口导出图片,en----代表入口导出图片 * @throws Exception */ @RequestMapping("/exportPicture") public void exportPicture(HttpServletRequest request,HttpServletResponse response,String sqlString,String cpucardsnno,String laneserialno,Integer condition,String ex_enFlag) throws Exception { //1、获取查询条件,查询获取list集合 int roadid = SessionUtil.getSessionUser().getDefaultroadid(); Map<String, Object> params = new HashMap<String, Object>();//this.handleFilterCondition(request); String sqlInfo = ""; try { //获取出口流水,或者入口流水号 laneserialno = URLDecoder.decode(URLDecoder.decode(laneserialno,"UTF-8"),"UTF-8"); //获取通行卡号 cpucardsnno = URLDecoder.decode(URLDecoder.decode(cpucardsnno,"UTF-8"),"UTF-8"); //sqlInfo 信息,URLDecoder 进行转码,防止中文乱码 sqlInfo = URLDecoder.decode(URLDecoder.decode(sqlString,"UTF-8"),"UTF-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } //以上代码均是获取传入的查询条件 Page page = null; if("ex".equals(ex_enFlag)){//查询出口流水信息表,获取page对象,通过page 对象,可以获取到list集合 if(condition == null){ params.put("laneexserialno", "-00000"); }else if(condition == 0){ params.put("laneexserialno",laneserialno); params.put("excpucardsnno", cpucardsnno); }else{ params.put("sqlInfo", sqlInfo); } page = roadChargeExportDataService.exportData(request, LaneexlistDAO.class.getName(), "getLaneexlistListByCondition", "countLaneexlistByCondition", params,ExportImageCount); }else{//查询入口流水信息表 if(condition == null){ params.put("laneenserialno", "-00000"); }else if(condition==1){ params.put("sqlInfo", sqlInfo); }else{ params.put("laneenserialno", laneserialno); params.put("encpucardsnno",cpucardsnno); } page = roadChargeExportDataService.exportData(request, LaneenlistDAO_RS.class.getName(), "getLaneenlistListByCondition", "countLaneenlistByCondition", params,ExportImageCount); } //2、循环遍历list集合,调用接口获取图片的byte数据,放入压缩文件中 String rootPath = request.getRealPath("/"); int count = page.getTotalCount(); if(count <= ExportImageCount){// && page.getResult().size() >0 List<Laneenlist> laneenlistList = new ArrayList<Laneenlist>(); List<Laneexlist> laneexlistList = new ArrayList<Laneexlist>(); if("ex".equals(ex_enFlag)){ if(count > 0){ laneexlistList = page.getResult(); } }else{ if(count >0 ){ laneenlistList = page.getResult(); } } try{ File file = new File(rootPath+"temp_download"); if(!file.exists()){ file.mkdir(); } String zipFileName = ""; if("ex".equals(ex_enFlag)){ zipFileName = "出口流水图片.zip"; }else{ zipFileName = "入口流水图片.zip"; } //String fileName = ex_enFlag; //String zipFileName = fileName + ".zip"; File zipFile = null; String path = rootPath + "temp_download"; /* * 用JDK自带的zipentry压缩后,压缩包中的文件名中文乱码,故放弃此种方法,使用commons-compress-1.6.jar包 * ZipEntry zipEntry = new ZipEntry(new * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1")); * zos.putNextEntry(zipEntry); ZipOutputStream zos = new * ZipOutputStream(zipFos); ZipEntry zipEntry = new ZipEntry(new * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1")); zos. * putNextEntry(zipEntry); * zos.write(FileUtils.readFileToByteArray(txtFile)); zos.flush(); * zos.close(); */ // 打成压缩包 zipFile = new File(path + "/" + zipFileName); FileOutputStream zipFos = new FileOutputStream(zipFile); ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos); if (archOut instanceof ZipArchiveOutputStream) { ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut; //循环遍历list,获取图片序列号,通过序列号调用接口获取图片。 if("ex".equals(ex_enFlag)){ for(Laneexlist laneexlist : laneexlistList){ byte[] imgData = null; String imageSerialNo = laneexlist.getImageserialno(); Long enRoadid = laneexlist.getEnroadid(); if(roadid == enRoadid){ try{ //调用接口获取byte字节数组(image对象转换而来) imgData = listimageService.getImageByExSerialNo(imageSerialNo); }catch (Exception e) { } }else{ try{ imgData = commonDealService.getPTServiceImage(request, response, imageSerialNo, enRoadid.toString()); }catch (Exception e) { } } //将byte字节数组保存为图片 File imageFile = commonDealService.save(path, ex_enFlag, imgData, imageSerialNo); commonDealService.putZipFiles(imageFile, zos); } zos.close();//关闭输出流 }else{ for(Laneenlist laneenlist : laneenlistList){ String imageSerialNo = laneenlist.getImageserialno(); byte[] imgData = null; try{ imgData = listimageService.getImageByEnSerialNo(imageSerialNo); }catch(Exception e){ } File imageFile = commonDealService.save(path, ex_enFlag, imgData, imageSerialNo); //将文件put进入 commonDealService.putZipFiles(imageFile, zos); } zos.close(); } } // 输出到客户端 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/octet-stream; charset=utf-8"); response.setCharacterEncoding("UTF-8"); out.write(FileUtils.readFileToByteArray(zipFile)); out.flush(); out.close(); // 输出客户端结束后,删除压缩包 if (zipFile.exists()) { zipFile.delete(); } } catch (Exception e) { e.printStackTrace(); } }else{ response.setCharacterEncoding("gbk"); try { PrintWriter writer = response.getWriter(); //if(page.getResult().size()>0){ writer.write("记录数大于"+ExportImageCount+",请分时间段导出!"); //}else{ //writer.write("暂无数据记录,不能生成zip压缩文件!"); //} writer.close(); } catch (IOException e) { e.printStackTrace(); } } }CommonDealService代码 /** * 将图片文件保存到本地 * @author liucong * 思路:调用路段接口进行查询图片信息,获取byte数据,保存文件。 */ public File save(String path,String ex_enFlag,byte[] data,String imageSerialno) throws Exception //保存图片到相应的路径下 { File imageFile = null; //new一个文件对象用来保存图片,默认保存当前工程根目录 if(data != null && data.length >0){ Random random = new Random(); imageFile = new File(path+File.separator+ex_enFlag+"_"+imageSerialno+".jpg"); //创建输出流 FileOutputStream outStream = new FileOutputStream(imageFile); //写入数据 outStream.write(data); //关闭输出流 outStream.close(); } return imageFile; } /** * 将文件put进入zip 文件中, * @description : * @date 2016-11-4 * @author liucong * @param imageFile 图片文件 * @param zos zip输出流 * @throws IOException * * */ public void putZipFiles(File imageFile,ZipArchiveOutputStream zos) throws IOException{ if(imageFile != null && imageFile.exists()){ //加入压缩文件中 ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName()); zos.putArchiveEntry(zipEntry); zos.write(FileUtils.readFileToByteArray(imageFile)); zos.closeArchiveEntry(); zos.flush(); imageFile.delete(); } }
2 0
- 多文件导出为ZIP文件
- 导出excel文件 导出zip文件
- 利用rubyzip导出zip文件
- 批量文件压缩为ZIP
- 压缩为zip文件命令
- 文件压缩为zip--C#
- JAVA实现多文件以ZIP压缩包导出
- php 将文件压缩为zip文件
- JAVA 将文件压缩为zip文件
- 资源导出为文件
- 导出为jar文件
- Django具体导出excel、json、zip文件
- java导出静态页面html文件zip
- ZIP多文件压缩
- 把数据量大导出 放入多个excel 然后压缩成zip文件,导出
- 导出下载多个Excel文件并打包成zip文件
- 将文件压缩为zip格式
- vbs 实现压缩文件夹为zip文件
- Android研发安全3-Service安全
- linux内核的队列实现移植
- 构造函数
- Qt下载网络文件
- 并发编程——并行级别
- 多文件导出为ZIP文件
- 数组工具对象建立
- 移动应用渗透测试的军火库清单
- 使用Python完成机器学习算法之Logistic Regression
- 在 Alpine Linux 上编译 Linux0.11 内核
- CXF
- MFC中的视图
- 责任链模式【Chain of Responsibility Pattern】
- 互联网和金融 在数据挖掘上究竟存在什么区别