Browse Source

动物样本附件调整,转义XML特殊字符

陈长荣 2 tháng trước cách đây
mục cha
commit
bea1e25d2c

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

@@ -71,7 +71,7 @@ public class FileInfoServiceImpl extends ServiceImpl<FileInfoMapper, FileInfo> i
     @Override
     public void uploadFileWithFileName(String modelName, String fileName, Long flowId, String flowStatus, JSONObject dataMap) {
         log.info("流程id={} status={} 模板modelName={} 生成文档:{}", flowId, flowStatus, modelName, fileName);
-
+        log.debug("附件生成 data={}", dataMap.toJSONString().replaceAll("\"iVBOR.*?\"", "\"[图片Base64]\""));
         try (InputStream inputStream = WordUtil.exportWord(modelName, dataMap);
              ByteArrayOutputStream out = new ByteArrayOutputStream()) {
             //转化为docx文件

+ 25 - 8
jfcloud-gene-biz/src/main/java/com/github/jfcloud/gene/util/WordUtil.java

@@ -23,6 +23,23 @@ public class WordUtil {
             {">", "&gt;"}, // > - greater-than
     };
 
+    /**
+     * 转义XML特殊字符
+     */
+    private static String escapeString(String content) {
+        if (StrUtil.isBlank(content)) {
+            return content;
+        }
+
+        // 转义特殊字符,除了标签内容
+        if (!content.startsWith("<") && !content.endsWith(">")) {
+            for (String[] pair : XML_ESCAPE) {
+                content = content.replaceAll(pair[0], pair[1]);
+            }
+        }
+        return content;
+    }
+
     /**
      * 转义XML特殊字符
      */
@@ -32,13 +49,7 @@ public class WordUtil {
             Object value = obj.get(key);
             if (value instanceof String) {
                 String content = (String) value;
-                // 转义特殊字符,除了标签内容
-                if (StrUtil.isNotBlank(content) && !content.startsWith("<") && !content.endsWith(">")) {
-                    for (String[] pair : XML_ESCAPE) {
-                        content = content.replaceAll(pair[0], pair[1]);
-                    }
-                    obj.put(key, content);
-                }
+                obj.put(key, escapeString(content));
                 continue;
             }
             //递归调用map
@@ -50,7 +61,13 @@ public class WordUtil {
             if (value instanceof Collection) {
                 JSONArray array = obj.getJSONArray(key);
                 for (int i = 0; i < array.size(); i++) {
-                    escapeXml(array.getJSONObject(i));
+                    Object o = array.get(i);
+                    if (o instanceof String || o.getClass().isPrimitive()) {
+                        String content = String.valueOf(o);
+                        array.set(i, escapeString(content));
+                    } else {
+                        escapeXml(array.getJSONObject(i));
+                    }
                 }
             }
         }