From 546d3fcba73b772446b4cf4654c3b3a995244bce Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期三, 26 十一月 2025 09:09:24 +0800
Subject: [PATCH] 新增访问路径

---
 jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java |  109 +++++++++++++++++++++++++++++++-----------------------
 1 files changed, 62 insertions(+), 47 deletions(-)

diff --git a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
index 60f50c0..d5d668a 100644
--- a/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
+++ b/jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
@@ -2,6 +2,8 @@
 
 import cn.lihu.jh.module.ecg.dal.dataobject.appointment.AppointmentDO;
 import cn.lihu.jh.module.ecg.service.appointment.AppointmentService;
+import cn.lihu.jh.module.infra.dal.dataobject.config.ConfigDO;
+import cn.lihu.jh.module.infra.service.config.ConfigService;
 import freemarker.template.Configuration;
 import freemarker.template.Template;
 import io.swagger.v3.oas.annotations.Operation;
@@ -14,12 +16,9 @@
 import org.springframework.web.bind.annotation.RestController;
 
 import javax.annotation.Resource;
-import javax.servlet.http.HttpServletResponse;
 import java.io.*;
-import java.net.URLEncoder;
 import java.time.LocalDate;
 import java.time.Period;
-import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -32,6 +31,8 @@
     @Resource
     private AppointmentService appointmentService;
 
+    @Resource
+    private ConfigService configService;
 
     private static Configuration configuration = null;
 
@@ -42,79 +43,77 @@
 
     @Operation(summary = "鐭ユ儏鍚屾剰杩愬姩璇曢獙")
     @GetMapping(value = "/download/{id}")
