diff --git a/src/main/java/com/chinaunicom/mall/ebtp/extend/crypconfigure/crypto/helper/Utils.java b/src/main/java/com/chinaunicom/mall/ebtp/extend/crypconfigure/crypto/helper/Utils.java index ccd6c3f..f4ac2e1 100644 --- a/src/main/java/com/chinaunicom/mall/ebtp/extend/crypconfigure/crypto/helper/Utils.java +++ b/src/main/java/com/chinaunicom/mall/ebtp/extend/crypconfigure/crypto/helper/Utils.java @@ -40,6 +40,7 @@ import java.util.Properties; import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; +import java.util.stream.Stream; import static java.lang.String.format; import static java.nio.charset.StandardCharsets.UTF_8; @@ -97,20 +98,22 @@ public final class Utils { } StringBuilder hashBuilder = new StringBuilder(hash); - Files.walk(projectPath) - .sorted(Comparator.naturalOrder()) - .filter(Files::isRegularFile) - .map(Path::toFile) - .forEach(file -> { - try { - byte[] buf = readFile(file); - byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8)); - hashBuilder.setLength(0); - hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest()))); - } catch (IOException ex) { - throw new RuntimeException(format("Error while reading file %s", file.getAbsolutePath()), ex); - } - }); + try (Stream walk = Files.walk(projectPath)) { + walk.sorted(Comparator.naturalOrder()) + .filter(Files::isRegularFile) + .map(Path::toFile) + .forEach(file -> { + try { + byte[] buf = readFile(file); + byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8)); + hashBuilder.setLength(0); + hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest()))); + } catch (IOException ex) { + throw new RuntimeException(format("Error while reading file %s", file.getAbsolutePath()), ex); + } + }); + } + // If original hash and final hash are the same, it indicates that no new contents were found if (hashBuilder.toString().equals(hash)) { @@ -240,10 +243,11 @@ public final class Utils { if (file.isDirectory()) { Path rootPath = Paths.get(file.getAbsolutePath()); - Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS) - .sorted(Comparator.reverseOrder()) - .map(Path::toFile) - .forEach(File::delete); + try (Stream walk = Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)) { + walk.sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } } else { file.delete(); } @@ -296,7 +300,7 @@ public final class Utils { } Properties props = new Properties(); - Pattern p = Pattern.compile("([^:]+)[:]//([^:]+)[:]([0-9]+)", Pattern.CASE_INSENSITIVE); + final Pattern p = Pattern.compile("([^:]+)[:]//([^:]+)[:]([0-9]+)", Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(url); if (m.matches()) { props.setProperty("protocol", m.group(1)); diff --git a/src/main/java/com/chinaunicom/mall/ebtp/extend/signature/service/impl/ExpertSignatureServiceImpl.java b/src/main/java/com/chinaunicom/mall/ebtp/extend/signature/service/impl/ExpertSignatureServiceImpl.java index 12fcc34..722b64e 100644 --- a/src/main/java/com/chinaunicom/mall/ebtp/extend/signature/service/impl/ExpertSignatureServiceImpl.java +++ b/src/main/java/com/chinaunicom/mall/ebtp/extend/signature/service/impl/ExpertSignatureServiceImpl.java @@ -1,6 +1,7 @@ package com.chinaunicom.mall.ebtp.extend.signature.service.impl; +import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.io.FileUtil; import com.aspose.words.FontSettings; import com.aspose.words.License; @@ -79,7 +80,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService { try { pdf = mergeWord(word, expertSign, path); } catch (IOException e) { - e.printStackTrace(); + log.info(ExceptionUtil.stacktraceToString(e)); } //上传pdf到文档中心 @@ -123,18 +124,22 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService { } File f = new File(path + "merged.pdf"); - FileOutputStream fos = new FileOutputStream(f); + FileOutputStream fos = null; FontSettings.getDefaultInstance().setFontsFolder(fontAddress, true); try { + fos = new FileOutputStream(f); com.aspose.words.Document doc = new com.aspose.words.Document(path + "merged.docx"); doc.save(fos, SaveFormat.PDF); } catch (Exception e) { - e.printStackTrace(); + log.info(ExceptionUtil.stacktraceToString(e)); } finally { source.close(); addObj.close(); - fos.flush(); - fos.close(); + if (fos != null) { + fos.flush(); + fos.close(); + } + } long l1 = System.currentTimeMillis(); @@ -151,7 +156,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService { license.setLicense(is); result = true; } catch (Exception e) { - e.printStackTrace(); + log.info(ExceptionUtil.stacktraceToString(e)); } return result; } @@ -184,7 +189,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService { template.writeToFile(docPath); fis = new FileInputStream(new File(docPath)); } catch (IOException e) { - e.printStackTrace(); + log.info(ExceptionUtil.stacktraceToString(e)); } return fis; @@ -210,12 +215,12 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService { b[i] += 256; } } - OutputStream out = new FileOutputStream(imgFilePath); - out.write(b); - out.flush(); - out.close(); + try (OutputStream out = new FileOutputStream(imgFilePath)) { + out.write(b); + } + } catch (Exception e) { - e.printStackTrace(); + log.info(ExceptionUtil.stacktraceToString(e)); } }