Springboot將多個圖片導出成zip壓縮包
將多個圖片導出成zip壓縮包
/*** 判斷時間差是否超過6小時* @param startTime 開始時間* @param endTime 結束時間* @return*/public static boolean isWithin6Hours(String startTime, String endTime) {// 定義日期時間格式DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");// 將字符串轉換為 LocalDateTimeLocalDateTime startDateTime = LocalDateTime.parse(startTime,formatter);LocalDateTime endDateTime = LocalDateTime.parse(endTime,formatter);// 檢查兩個時間之間的時間差long hoursDifference = ChronoUnit.SECONDS.between(startDateTime, endDateTime);// 如果小時差小于等于6小時,則符合條件return hoursDifference <= 6 * 60 * 60;}/*** 批量下載上傳的圖片** @param startTime 開始時間* @param endTime 結束時間*/public void batchDownloadUploadPhoto(String startTime, String endTime,String fieldId,String unitTypeId,Integer unitNo,Integer termAddr, HttpServletResponse response, HttpServletRequest request) {if (StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime)) {throw new DataValidateErrorException("請先傳遞時間在批量導出!");}if (!startTime.contains(":") || !endTime.contains(":")) {throw new DataValidateErrorException("請傳遞時分秒");}//相隔時間不得超過6小時if(!isWithin6Hours(startTime,endTime)){//大于6小時,拋出異常throw new DataValidateErrorException("導出的時間范圍不得超過6小時!");}//查詢導出圖片urlList<ChuteImageAddress> addressList = chuteImageAddressService.selectUploadPhoto(startTime, endTime, fieldId, unitTypeId, unitNo, termAddr);//沒有數據時,拋出異常if(CollectionUtils.isEmpty(addressList)){throw new DataValidateErrorException("無數據導出!");}DownloadByteArray downloadByteArray = new DownloadByteArray();// 創建臨時文件File zipFile = null;FileInputStream fis = null;BufferedInputStream buff = null;ZipOutputStream zos = null;CheckedOutputStream cos = null;FileOutputStream fot = null;ServletOutputStream os = null;try {//臨時文件名稱zipFile = File.createTempFile("" + System.currentTimeMillis(), ".zip");fot = new FileOutputStream(zipFile);// 為任何OutputStream產生校驗,第一個參數是制定產生校驗和的輸出流,第二個參數是指定Checksum的類型 (Adler32(較快)和CRC32兩種)cos = new CheckedOutputStream(fot, new Adler32());// 用于將數據壓縮成Zip文件格式zos = new ZipOutputStream(cos);String[] files = new String[addressList.size()];List<String> list = addressList.stream().map(ChuteImageAddress::getImgUrl).collect(Collectors.toList());list.toArray(files);for (int i = 0; i < files.length; i++) {String file = files[i];zos.putNextEntry(new ZipEntry(addressList.get(i).getImgName()));StorePath storePath = StorePath.praseFromUrl(file);byte[] fileBytes = client.downloadFile(storePath.getGroup(), storePath.getPath(), downloadByteArray);zos.write(fileBytes);}os = response.getOutputStream();//下載文件,使用spring框架中的FileCopyUtils工具response.setCharacterEncoding("GB2312");response.setContentType(request.getSession().getServletContext().getMimeType(UUIDUtil.create()));//設置響應頭,attachment表示以附件的形式下載,inline表示在線打開response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("批量下載.zip", "UTF-8"));fis = new FileInputStream(zipFile);buff = new BufferedInputStream(fis);FileCopyUtils.copy(buff, os);} catch (Exception e) {log.error("【批量下載圖片出現異常】異常原因" + e.getMessage());} finally {if (zos != null) {try {zos.close();} catch (IOException e) {e.printStackTrace();}}if (fot != null) {try {fot.close();} catch (IOException e) {e.printStackTrace();}}if (os != null) {try {os.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}if (buff != null) {try {buff.close();} catch (IOException e) {e.printStackTrace();}}}}