Sfoglia il codice sorgente

附件文档生成前转义特殊字符

陈长荣 3 mesi fa
parent
commit
5c9db3647e

+ 1 - 0
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/file/service/impl/FileInfoServiceImpl.java

@@ -96,6 +96,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
                         .insert();
             }
         } catch (Exception e) {
+            log.error("附件生成失败 data={}", dataMap.toJSONString().replaceAll("\"iVBOR.*?\"", "\"[图片Base64]\""));
             log.error(e.getMessage(), e);
             throw new RuntimeException("附件生成失败");
         }

+ 19 - 3
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/util/WordUtil.java

@@ -1,6 +1,8 @@
 package com.github.jfcloud.gene.util;
 
 import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson.JSONObject;
+import com.alibaba.fastjson.serializer.SerializerFeature;
 import com.github.jfcloud.gene.handler.MyCheck;
 import com.github.jfcloud.gene.handler.MyJsonFilter;
 import freemarker.template.Configuration;
@@ -14,17 +16,31 @@ import java.util.Map;
 @Slf4j
 public class WordUtil {
 
+    public static final String[][] XML_ESCAPE = {
+            {"&", "&amp;"}, // & - ampersand
+            {"<", "&lt;"}, // < - less-than
+            {">", "&gt;"}, // > - greater-than
+    };
+
     /**
      * @param modelName 模板的名称 比如"basic.ftl"
      * @param dataMap   传入的数据(key=ftl 中的站位的名称同时 要是String )
      */
-    public static ByteArrayOutputStream exportWordOut(String modelName, Map<String, Object> dataMap) {
+    public static ByteArrayOutputStream exportWordOut(String modelName, JSONObject dataMap) {
+        Object modal = dataMap;
         if (dataMap != null) {
+            //null 值转为 空
             for (Map.Entry<String, Object> entry : dataMap.entrySet()) {
                 if (entry.getValue() == null) {
                     entry.setValue("");
                 }
             }
+            String string = JSONObject.toJSONString(dataMap, SerializerFeature.WRITE_MAP_NULL_FEATURES);
+            //转义特殊符号
+            for (String[] pair : XML_ESCAPE) {
+                string = string.replaceAll(pair[0], pair[1]);
+            }
+            modal = JSONObject.parseObject(string, JSONObject.class);
         }
 
         // freemaker配置
@@ -37,7 +53,7 @@ public class WordUtil {
 
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
         try (Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))){
-            configuration.getTemplate(modelName).process(dataMap, out);
+            configuration.getTemplate(modelName).process(modal, out);
             return outputStream;
         } catch (Exception e) {
             log.error("导出word异常", e);
@@ -49,7 +65,7 @@ public class WordUtil {
      * @param modelName 模板的名称 比如"basic.ftl"
      * @param dataMap   传入的数据(key=ftl 中的站位的名称同时 要是String )
      */
-    public static InputStream exportWord(String modelName, Map<String, Object> dataMap) {
+    public static InputStream exportWord(String modelName, JSONObject dataMap) {
         try (ByteArrayOutputStream outputStream = exportWordOut(modelName, dataMap)){
             if (outputStream != null) {
                 return new ByteArrayInputStream(outputStream.toByteArray());