Commit 5b6c6498 authored by 韩桐桐's avatar 韩桐桐

fix(jg):模板填充字段特殊字符转义

parent 12a84b09
......@@ -63,7 +63,7 @@ public class WordTemplateUtils {
// 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
Writer writer = new OutputStreamWriter(Files.newOutputStream(docFile.toPath()), StandardCharsets.UTF_8)
) {
template.process(dataMap, writer);
template.process(escapeSpecialCharacters(dataMap), writer);
}
return docFile;
}
......@@ -226,4 +226,34 @@ public class WordTemplateUtils {
return instance.fillAndConvertDocFile(wordPath, pdfName, placeholders, SaveFormat.PDF);
}
/**
* 下载填充模板的字段,特殊符号转义
*/
private static Map<String, Object> escapeSpecialCharacters(Map<String, ?> inputMap) {
Map<String, Object> escapedMap = new HashMap<>();
for (Map.Entry<String, ?> entry : inputMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if (value instanceof String) {
escapedMap.put(key, escapeValue((String) value));
} else if (value instanceof Map) {
escapedMap.put(key, escapeSpecialCharacters((Map<String, Object>) value));
} else {
escapedMap.put(key, value);
}
}
return escapedMap;
}
private static String escapeValue(String value) {
if (value == null) {
return null;
}
return value.replace("&", "&amp;")
.replace("<", "&lt;")
.replace(">", "&gt;")
.replace("\"", "&quot;")
.replace("'", "&apos;");
}
}
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