//导出二维码 @Transactional public void generateTwoDimensionalCode(HttpServletResponse response,List<String> empCodes) { if(empCodes==null||empCodes.size()<1){ throw new BizException("没有要导出的人员二维码"); } synchronized(this) { String today = DateTimeUtil.parseDateToString(new Date(), DateTimeUtil.SIMPLE_YMD); //以日期分文件夹 String physicalPath = today; //保存在本地的地址 File file = new File(physicalPath); file.mkdir(); FileInputStream fileInput = null; OutputStream output = null; File zipFile = null; try { for (String empCode : empCodes) { ByteArrayInputStream in = new ByteArrayInputStream(QrCodeUtil.createQrCode(activeUrl + "?empCode=" + empCode.getEmpCode())); //将b作为输入流; BufferedImage image = ImageIO.read(in); //将in作为输入流,读取图片存入image中,而这里in可以为ByteArrayInputStream(); String targerPath = "./" + physicalPath + "/" + empCode.getEmpCardCode() + ".jpg"; OutputStream os = new FileOutputStream(targerPath); ImageIO.write(image, "jpg", os); } fileToZip(today, today, today); String fileName = today + ".zip"; zipFile = new File(fileName); fileInput = new FileInputStream(zipFile); int i = fileInput.available(); byte[] content = new byte[i]; fileInput.read(content); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=" + new String(fileName.getBytes("GB2312"), "ISO8859-1")); output = response.getOutputStream(); output.write(content); output.flush(); } catch (IOException e) { throw new BizException("导出失败"); } finally { try { fileInput.close(); output.close(); } catch (IOException e) { throw new BizException("导出失败"); } if (file.exists()) { Boolean flag = deleteDirectory(file.getName()); } if (zipFile.exists()) { Boolean flag = deleteFile(zipFile.getName()); } } } } /** * 删除目录(文件夹)以及目录下的文件 * @param sPath 被删除目录的文件路径 * @return 目录删除成功返回true,否则返回false */ private boolean deleteDirectory(String sPath) { //如果sPath不以文件分隔符结尾,自动添加文件分隔符 if (!sPath.endsWith(File.separator)) { sPath = sPath + File.separator; } File dirFile = new File(sPath); //如果dir对应的文件不存在,或者不是一个目录,则退出 if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } Boolean flag = true; //删除文件夹下的所有文件(包括子目录) File[] files = dirFile.listFiles(); for (int i = 0; i < files.length; i++) { //删除子文件 if (files[i].isFile()) { flag = deleteFile(files[i].getAbsolutePath()); if (!flag) break; } //删除子目录 else { /*flag = deleteDirectory(files[i].getAbsolutePath()); if (!flag) break;*/ } } if (!flag) return false; //删除当前目录 if (dirFile.delete()) { return true; } else { return false; } } /** * 删除单个文件 * @param sPath 被删除文件的文件名 * @return 单个文件删除成功返回true,否则返回false */ private boolean deleteFile(String sPath) { Boolean flag = false; File file = new File(sPath); // 路径为文件且不为空则进行删除 if (file.isFile() && file.exists()) { file.delete(); flag = true; } return flag; } /** * 给文件夹打包 * @param sourceFilePath * @param zipFilePath * @param fileName * @return */ private static boolean fileToZip(String sourceFilePath,String zipFilePath,String fileName){ boolean flag = false; File sourceFile = new File(sourceFilePath); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; if(sourceFile.exists() == false){ System.out.println("待压缩的文件目录:"+sourceFilePath+"不存在."); }else{ try { // File zipFile = new File(zipFilePath + "/" + fileName +".zip"); File zipFile = new File(fileName +".zip"); if(zipFile.exists()){ System.out.println(zipFilePath + "目录下存在名字为:" + fileName +".zip" +"打包文件."); }else{ File[] sourceFiles = sourceFile.listFiles(); if(null == sourceFiles || sourceFiles.length<1){ System.out.println("待压缩的文件目录:" + sourceFilePath + "里面不存在文件,无需压缩."); }else{ fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024*10]; for(int i=0;i<sourceFiles.length;i++){ //创建ZIP实体,并添加进压缩包 ZipEntry zipEntry = new ZipEntry(sourceFiles[i].getName()); zos.putNextEntry(zipEntry); //读取待压缩的文件并写进压缩包里 fis = new FileInputStream(sourceFiles[i]); bis = new BufferedInputStream(fis, 1024*10); int read = 0; while((read=bis.read(bufs, 0, 1024*10)) != -1){ zos.write(bufs,0,read); } } flag = true; } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally{ //关闭流 try { if(null != bis) bis.close(); if(null != zos) zos.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } } return flag; }
批量导出二维码操作并打成zip包输出
最新推荐文章于 2024-08-21 15:06:19 发布