From c8d8a511f45c96ed3a5123a88e48de2ffdbf632a Mon Sep 17 00:00:00 2001 From: jinlin <jinlin> Date: 星期日, 03 十二月 2023 20:00:47 +0800 Subject: [PATCH] 6 --- core/src/main/java/com/zt/life/export/service/WordFileService.java | 47 ++++++++++++++++++++++++++++++++--------------- 1 files changed, 32 insertions(+), 15 deletions(-) diff --git a/core/src/main/java/com/zt/life/export/service/WordFileService.java b/core/src/main/java/com/zt/life/export/service/WordFileService.java index 642234f..0576d03 100644 --- a/core/src/main/java/com/zt/life/export/service/WordFileService.java +++ b/core/src/main/java/com/zt/life/export/service/WordFileService.java @@ -8,6 +8,9 @@ import com.deepoove.poi.policy.HackLoopTableRenderPolicy; import com.zt.life.export.dto.WordFile; +import com.zt.life.oss.OssEncryptService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -19,16 +22,23 @@ @Service public class WordFileService { - public void exportWordFile(HttpServletRequest request, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException { + @Value("${zt.oss.local-path}") + private String localPath; + + @Autowired + private OssEncryptService ossEncryptService; + + + public void exportWordFile(HttpServletRequest request, Object dataObj, WordFile wordFile, HttpServletResponse response) throws UnsupportedEncodingException, FileNotFoundException { // 鏁版嵁map闆嗗悎 - Map<String, Object> data = new HashMap<>(); + Map<String, Object> wordData = new HashMap<>(); //HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy(); Configure config = null; ConfigureBuilder builder = Configure.newBuilder(); - this.formatWordData(wordFile, data, builder); + this.formatWordData(dataObj, wordData, builder); config = builder.build(); - File fl = new File(wordFile.getModulePath()); + File fl = new File(localPath + "/template/" + wordFile.getModulePath()); FileInputStream fs = new FileInputStream(fl); XWPFTemplate template; if (config != null) { @@ -36,32 +46,39 @@ } else { template = XWPFTemplate.compile(fs); } - template.render(data); + template.render(wordData); DownloadService.download(request, response, template, wordFile.getWordName()); } - public void formatWordData(Object obj, Map<String, Object> data, ConfigureBuilder builder) { - Field[] fields = ReflectUtil.getFields(obj.getClass()); + public void formatWordData(Object dataObj, Map<String, Object> wordData, ConfigureBuilder builder) { + Field[] fields = ReflectUtil.getFields(dataObj.getClass()); for (Field field : fields) { field.setAccessible(true); String fieldName = field.getName(); String typeName = field.getType().getName(); - Object staticFieldValue = ReflectUtil.getFieldValue(obj, field.getName()); // 灞炴�у搴旂殑鏁版嵁 + Object staticFieldValue = ReflectUtil.getFieldValue(dataObj, field.getName()); // 灞炴�у搴旂殑鏁版嵁 if (typeName.contains("com.zt.life.modules")) { - formatWordData(staticFieldValue, data, builder); + formatWordData(staticFieldValue, wordData, builder); } else { if (field.getType().getName().equals("java.util.List")) { builder.bind(fieldName, new HackLoopTableRenderPolicy()); } else if (staticFieldValue != null) { String valStr = staticFieldValue.toString(); - if (valStr.contains("绛惧悕鍥剧墖:")) { - valStr = valStr.replace("绛惧悕鍥剧墖:", ""); - staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr); - /*put("localbyte", new PictureRenderData(80, 100, ".png", new FileInputStream("./logo.png")));*/ + if (valStr.contains("鏂囦欢鍥剧墖:")) { + valStr = valStr.replace("鏂囦欢鍥剧墖:", ""); + File file = new File(valStr); + if (file.exists()) { + try (InputStream in = ossEncryptService.decrypt(file)) { + staticFieldValue = new PictureRenderData(500, 500, ".png", in); + /*staticFieldValue = new PictureRenderData(80, 100, "d://" + valStr);*/ + } catch (IOException e) { + e.printStackTrace(); + } + } + if (wordData.get(fieldName) == null) + wordData.put(fieldName, staticFieldValue); } } - if (data.get(fieldName) == null) - data.put(fieldName, staticFieldValue); } } } -- Gitblit v1.9.1