-    public void downloadInfo(@PathVariable("id") Long id, HttpServletResponse response) {
+    public Map downloadInfo(@PathVariable("id") Long id) {
         log.info("鐭ユ儏鍚屾剰杩愬姩璇曢獙锛歿}", id);
-
         try {
-            Map<String, Object> dataMap = new HashMap<>();
+            Map dataMap = new HashMap();
             AppointmentDO appointment = appointmentService.getAppointment(id);
             getData(dataMap, appointment);
-
             String filePath = getClass().getResource("/template/").getPath();
+            System.out.println(filePath);
+            //璁剧疆妯℃湰瑁呯疆鏂规硶鍜岃矾寰�,FreeMarker鏀寔澶氱妯℃澘瑁呰浇鏂规硶銆傚彲浠ラ噸servlet锛宑lasspath锛屾暟鎹簱鏁欑▼瑁呰浇锛�
             configuration.setDirectoryForTemplateLoading(new File(filePath));
-            Template t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
+            Template t = null;
+            //鎹愮尞琛�.ftl涓鸿瑁呰浇鐨勬ā鏉�
+            t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
 
-            String timestamp = String.valueOf(System.currentTimeMillis());
-            String fileName = "鐭ユ儏鍚屾剰杩愬姩璇曢獙-" + appointment.getPatName() + "-" + timestamp + ".doc";
+            String time = String.valueOf(System.currentTimeMillis());
+            String name = "骞虫澘杩愬姩蹇冪數鍥剧煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + time;
 
-            // 璁剧疆鍝嶅簲澶达紝鍛婅瘔娴忚鍣ㄨ繘琛屼笅杞�
-            response.setCharacterEncoding("UTF-8");
-            response.setContentType("application/msword"); // Word鏂囨。绫诲瀷
-            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
-
-            // 鐩存帴杈撳嚭鍒皉esponse鐨勮緭鍑烘祦
-            Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
+            //杈撳嚭鏂囨。璺緞鍙婂悕绉�
+            ConfigDO configByKey = configService.getConfigByKey("file.upload.path");
+            createPath(configByKey.getValue());
+            File outFile = new File(configByKey.getValue() + name + ".doc");
+            Writer out = null;
+            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
             t.process(dataMap, out);
-            out.flush();
-            out.close();
 
+            Map<String, Object> map = new HashMap<>();
+            map.put("downloadUrl", "profile/" + name + ".doc");
+            map.put("downloadName", name + ".doc");
+            return map;
         } catch (Exception e) {
-            log.error("涓嬭浇鐭ユ儏鍚屾剰杩愬姩璇曢獙鏂囨。澶辫触", e);
-            try {
-                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "鏂囨。鐢熸垚澶辫触");
-            } catch (IOException ex) {
-                log.error("鍙戦�侀敊璇搷搴斿け璐�", ex);
-            }
+            e.printStackTrace();
         }
+        return null;
     }
 
 
     @Operation(summary = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功")
     @GetMapping(value = "/downloadesophagus/{id}")
-    public void downloadEsophagusInfo(@PathVariable("id") Long id, HttpServletResponse response) {
+    public Map downloadEsophagusInfo(@PathVariable("id") Long id) {
         log.info("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功锛歿}", id);
-
         try {
-            Map<String, Object> dataMap = new HashMap<>();
+            Map dataMap = new HashMap();
             AppointmentDO appointment = appointmentService.getAppointment(id);
             getData(dataMap, appointment);
-
             String filePath = getClass().getResource("/template/").getPath();
+            System.out.println(filePath);
+            //璁剧疆妯℃湰瑁呯疆鏂规硶鍜岃矾寰�,FreeMarker鏀寔澶氱妯℃澘瑁呰浇鏂规硶銆傚彲浠ラ噸servlet锛宑lasspath锛屾暟鎹簱鏁欑▼瑁呰浇锛�
             configuration.setDirectoryForTemplateLoading(new File(filePath));
-            Template t = configuration.getTemplate("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功.ftl");
+            Template t = null;
+            //鎹愮尞琛�.ftl涓鸿瑁呰浇鐨勬ā鏉�
+            t = configuration.getTemplate("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功.ftl");
 
-            String timestamp = String.valueOf(System.currentTimeMillis());
-            String fileName = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + timestamp + ".doc";
+            String time = String.valueOf(System.currentTimeMillis());
+            String name = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + time;
 
-            // 璁剧疆鍝嶅簲澶达紝鍛婅瘔娴忚鍣ㄨ繘琛屼笅杞�
-            response.setCharacterEncoding("UTF-8");
-            response.setContentType("application/msword"); // Word鏂囨。绫诲瀷
-            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
-
-            // 鐩存帴杈撳嚭鍒皉esponse鐨勮緭鍑烘祦
-            Writer out = new BufferedWriter(new OutputStreamWriter(response.getOutputStream(), "UTF-8"));
+            //杈撳嚭鏂囨。璺緞鍙婂悕绉�
+            ConfigDO configByKey = configService.getConfigByKey("file.upload.path");
+            createPath(configByKey.getValue());
+            File outFile = new File(configByKey.getValue() + name + ".doc");
+            Writer out = null;
+            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
             t.process(dataMap, out);
-            out.flush();
-            out.close();
 
+            Map<String, Object> map = new HashMap<>();
+            map.put("downloadUrl", "profile/" + name + ".doc");
+            map.put("downloadName", name + ".doc");
+            return map;
         } catch (Exception e) {
-            log.error("涓嬭浇椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功鏂囨。澶辫触", e);
-            try {
-                response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "鏂囨。鐢熸垚澶辫触");
-            } catch (IOException ex) {
-                log.error("鍙戦�侀敊璇搷搴斿け璐�", ex);
-            }
+            e.printStackTrace();
         }
+        return null;
     }
 
     private void getData(Map dataMap, AppointmentDO appointment) {
@@ -135,4 +134,20 @@
         return Period.between(birthdayStr, LocalDate.now()).getYears();
     }
 
+    public void createPath(String uploadPath) {
+        // 纭繚璺緞浠ユ枩鏉犵粨灏�
+        if (!uploadPath.endsWith("/") && !uploadPath.endsWith("\\")) {
+            uploadPath += "/";
+        }
+
+        // 鍒涘缓鐩綍锛堝鏋滀笉瀛樺湪锛�
+        File uploadDir = new File(uploadPath);
+        if (!uploadDir.exists()) {
+            boolean created = uploadDir.mkdirs();
+            if (created) {
+                log.info("鍒涘缓鐩綍鎴愬姛: {}", uploadPath);
+            }
+        }
+    }
+
 }

--
Gitblit v1.9.3