Resources should be closed

This commit is contained in:
fuqingji
2022-03-17 09:41:33 +08:00
parent 468a14087a
commit 346ec02cb0
2 changed files with 40 additions and 31 deletions

View File

@ -40,6 +40,7 @@ import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import java.util.stream.Stream;
import static java.lang.String.format; import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -97,20 +98,22 @@ public final class Utils {
} }
StringBuilder hashBuilder = new StringBuilder(hash); StringBuilder hashBuilder = new StringBuilder(hash);
Files.walk(projectPath) try (Stream<Path> walk = Files.walk(projectPath)) {
.sorted(Comparator.naturalOrder()) walk.sorted(Comparator.naturalOrder())
.filter(Files::isRegularFile) .filter(Files::isRegularFile)
.map(Path::toFile) .map(Path::toFile)
.forEach(file -> { .forEach(file -> {
try { try {
byte[] buf = readFile(file); byte[] buf = readFile(file);
byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8)); byte[] toHash = Arrays.concatenate(buf, hashBuilder.toString().getBytes(UTF_8));
hashBuilder.setLength(0); hashBuilder.setLength(0);
hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest()))); hashBuilder.append(Hex.toHexString(hash(toHash, new SHA3Digest())));
} catch (IOException ex) { } catch (IOException ex) {
throw new RuntimeException(format("Error while reading file %s", file.getAbsolutePath()), 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 original hash and final hash are the same, it indicates that no new contents were found
if (hashBuilder.toString().equals(hash)) { if (hashBuilder.toString().equals(hash)) {
@ -240,10 +243,11 @@ public final class Utils {
if (file.isDirectory()) { if (file.isDirectory()) {
Path rootPath = Paths.get(file.getAbsolutePath()); Path rootPath = Paths.get(file.getAbsolutePath());
Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS) try (Stream<Path> walk = Files.walk(rootPath, FileVisitOption.FOLLOW_LINKS)) {
.sorted(Comparator.reverseOrder()) walk.sorted(Comparator.reverseOrder())
.map(Path::toFile) .map(Path::toFile)
.forEach(File::delete); .forEach(File::delete);
}
} else { } else {
file.delete(); file.delete();
} }
@ -296,7 +300,7 @@ public final class Utils {
} }
Properties props = new Properties(); 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); Matcher m = p.matcher(url);
if (m.matches()) { if (m.matches()) {
props.setProperty("protocol", m.group(1)); props.setProperty("protocol", m.group(1));

View File

@ -1,6 +1,7 @@
package com.chinaunicom.mall.ebtp.extend.signature.service.impl; package com.chinaunicom.mall.ebtp.extend.signature.service.impl;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import com.aspose.words.FontSettings; import com.aspose.words.FontSettings;
import com.aspose.words.License; import com.aspose.words.License;
@ -79,7 +80,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService {
try { try {
pdf = mergeWord(word, expertSign, path); pdf = mergeWord(word, expertSign, path);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.info(ExceptionUtil.stacktraceToString(e));
} }
//上传pdf到文档中心 //上传pdf到文档中心
@ -123,18 +124,22 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService {
} }
File f = new File(path + "merged.pdf"); File f = new File(path + "merged.pdf");
FileOutputStream fos = new FileOutputStream(f); FileOutputStream fos = null;
FontSettings.getDefaultInstance().setFontsFolder(fontAddress, true); FontSettings.getDefaultInstance().setFontsFolder(fontAddress, true);
try { try {
fos = new FileOutputStream(f);
com.aspose.words.Document doc = new com.aspose.words.Document(path + "merged.docx"); com.aspose.words.Document doc = new com.aspose.words.Document(path + "merged.docx");
doc.save(fos, SaveFormat.PDF); doc.save(fos, SaveFormat.PDF);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.info(ExceptionUtil.stacktraceToString(e));
} finally { } finally {
source.close(); source.close();
addObj.close(); addObj.close();
fos.flush(); if (fos != null) {
fos.close(); fos.flush();
fos.close();
}
} }
long l1 = System.currentTimeMillis(); long l1 = System.currentTimeMillis();
@ -151,7 +156,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService {
license.setLicense(is); license.setLicense(is);
result = true; result = true;
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.info(ExceptionUtil.stacktraceToString(e));
} }
return result; return result;
} }
@ -184,7 +189,7 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService {
template.writeToFile(docPath); template.writeToFile(docPath);
fis = new FileInputStream(new File(docPath)); fis = new FileInputStream(new File(docPath));
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); log.info(ExceptionUtil.stacktraceToString(e));
} }
return fis; return fis;
@ -210,12 +215,12 @@ public class ExpertSignatureServiceImpl implements ExpertSignatureService {
b[i] += 256; b[i] += 256;
} }
} }
OutputStream out = new FileOutputStream(imgFilePath); try (OutputStream out = new FileOutputStream(imgFilePath)) {
out.write(b); out.write(b);
out.flush(); }
out.close();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); log.info(ExceptionUtil.stacktraceToString(e));
} }
} }