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.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,8 +98,8 @@ public final class Utils {
}
StringBuilder hashBuilder = new StringBuilder(hash);
Files.walk(projectPath)
.sorted(Comparator.naturalOrder())
try (Stream<Path> walk = Files.walk(projectPath)) {
walk.sorted(Comparator.naturalOrder())
.filter(Files::isRegularFile)
.map(Path::toFile)
.forEach(file -> {
@ -111,6 +112,8 @@ public final class Utils {
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())
try (Stream<Path> 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));

View File

@ -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,20 +124,24 @@ 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();
if (fos != null) {
fos.flush();
fos.close();
}
}
long l1 = System.currentTimeMillis();
log.info(" ======= " + (l1 - l) + " ======= ");
@ -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);
try (OutputStream out = new FileOutputStream(imgFilePath)) {
out.write(b);
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
log.info(ExceptionUtil.stacktraceToString(e));
}
}