liusheng
3 天以前 ab21541dc210180d1fef5b4351e416ee5c5be0db
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,classpath,数据库教程装载,
            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"));
            // 直接输出到response的输出流
            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,classpath,数据库教程装载,
            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"));
            // 直接输出到response的输出流
            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) {