Commit 7bd483fd authored by hongguangwu's avatar hongguangwu

1.7.21-优化临时文件

parent a77104c7
......@@ -1293,7 +1293,7 @@ public class TSocialFriendPushServiceImpl extends ServiceImpl<TSocialFriendMappe
String filePath = projectRoot + CommonConstants.DOUBLE_LINE + fileName;
//获取要导出的列表
List<TDispatchSocialPersionExportVo> list = new ArrayList<>();
ExcelWriter excelWriter = null;
//ExcelWriter excelWriter = null;
File createFile = null;
try {
......@@ -1379,9 +1379,9 @@ public class TSocialFriendPushServiceImpl extends ServiceImpl<TSocialFriendMappe
finallyClose(null, null, null, inputStream, null, inStream, null);
}
} catch (Exception e) {
if (excelWriter != null) {
/*if (excelWriter != null) {
excelWriter.finish();
}
}*/
log.error("税友附件生成异常", e);
return R.failed("税友附件生成异常:" + e.getMessage());
} finally {
......
......@@ -53,26 +53,28 @@ public class ExcelToImage {
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagParamError);
}
String projectRoot = System.getProperty(CommonConstants.USER_DIR);
File tempFile = null;
File compressedTempFile = null;
try {
//加载Excel工作表
Workbook wb = new Workbook();
wb.loadFromHtml(inputStream);
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//调用方法将Excel工作表保存为图片
String toImagePath = projectRoot + toImag;
sheet.saveToImage(projectRoot+toImag);
File file = null;
try {
tempFile = new File(toImagePath);
// 上传税友文件服务器,返回文件路径
file = new File(projectRoot+toImag);
if (file.exists()){
if (tempFile.exists()){
// file 压缩到1M以内
backLog.setLogInfo("压缩大小前");
// 压缩并加章
file = compressImage(file, projectRoot, zhangStream);
tempFile = compressImage(tempFile, projectRoot, zhangStream, compressedTempFile);
backLog.setLogInfo("压缩大小后");
String key = System.currentTimeMillis() + file.getName();
byte[] fileContent = Files.readAllBytes(file.toPath());
String key = System.currentTimeMillis() + tempFile.getName();
byte[] fileContent = Files.readAllBytes(tempFile.toPath());
String base64String = Base64.getEncoder().encodeToString(fileContent);
backLog.setLogInfo("转化为base64String");
String url = socialFriendConfig.uploadPng(restTemplate, key, base64String, backLog);
......@@ -80,15 +82,18 @@ public class ExcelToImage {
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagOSSError);
}
// 上传花名册到阿里云
this.uploadHuaMingCeToOss(ossUtil, tSocialFriendBackLogService, socialId, file.getName(), file);
this.uploadHuaMingCeToOss(ossUtil, tSocialFriendBackLogService, socialId, tempFile.getName(), tempFile);
return R.ok(url);
}
} catch (Exception e) {
e.printStackTrace();
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagConvertError+e.getMessage());
} finally {
if (file.exists()){
file.delete();
if (tempFile != null && tempFile.exists()){
tempFile.delete();
}
if (compressedTempFile != null && compressedTempFile.exists()){
compressedTempFile.delete();
}
if (null != inputStream){
try {
......@@ -133,33 +138,26 @@ public class ExcelToImage {
}
//压缩文件到指定大小
private File compressImage(File originalFile, String projectRoot, InputStream zhangStream) throws IOException {
private File compressImage(File originalFile, String projectRoot, InputStream zhangStream, File compressedTempFile) throws IOException {
BufferedImage image = ImageIO.read(originalFile);
// 新: 加盖公章
BufferedImage sealedImage = addSealToImage(image, zhangStream);
ImageIO.write(sealedImage, "png", originalFile);
int quality = 100; // 从100%开始
File compressedFile = new File(projectRoot, TO_IMAGE_TEMP);
int maxSize = 1024 * 1024;
try {
while (originalFile.length() > maxSize && quality > 10) {
quality -= 5;
ImageIO.write(sealedImage, "png", compressedFile);
if (compressedFile.length() < maxSize) {
ImageIO.write(sealedImage, "png", compressedTempFile);
if (compressedTempFile.length() < maxSize) {
originalFile.delete();
compressedFile.renameTo(originalFile);
compressedTempFile.renameTo(originalFile);
break;
}
compressedFile.delete();
compressedTempFile.delete();
}
} catch (Exception e) {
if (compressedFile != null && compressedFile.exists()) {
compressedFile.delete();
}
} finally {
if (compressedFile != null && compressedFile.exists()) {
compressedFile.delete();
}
throw e;
}
return originalFile;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment