From ab21541dc210180d1fef5b4351e416ee5c5be0db Mon Sep 17 00:00:00 2001
From: liusheng <337615773@qq.com>
Date: 星期二, 25 十一月 2025 11:05:51 +0800
Subject: [PATCH] 修改代码位置
---
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java | 84 +++++++++++++++++++++++-------------------
1 files changed, 46 insertions(+), 38 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 cad5cc2..60f50c0 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
@@ -14,7 +14,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;
@@ -40,73 +42,79 @@
@Operation(summary = "鐭ユ儏鍚屾剰杩愬姩璇曢獙")
@GetMapping(value = "/download/{id}")
- public Map downloadInfo(@PathVariable("id") Long id) {
+ public void downloadInfo(@PathVariable("id") Long id, HttpServletResponse response) {
log.info("鐭ユ儏鍚屾剰杩愬姩璇曢獙锛歿}", id);
+
try {
- Map dataMap = new HashMap();
+ Map<String, Object> 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 = null;
- //鎹愮尞琛�.ftl涓鸿瑁呰浇鐨勬ā鏉�
- t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
+ Template t = configuration.getTemplate("鐭ユ儏鍚屾剰杩愬姩璇曢獙.ftl");
- String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
- String name = "鐭ユ儏鍚屾剰杩愬姩璇曢獙-" + appointment.getPatName() + "-" + year;
+ String timestamp = String.valueOf(System.currentTimeMillis());
+ String fileName = "鐭ユ儏鍚屾剰杩愬姩璇曢獙-" + appointment.getPatName() + "-" + timestamp + ".doc";
- //杈撳嚭鏂囨。璺緞鍙婂悕绉�
- File outFile = new File("D:/" + name + ".doc");
- Writer out = null;
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
+ // 璁剧疆鍝嶅簲澶达紝鍛婅瘔娴忚鍣ㄨ繘琛屼笅杞�
+ 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"));
t.process(dataMap, out);
+ out.flush();
+ out.close();
- Map<String, Object> map = new HashMap<>();
- map.put("downloadUrl", "D:/" + name + ".doc");
- map.put("downloadName", name + ".doc");
- return map;
} catch (Exception e) {
- e.printStackTrace();
+ log.error("涓嬭浇鐭ユ儏鍚屾剰杩愬姩璇曢獙鏂囨。澶辫触", e);
+ try {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "鏂囨。鐢熸垚澶辫触");
+ } catch (IOException ex) {
+ log.error("鍙戦�侀敊璇搷搴斿け璐�", ex);
+ }
}
- return null;
}
@Operation(summary = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功")
@GetMapping(value = "/downloadesophagus/{id}")
- public Map downloadEsophagusInfo(@PathVariable("id") Long id) {
+ public void downloadEsophagusInfo(@PathVariable("id") Long id, HttpServletResponse response) {
log.info("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功锛歿}", id);
+
try {
- Map dataMap = new HashMap();
+ Map<String, Object> 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 = null;
- //鎹愮尞琛�.ftl涓鸿瑁呰浇鐨勬ā鏉�
- t = configuration.getTemplate("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功.ftl");
+ Template t = configuration.getTemplate("椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功.ftl");
- String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
- String name = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + year;
+ String timestamp = String.valueOf(System.currentTimeMillis());
+ String fileName = "椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功-" + appointment.getPatName() + "-" + timestamp + ".doc";
- //杈撳嚭鏂囨。璺緞鍙婂悕绉�
- File outFile = new File("D:/" + name + ".doc");
- Writer out = null;
- out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"));
+ // 璁剧疆鍝嶅簲澶达紝鍛婅瘔娴忚鍣ㄨ繘琛屼笅杞�
+ 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"));
t.process(dataMap, out);
+ out.flush();
+ out.close();
- Map<String, Object> map = new HashMap<>();
- map.put("downloadUrl", "D:/" + name + ".doc");
- map.put("downloadName", name + ".doc");
- return map;
} catch (Exception e) {
- e.printStackTrace();
+ log.error("涓嬭浇椋熺蹇冭剰鐢电敓鐞嗙煡鎯呭悓鎰忎功鏂囨。澶辫触", e);
+ try {
+ response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "鏂囨。鐢熸垚澶辫触");
+ } catch (IOException ex) {
+ log.error("鍙戦�侀敊璇搷搴斿け璐�", ex);
+ }
}
- return null;
}
private void getData(Map dataMap, AppointmentDO appointment) {
--
Gitblit v1.9.3