Commit 99665a71 authored by huangcb's avatar huangcb

1.word转换为pdf由docx4j调整为aspose-words;2.word转换为pdf由同步改为异步;

parent ba0a696f
......@@ -121,27 +121,10 @@
<artifactId>poi-tl</artifactId>
<version>1.7.3</version>
</dependency>
<!-- word转pdf -->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>6.1.2</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.1.6</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
<groupId>com.aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>16.4.0</version>
</dependency>
<dependency>
<groupId>com.esv.platform</groupId>
......
......@@ -11,6 +11,7 @@ package com.esv.freight.customer.common.constants;
*/
public class CommonConstants {
/**
* 访问端来源:1-浏览器端、2-Android端、3-iOS端、4-后台服务端
**/
......@@ -20,4 +21,51 @@ public class CommonConstants {
public static final String REQ_SOURCE_TYPE_IOS = "3";
public static final String REQ_SOURCE_TYPE_SERVICE = "4";
/**
* Feign调用返回参数
**/
public static final String FEIGN_RESULT_CODE = "code";
public static final String FEIGN_RESULT_MESSAGE = "message";
public static final String FEIGN_RESULT_DATA = "data";
public static final int FEIGN_RESULT_SUCCESS = 200;
/**
* 字符串"null"
**/
public static final String NULL_STRING = "null";
/**
* 字符串"unknown"
**/
public static final String UNKNOWN_STRING = "unknown";
/**
* log日志输出的最大长度及截取输出的长度
**/
public static final int LOG_MAX_LENGTH = 5000;
public static final int LOG_CUT_LENGTH = 1000;
/**
* Spring Boot Admin监控url前缀
**/
public static final String SPRING_BOOT_ADMIN_PREFIX_URL = "/actuator";
/**
* 默认字符编码
**/
public static final String DEFAULT_CHARACTER_ENCODING = "utf-8";
/**
* Http请求方式
**/
public static final String HTTP_REQUEST_METHOD_GET = "GET";
public static final String HTTP_REQUEST_METHOD_POST = "POST";
/**
* Http请求头
**/
public static final String HTTP_HEADER_X_FORWARDED_FOR = "x-forwarded-for";
public static final String HTTP_HEADER_PROXY_CLIENT_IP = "Proxy-Client-IP";
public static final String HTTP_HEADER_WL_PROXY_CLIENT_IP = "WL-Proxy-Client-IP";
}
package com.esv.freight.customer.common.filter;
import com.esv.freight.customer.common.constants.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
......@@ -50,7 +51,7 @@ public class LogbackFilter implements Filter {
}
private boolean setMDC(String traceId) {
if (StringUtils.isEmpty(traceId) || "null".equalsIgnoreCase(traceId)) {
if (StringUtils.isEmpty(traceId) || CommonConstants.NULL_STRING.equalsIgnoreCase(traceId)) {
traceId = UUID.randomUUID().toString().replace("-", "");
}
MDC.put("traceId", traceId);
......
package com.esv.freight.customer.common.filter;
import com.alibaba.fastjson.JSONObject;
import com.esv.freight.customer.common.constants.CommonConstants;
import com.esv.freight.customer.common.util.ReqUtils;
import com.esv.freight.customer.common.wrapper.RestRequestWrapper;
import com.esv.freight.customer.common.wrapper.RestResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
......@@ -30,44 +28,37 @@ import java.util.Map;
@Slf4j
public class RestLogFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) {
}
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
RestRequestWrapper requestWrapper = new RestRequestWrapper((HttpServletRequest)servletRequest);
RestResponseWrapper responseWrapper = new RestResponseWrapper((HttpServletResponse) servletResponse);
String reqContentType = StringUtils.trimToEmpty(requestWrapper.getContentType()).toLowerCase();
if (!reqContentType.contains("multipart/form-data")) {
// 日志输出请求
logReq(requestWrapper);
}
logReqHeader(requestWrapper);
filterChain.doFilter(requestWrapper, responseWrapper);
// 日志输出请求体
this.logReq(requestWrapper);
String resContentType = StringUtils.trimToEmpty(responseWrapper.getContentType());
if (resContentType.contains("text") || resContentType.contains("xml")
|| resContentType.contains("json")) {
// 日志输出返回
try {
logRes(requestWrapper, responseWrapper);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
} else {
log.info("Response ContentType: {}", resContentType);
}
}
// 日志输出请求头
this.logReqHeader(requestWrapper);
@Override
public void destroy() {
filterChain.doFilter(requestWrapper, responseWrapper);
// 日志输出返回体
this.logRes(requestWrapper, responseWrapper);
}
/**
* 获取请求返回体
**/
private String getPostBodyStr(HttpServletRequest request) {
String bodyStr = null;
StringBuilder sb = new StringBuilder();
......@@ -75,7 +66,7 @@ public class RestLogFilter implements Filter {
BufferedReader reader = null;
try {
inputStream = request.getInputStream();
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName(CommonConstants.DEFAULT_CHARACTER_ENCODING)));
String line = "";
while ((line = reader.readLine()) != null) {
sb.append(line);
......@@ -114,30 +105,43 @@ public class RestLogFilter implements Filter {
return bodyStr;
}
/**
* 日志输出请求体
**/
private void logReq(RestRequestWrapper requestWrapper) {
String url = requestWrapper.getRequestURI();
String method = requestWrapper.getMethod();
if ("GET".equalsIgnoreCase(method)) {
String reqBody = "";
// 获取get、post请求体
if (CommonConstants.HTTP_REQUEST_METHOD_GET.equalsIgnoreCase(method)) {
Enumeration em = requestWrapper.getParameterNames();
String reqParams = "";
while (em.hasMoreElements()) {
String k = em.nextElement().toString();
String v = requestWrapper.getParameter(k);
reqParams += "&" + k + "=" + v;
}
reqParams = reqParams.replaceFirst("&", "");
if (url.startsWith("/actuator")) {
log.debug("[IP={}]收到{}请求,url:{},params:{}", getHttpClientIp(requestWrapper), method, url, reqParams);
} else {
log.info("[IP={}]收到{}请求,url:{},params:{}", getHttpClientIp(requestWrapper), method, url, reqParams);
reqBody += "&" + k + "=" + v;
}
} else if ("POST".equalsIgnoreCase(method)) {
log.info("[IP={}]收到{}请求,url:{},body:{}", getHttpClientIp(requestWrapper), method, url, getPostBodyStr(requestWrapper));
} else {
reqBody = reqBody.replaceFirst("&", "");
} else if (CommonConstants.HTTP_REQUEST_METHOD_POST.equalsIgnoreCase(method)) {
reqBody = this.getPostBodyStr(requestWrapper);
}
// 请求体日志截取
if (CommonConstants.LOG_MAX_LENGTH < reqBody.length()) {
reqBody = reqBody.substring(0, CommonConstants.LOG_CUT_LENGTH) + "……";
}
// 日志输出请求体
if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) {
log.debug("[IP={}]收到{}请求,url:{},params:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url, reqBody);
} else {
log.info("[IP={}]收到{}请求,url:{},body:{}", ReqUtils.getHttpClientIp(requestWrapper), method, url, reqBody);
}
}
/**
* 日志输出请求头
**/
private void logReqHeader(RestRequestWrapper request) {
JSONObject headerJson = new JSONObject();
Enumeration headerNames = request.getHeaderNames();
......@@ -148,28 +152,25 @@ public class RestLogFilter implements Filter {
log.info("请求头:{}", headerJson.toJSONString());
}
private void logRes(RestRequestWrapper requestWrapper, RestResponseWrapper responseWrapper) throws Exception {
/**
* 日志输出返回体
**/
private void logRes(RestRequestWrapper requestWrapper, RestResponseWrapper responseWrapper) {
byte[] bytes = responseWrapper.getBody();
String resStr = new String(bytes,"utf-8");
String url = requestWrapper.getRequestURI();
if (url.startsWith("/actuator")) {
log.debug("请求响应:{}", resStr);
} else {
log.info("请求响应:{}", resStr);
}
}
private String getHttpClientIp(HttpServletRequest req){
String ip = req.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("Proxy-Client-IP");
String resBody = null;
try {
resBody = new String(bytes, CommonConstants.DEFAULT_CHARACTER_ENCODING);
} catch (UnsupportedEncodingException e) {
log.error(e.getMessage(), e);
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("WL-Proxy-Client-IP");
String url = requestWrapper.getRequestURI();
if (CommonConstants.LOG_MAX_LENGTH < resBody.length()) {
resBody = resBody.substring(0, CommonConstants.LOG_CUT_LENGTH) + "……";
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getRemoteAddr();
if (url.startsWith(CommonConstants.SPRING_BOOT_ADMIN_PREFIX_URL)) {
log.debug("请求响应:{}", resBody);
} else {
log.info("请求响应:{}", resBody);
}
return ip;
}
}
\ No newline at end of file
package com.esv.freight.customer.common.util;
import com.alibaba.fastjson.JSONObject;
import com.esv.freight.customer.common.constants.CommonConstants;
import com.esv.freight.customer.common.exception.EException;
import com.esv.freight.customer.common.response.ECode;
import lombok.extern.slf4j.Slf4j;
/**
......@@ -25,11 +25,15 @@ public class FeignUtils {
* createTime 2020/04/17 18:12
**/
public static JSONObject getFeignResultData(JSONObject resultJson) throws EException {
log.info(resultJson.toJSONString());
if (ECode.SUCCESS.code() != resultJson.getIntValue("code")) {
throw new EException(resultJson.getIntValue("code"), resultJson.getString("message"));
String result = resultJson.toJSONString();
if (CommonConstants.LOG_MAX_LENGTH < result.length()) {
result = result.substring(0, CommonConstants.LOG_CUT_LENGTH) + "……";
}
log.info(result);
if (CommonConstants.FEIGN_RESULT_SUCCESS == resultJson.getIntValue(CommonConstants.FEIGN_RESULT_CODE)) {
return resultJson.getJSONObject(CommonConstants.FEIGN_RESULT_DATA);
} else {
return resultJson.getJSONObject("data");
throw new EException(resultJson.getIntValue(CommonConstants.FEIGN_RESULT_CODE), resultJson.getString(CommonConstants.FEIGN_RESULT_MESSAGE));
}
}
......
package com.esv.freight.customer.common.util;
import com.alibaba.fastjson.JSONObject;
import com.esv.freight.customer.common.constants.CommonConstants;
import com.esv.freight.customer.common.exception.EException;
import com.esv.freight.customer.common.response.ECode;
import com.esv.gateway.common.GatewayHeaders;
......@@ -74,14 +75,14 @@ public class ReqUtils {
* @return
*/
public static String getHttpClientIp(HttpServletRequest req){
String ip = req.getHeader("x-forwarded-for");
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("Proxy-Client-IP");
String ip = req.getHeader(CommonConstants.HTTP_HEADER_X_FORWARDED_FOR);
if(ip == null || ip.length() == 0 || CommonConstants.UNKNOWN_STRING.equalsIgnoreCase(ip)) {
ip = req.getHeader(CommonConstants.HTTP_HEADER_PROXY_CLIENT_IP);
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = req.getHeader("WL-Proxy-Client-IP");
if(ip == null || ip.length() == 0 || CommonConstants.UNKNOWN_STRING.equalsIgnoreCase(ip)) {
ip = req.getHeader(CommonConstants.HTTP_HEADER_WL_PROXY_CLIENT_IP);
}
if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if(ip == null || ip.length() == 0 || CommonConstants.UNKNOWN_STRING.equalsIgnoreCase(ip)) {
ip = req.getRemoteAddr();
}
return ip;
......
package com.esv.freight.customer.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* @description: 异步任务
* @project: freight-customer-service
* @name: com.esv.freight.customer.config.AsyncConfig
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/05/26 9:19
* @version:1.0
*/
@Configuration
@EnableAsync
public class AsyncConfig {
}
......@@ -41,13 +41,21 @@ public class ContractOnlineRecordEntity implements Serializable {
*/
private Integer contractType;
/**
* 合同文件ID
* 合同文件ID
*/
private String contractFileId;
private String sourceFileId;
/**
* 合同文件URL
* 合同文件URL
*/
private String contractFileUrl;
private String sourceFileUrl;
/**
* 合同目标文件ID
*/
private String targetFileId;
/**
* 合同目标文件URL
*/
private String targetFileUrl;
/**
* 合同编号
*/
......
......@@ -43,7 +43,7 @@
<!-- mybatis sql日志 日志的级别需要是DEBUG -->
<!-- 日志打印的包的范围,及分类日志文件存储 -->
<logger name="com.esv" level="DEBUG" additivity="false">
<logger name="com.esv" level="INFO" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
......@@ -52,6 +52,10 @@
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
<logger name="com.deepoove.poi" level="ERROR" additivity="false">
<appender-ref ref="CONSOLE_APPENDER" />
<appender-ref ref="FILE_APPENDER" />
</logger>
<!--控制台和日志文件输出级别-->
<root level="INFO" additivity="false">
......
......@@ -9,8 +9,10 @@
<result property="tenantId" column="tenant_id"/>
<result property="departmentId" column="department_id"/>
<result property="contractType" column="contract_type"/>
<result property="contractFileId" column="contract_file_id"/>
<result property="contractFileUrl" column="contract_file_url"/>
<result property="sourceFileId" column="source_file_id"/>
<result property="sourceFileUrl" column="source_file_url"/>
<result property="targetFileId" column="target_file_id"/>
<result property="targetFileUrl" column="target_file_url"/>
<result property="contractNumber" column="contract_number"/>
<result property="businessNumber" column="business_number"/>
<result property="customerId" column="customer_id"/>
......
......@@ -29,14 +29,20 @@ public class BaseTestController {
MockMvc mockMvc;
private static Long TEST_START_TIME;
private static Long TEST_END_TIME;
@Before
public void before() {
log.info("=================================== Test Start ===================================");
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
TEST_START_TIME = System.currentTimeMillis();
}
@After
public void after() {
TEST_END_TIME = System.currentTimeMillis();
log.info("Test耗时:" + (TEST_END_TIME - TEST_START_TIME) + "毫秒");
log.info("=================================== Test End ===================================");
}
......
package com.esv.freight.customer.common.util;
import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileOutputStream;
/**
* @description:
* @project: freight-customer-service
* @name: com.esv.freight.customer.common.util.AsposeWordsTest
* @author: 黄朝斌
* @email: huangchaobin@esvtek.com
* @createTime: 2020/05/25 20:40
* @version:1.0
*/
@SpringBootTest
@Slf4j
public class AsposeWordsTest {
@Test
public void word_2_pdf_time_test() {
for (int i = 0; i < 10; i++) {
word_2_pdf_test(i);
}
}
public static void word_2_pdf_test(int i) {
String sourcePath = "D:\\test\\线上合同-货主.docx";
String pdfPath = "D:\\test\\线上合同-货主-test-aspose-" + (i + 1) + ".pdf";
long old = System.currentTimeMillis();
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(new File(pdfPath));
Document doc = new Document(sourcePath);
doc.save(fileOutputStream, SaveFormat.PDF);
} catch (Exception e) {
e.printStackTrace();
log.error("docx文档转换为PDF失败");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
long now = System.currentTimeMillis();
log.info("共耗时:" + ((now - old) / 1000.0) + "秒");
}
}
package com.esv.freight.customer.common.util;
import com.baomidou.mybatisplus.core.toolkit.BeanUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.esv.freight.customer.module.contract.pojo.ContractOnlineGoodsOwnerPojo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.docx4j.Docx4J;
import org.docx4j.fonts.IdentityPlusMapper;
import org.docx4j.fonts.Mapper;
import org.docx4j.fonts.PhysicalFonts;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
......@@ -36,7 +28,6 @@ public class WordTest {
public void simple_test() throws Exception {
String sourcePath = "D:\\test\\线上合同-货主.docx";
String targetPath = "D:\\test\\线上合同-货主-合成.docx";
String pdfPath = "D:\\test\\线上合同-货主-PDF.pdf";
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("contactNumber", System.currentTimeMillis());
......@@ -68,27 +59,12 @@ public class WordTest {
out.flush();
out.close();
template.close();
FileOutputStream fileOutputStream = null;
try {
File file = new File(targetPath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
} catch (Exception e) {
e.printStackTrace();
log.error("docx文档转换为PDF失败");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
}
@Test
public void pojo_test() throws Exception {
String sourcePath = "D:\\test\\线上合同-货主.docx";
String targetPath = "D:\\test\\线上合同-货主-合成.docx";
String pdfPath = "D:\\test\\线上合同-货主-PDF.pdf";
ContractOnlineGoodsOwnerPojo pojo = new ContractOnlineGoodsOwnerPojo();
pojo.setContactNumber(String.valueOf(System.currentTimeMillis()));
......@@ -110,8 +86,6 @@ public class WordTest {
pojo.setEffectiveTime("{{effectiveTime}}");
pojo.setGoodsOwnerSignImg(new PictureRenderData(150, 60, "D:\\test\\何锋.jpg"));
pojo.setPlatformFreightSealImg("{{@platformFreightSealImg}}");
// Map<String, Object> dataMap = BeanUtils.beanToMap(pojo);
// dataMap.put("platformFreightSealImg", "{{@platformFreightSealImg}}");
XWPFTemplate template = XWPFTemplate.compile(sourcePath);
template.render(pojo);
......@@ -120,47 +94,5 @@ public class WordTest {
out.flush();
out.close();
template.close();
FileOutputStream fileOutputStream = null;
try {
File file = new File(targetPath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
} catch (Exception e) {
e.printStackTrace();
log.error("docx文档转换为PDF失败");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
}
@Test
public void pdf_test() throws Exception {
String sourcePath = "D:\\test\\线上合同-货主.docx";
String pdfPath = "D:\\test\\线上合同-货主-test.pdf";
FileOutputStream fileOutputStream = null;
try {
File file = new File(sourcePath);
fileOutputStream = new FileOutputStream(new File(pdfPath));
WordprocessingMLPackage mlPackage = WordprocessingMLPackage.load(file);
setFontMapper(mlPackage);
Docx4J.toPDF(mlPackage, new FileOutputStream(new File(pdfPath)));
} catch (Exception e) {
e.printStackTrace();
log.error("docx文档转换为PDF失败");
} finally {
IOUtils.closeQuietly(fileOutputStream);
}
}
private static void setFontMapper(WordprocessingMLPackage mlPackage) throws Exception {
Mapper fontMapper = new IdentityPlusMapper();
fontMapper.put("等线", PhysicalFonts.get("Arial"));
fontMapper.put("宋体", PhysicalFonts.get("SimSun"));
mlPackage.setFontMapper(fontMapper);
}
}
......@@ -139,7 +139,7 @@ public class ContractOnlineRecordControllerTest extends BaseTestController {
// 构造数据
ContractOnlineRecordForm form = new ContractOnlineRecordForm();
form.setContractNumber("HZHT20200522000006");
form.setContractNumber("HZHT20200526000002");
MvcResult mvcResult = this.getMockMvc().perform(MockMvcRequestBuilders.post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
......@@ -191,7 +191,7 @@ public class ContractOnlineRecordControllerTest extends BaseTestController {
// 构造数据
ContractOnlineRecordForm form = new ContractOnlineRecordForm();
form.setContractNumber("HZHT20200525000002");
form.setContractNumber("HZHT20200526000002");
MvcResult mvcResult = this.getMockMvc().perform(MockMvcRequestBuilders.post(url)
.contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)
......
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