liusheng
2 天以前 a1e36fbec24cd7baaae4305b20d95cb4c3e3ef46
新增访问路径
已修改1个文件
108 ■■■■■ 文件已修改
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java 108 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
jh-module-ecg/jh-module-ecg-biz/src/main/java/cn/lihu/jh/module/ecg/controller/admin/freemark/TemplateCreateController.java
@@ -2,10 +2,14 @@
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 com.thoughtworks.xstream.core.BaseException;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
@@ -32,6 +36,8 @@
    @Resource
    private AppointmentService appointmentService;
    @Resource
    private ConfigService configService;
    private static Configuration configuration = null;
@@ -42,79 +48,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,classpath,数据库教程装载,
            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"));
            // 直接输出到response的输出流
            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,classpath,数据库教程装载,
            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"));
            // 直接输出到response的输出流
            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 +139,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);
            }
        }
    }
}