diff --git a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java index b949a753..fe7ac1a7 100644 --- a/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java +++ b/detection/src/main/java/com/njcn/gather/report/controller/ReportController.java @@ -12,6 +12,8 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; +import javax.servlet.http.HttpServletResponse; + /** * @author hongawen @@ -36,10 +38,10 @@ public class ReportController extends BaseController { @PostMapping("/generateReport") @ApiOperation("生成测试报告") @ApiImplicitParam(name = "param", value = "查询参数", required = true) - public void generateReport(@RequestBody DevReportParam devReportParam) { + public void generateReport(@RequestBody DevReportParam devReportParam, HttpServletResponse response) { String methodDescribe = getMethodDescribe("list"); LogUtil.njcnDebug(log, "{},终端参数为:{}", methodDescribe, devReportParam); - reportService.generateReport(devReportParam); + reportService.generateReport(devReportParam,response); } diff --git a/detection/src/main/java/com/njcn/gather/report/service/IReportService.java b/detection/src/main/java/com/njcn/gather/report/service/IReportService.java index b8476987..233ca495 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/IReportService.java +++ b/detection/src/main/java/com/njcn/gather/report/service/IReportService.java @@ -2,11 +2,13 @@ package com.njcn.gather.report.service; import com.njcn.gather.report.pojo.DevReportParam; +import javax.servlet.http.HttpServletResponse; + /** * @author hongawen * @version 1.0 * @data 2025/1/9 20:59 */ public interface IReportService { - void generateReport(DevReportParam devReportParam); + void generateReport(DevReportParam devReportParam, HttpServletResponse response); } diff --git a/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java b/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java index d0fe56ec..a2e44add 100644 --- a/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java +++ b/detection/src/main/java/com/njcn/gather/report/service/impl/ReportServiceImpl.java @@ -35,6 +35,7 @@ import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; import org.springframework.stereotype.Service; +import javax.servlet.http.HttpServletResponse; import java.io.*; import java.math.BigDecimal; import java.math.RoundingMode; @@ -66,7 +67,7 @@ public class ReportServiceImpl implements IReportService { @Override - public void generateReport(DevReportParam devReportParam) { + public void generateReport(DevReportParam devReportParam, HttpServletResponse response) { // 读取模板文件 Resource baseModel = new ClassPathResource("model/BaseModel.docx"); try (FileInputStream baseModelFis = new FileInputStream(baseModel.getFile())) { @@ -77,15 +78,23 @@ public class ReportServiceImpl implements IReportService { dealBaseModel(baseModelDocument, devReportParam); // 处理数据页中的信息 dealDataModel(baseModelDocument, devReportParam); - //最终文件输出的路径 - FileOutputStream out = new FileOutputStream("C:\\Users\\hongawen\\Desktop\\testModel\\BaseDataModel" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx"); - // 4. 保存新的Word文档 - try { - baseModelDocument.write(out); - } catch (IOException e) { - throw new BusinessException("生成报告文件失败"); - } - out.close(); +// //最终文件输出的路径 +// FileOutputStream out = new FileOutputStream("C:\\Users\\hongawen\\Desktop\\testModel\\BaseDataModel" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx"); +// // 4. 保存新的Word文档 +// try { +// baseModelDocument.write(out); +// } catch (IOException e) { +// throw new BusinessException("生成报告文件失败"); +// } +// out.close(); + + // 设置响应头 + response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); + String fileName = "调试报告" + DateUtil.format(new Date(), DatePattern.CHINESE_DATE_TIME_PATTERN) + ".docx"; + response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); + // 保存新的Word文档到响应输出流 + baseModelDocument.write(response.getOutputStream()); + response.getOutputStream().flush(); System.out.println("报告生成成功!"); } catch (Exception e) { throw new RuntimeException(e);