Commit 81ea44b7 authored by fangxinjiang's avatar fangxinjiang

代码优化-fxj

parent 7d4448dc
......@@ -4,6 +4,7 @@ package com.yifu.cloud.plus.v1.yifu.social.util;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.common.core.util.OSSUtil;
import com.yifu.cloud.plus.v1.yifu.common.core.util.R;
import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig;
......@@ -27,10 +28,18 @@ import java.util.Base64;
@Component
public class ExcelToImage {
private final static String toImag ="\\ToImg.png";
private final static String toImagParamError = ":带印章excel生成图片传参异常";
private final static String toImagConvertError = ":ExcelToImage转换异常-";
private final static String toImagOSSError = ":ExcelToImage上传OSS异常";
public R<String> excelToImg(InputStream inputStream , SocialFriendConfig socialFriendConfig
, RestTemplate restTemplate, TSocialFriendBackLog backLog){
if (null == inputStream || null == socialFriendConfig){
return R.failed(CommonConstants.RESULT_DATA_FAIL+":带印章excel生成图片传参异常");
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagParamError);
}
String projectRoot = System.getProperty(CommonConstants.USER_DIR);
//加载Excel工作表
......@@ -40,20 +49,23 @@ public class ExcelToImage {
Worksheet sheet = wb.getWorksheets().get(0);
//调用方法将Excel工作表保存为图片
sheet.saveToImage(projectRoot+"\\ToImg.png");
sheet.saveToImage(projectRoot+toImag);
File file = null;
try {
// 上传税友文件服务器,返回文件路径
file = new File(projectRoot+"\\ToImg.png");
file = new File(projectRoot+toImag);
if (file.exists()){
String key = System.currentTimeMillis() + file.getName();
byte[] fileContent = Files.readAllBytes(file.toPath());
String base64String = Base64.getEncoder().encodeToString(fileContent);
String url = socialFriendConfig.uploadPng(restTemplate, key, base64String, backLog);
if (Common.isEmpty(url)){
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagOSSError);
}
return R.ok(url);
}
} catch (Exception e) {
return R.failed(CommonConstants.RESULT_DATA_FAIL+":ExcelToImage转换异常-"+e.getMessage());
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagConvertError+e.getMessage());
} finally {
if (file.exists()){
file.delete();
......@@ -70,7 +82,7 @@ public class ExcelToImage {
}
public R<URL> excelToImg(InputStream inputStream ,OSSUtil ossUtil){
if (null == inputStream || null == ossUtil){
return R.failed(CommonConstants.RESULT_DATA_FAIL+":带印章excel生成图片传参异常");
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagParamError);
}
String projectRoot = System.getProperty(CommonConstants.USER_DIR);
//加载Excel工作表
......@@ -79,20 +91,20 @@ public class ExcelToImage {
//获取工作表
Worksheet sheet = wb.getWorksheets().get(0);
//调用方法将Excel工作表保存为图片
sheet.saveToImage(projectRoot+"\\ToImg.png");
sheet.saveToImage(projectRoot+toImag);
File file = null;
try {
file = new File(projectRoot+"\\ToImg.png");
file = new File(projectRoot+toImag);
if (file.exists()){
String key = System.currentTimeMillis() + file.getName();
if (ossUtil.uploadFileByFile(file,key,null)){
return R.ok(ossUtil.getObjectUrl(null,key));
}else {
return R.failed(CommonConstants.RESULT_DATA_FAIL+":ExcelToImage上传OSS异常");
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagOSSError);
}
}
} catch (Exception e) {
return R.failed(CommonConstants.RESULT_DATA_FAIL+":ExcelToImage转换异常-"+e.getMessage());
return R.failed(CommonConstants.RESULT_DATA_FAIL+toImagConvertError+e.getMessage());
} finally {
if (file.exists()){
file.delete();
......
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