Commit 7bd483fd authored by hongguangwu's avatar hongguangwu

1.7.21-优化临时文件

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