From 041ad7979f71247ad0ca46814317a3fdc33d8376 Mon Sep 17 00:00:00 2001 From: wangxianzhang Date: Tue, 15 Mar 2022 21:19:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9:=E8=A7=A3=E5=86=B3jar?= =?UTF-8?q?=E5=8C=85=E5=86=85=E6=96=87=E4=BB=B6=E6=97=A0=E6=B3=95=E8=AF=BB?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/MemoConcernController.java | 64 ++++++++++++++++-- .../controller/MemoDifficultyController.java | 65 +++++++++++++++++-- .../controller/MemoWorkDiaryController.java | 64 ++++++++++++++++-- 3 files changed, 179 insertions(+), 14 deletions(-) diff --git a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoConcernController.java b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoConcernController.java index c9eef29e73..aee3d541bf 100644 --- a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoConcernController.java +++ b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoConcernController.java @@ -6,6 +6,7 @@ import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.aop.NoRepeatSubmit; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetException; +import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.DateUtils; @@ -18,6 +19,8 @@ import com.epmet.commons.tools.validator.group.UpdateGroup; import com.epmet.dto.MemoConcernDTO; import com.epmet.dto.form.MemoConcernFormDTO; import com.epmet.service.MemoConcernService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -25,7 +28,14 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; @@ -35,6 +45,7 @@ import java.util.Map; * @author generator generator@elink-cn.com * @since v1.0.0 2022-03-15 */ +@Slf4j @RestController @RequestMapping("memoConcern") public class MemoConcernController { @@ -87,7 +98,7 @@ public class MemoConcernController { @PostMapping("/{id}/exportWord") public void exportWord(@PathVariable("id") String id, HttpServletResponse response) throws Exception { - URL resource = this.getClass().getClassLoader().getResource("templates/memo_concern_export_template.docx"); + String templateFilePath = loadTemplate("memo_concern_export_template.docx"); MemoConcernFormDTO formDTO = new MemoConcernFormDTO(); formDTO.setId(id); MemoConcernDTO data = memoConcernService.get(formDTO); @@ -117,10 +128,9 @@ public class MemoConcernController { //状态 map.put("statusName", "0".equals(data.getStatus()) ? "未完成" : "已完成"); - XWPFDocument doc = WordExportUtil.exportWord07(resource.getFile(), map); + XWPFDocument doc = WordExportUtil.exportWord07(templateFilePath, map); - String filePath = resource.getFile(); - String suffix = filePath.substring(filePath.lastIndexOf(".")); + String suffix = templateFilePath.substring(templateFilePath.lastIndexOf(".")); response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); @@ -137,5 +147,51 @@ public class MemoConcernController { } } + /** + * 加载模板 + * @param templateFileName + * @return + * @throws IOException + */ + private String loadTemplate(String templateFileName) throws IOException { + String homeDir = System.getProperty("user.home"); + Path templates = Paths.get(homeDir, "epmet_files", "templates"); + if (Files.notExists(templates)) { + Files.createDirectory(templates); + } + + Path templateFilePath = templates.resolve(templateFileName); + String templateFilePathStr = templateFilePath.toString(); + if (Files.exists(templateFilePath)) { + return templateFilePathStr; + } + + // 将项目中的模板拷贝至用户家目录中 + OutputStream os = null; + InputStream is = null; + try { + is = this.getClass().getClassLoader().getResourceAsStream("templates/" + templateFileName); + os = new FileOutputStream(templateFilePathStr); + IOUtils.copy(is, os); + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输入流出错:{}", errorMsg); + } + try { + if (os != null) { + os.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输出流出错:{}", errorMsg); + } + } + return templateFilePathStr; + } } diff --git a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoDifficultyController.java b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoDifficultyController.java index e3f855b8e5..149d043b8d 100644 --- a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoDifficultyController.java +++ b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoDifficultyController.java @@ -6,6 +6,7 @@ import cn.hutool.core.bean.BeanUtil; import com.epmet.commons.tools.aop.NoRepeatSubmit; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetException; +import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.security.dto.TokenDto; @@ -21,6 +22,8 @@ import com.epmet.dto.form.AddMemoDifficultyFromDTO; import com.epmet.dto.form.MemoDifficultyDetailFromDTO; import com.epmet.dto.form.MemoDifficultyFormDTO; import com.epmet.service.MemoDifficultyService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -28,7 +31,14 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Map; @@ -38,6 +48,7 @@ import java.util.Map; * @author generator generator@elink-cn.com * @since v1.0.0 2022-03-15 */ +@Slf4j @RestController @RequestMapping("memoDifficulty") public class MemoDifficultyController { @@ -90,8 +101,7 @@ public class MemoDifficultyController { @PostMapping("/{id}/exportWord") public void exportWord(@PathVariable("id") String id, HttpServletResponse response) throws Exception { - URL resource = this.getClass().getClassLoader().getResource("templates/memo_difficulty_export_template.docx"); - + String templateFilePath = loadTemplate("memo_difficulty_export_template.docx"); MemoDifficultyDTO data = memoDifficultyService.get(id); if (data == null) { @@ -106,10 +116,9 @@ public class MemoDifficultyController { String scheduledTimeStr = DateUtils.format(data.getScheduledTime(), "yyyy年MM月dd日 HH:mm"); map.put("scheduledTime", scheduledTimeStr); - XWPFDocument doc = WordExportUtil.exportWord07(resource.getFile(), map); + XWPFDocument doc = WordExportUtil.exportWord07(templateFilePath, map); - String filePath = resource.getFile(); - String suffix = filePath.substring(filePath.lastIndexOf(".")); + String suffix = templateFilePath.substring(templateFilePath.lastIndexOf(".")); response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); @@ -126,5 +135,51 @@ public class MemoDifficultyController { } } + /** + * 加载模板 + * @param templateFileName + * @return + * @throws IOException + */ + private String loadTemplate(String templateFileName) throws IOException { + String homeDir = System.getProperty("user.home"); + Path templates = Paths.get(homeDir, "epmet_files", "templates"); + if (Files.notExists(templates)) { + Files.createDirectory(templates); + } + + Path templateFilePath = templates.resolve(templateFileName); + String templateFilePathStr = templateFilePath.toString(); + if (Files.exists(templateFilePath)) { + return templateFilePathStr; + } + + // 将项目中的模板拷贝至用户家目录中 + OutputStream os = null; + InputStream is = null; + try { + is = this.getClass().getClassLoader().getResourceAsStream("templates/" + templateFileName); + os = new FileOutputStream(templateFilePathStr); + IOUtils.copy(is, os); + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输入流出错:{}", errorMsg); + } + try { + if (os != null) { + os.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输出流出错:{}", errorMsg); + } + } + return templateFilePathStr; + } } diff --git a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoWorkDiaryController.java b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoWorkDiaryController.java index 73ce22ac03..547983cf39 100644 --- a/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoWorkDiaryController.java +++ b/epmet-module/gov-project/gov-project-server/src/main/java/com/epmet/controller/MemoWorkDiaryController.java @@ -4,6 +4,7 @@ import cn.afterturn.easypoi.word.WordExportUtil; import cn.hutool.core.bean.BeanUtil; import com.epmet.commons.tools.annotation.LoginUser; import com.epmet.commons.tools.aop.NoRepeatSubmit; +import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.page.PageData; import com.epmet.commons.tools.utils.DateUtils; import com.epmet.commons.tools.security.dto.TokenDto; @@ -16,6 +17,8 @@ import com.epmet.commons.tools.validator.group.UpdateGroup; import com.epmet.dto.MemoWorkDiaryDTO; import com.epmet.dto.form.MemoWorkDiaryFormDTO; import com.epmet.service.MemoWorkDiaryService; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.io.IOUtils; import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; @@ -24,8 +27,13 @@ import org.springframework.web.bind.annotation.*; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import java.io.FileOutputStream; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.HashMap; import java.util.Map; import java.util.Optional; @@ -37,6 +45,7 @@ import java.util.Optional; * @author generator generator@elink-cn.com * @since v1.0.0 2022-03-15 */ +@Slf4j @RestController @RequestMapping("memoWorkDiary") public class MemoWorkDiaryController { @@ -88,8 +97,7 @@ public class MemoWorkDiaryController { @PostMapping("/{id}/exportWord") public void exportWord(@PathVariable("id") String id, HttpServletResponse response) throws Exception { - URL resource = this.getClass().getClassLoader().getResource("templates/memo_work_diary_export_template.docx"); - + String templateFilePath = loadTemplate("memo_work_diary_export_template.docx"); MemoWorkDiaryFormDTO form = new MemoWorkDiaryFormDTO(); form.setId(id); MemoWorkDiaryDTO data = memoWorkDiaryService.get(form); @@ -98,10 +106,9 @@ public class MemoWorkDiaryController { String createTimeStr = DateUtils.format(data.getCreatedTime(), "yyyy年MM月dd日 HH:mm"); map.put("createTime", createTimeStr); - XWPFDocument doc = WordExportUtil.exportWord07(resource.getFile(), map); + XWPFDocument doc = WordExportUtil.exportWord07(templateFilePath, map); - String filePath = resource.getFile(); - String suffix = filePath.substring(filePath.lastIndexOf(".")); + String suffix = templateFilePath.substring(templateFilePath.lastIndexOf(".")); response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, "Content-Disposition"); response.setHeader(HttpHeaders.CONTENT_TYPE, "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); @@ -117,4 +124,51 @@ public class MemoWorkDiaryController { } } } + + /** + * 加载模板 + * @param templateFileName + * @return + * @throws IOException + */ + private String loadTemplate(String templateFileName) throws IOException { + String homeDir = System.getProperty("user.home"); + Path templates = Paths.get(homeDir, "epmet_files", "templates"); + if (Files.notExists(templates)) { + Files.createDirectory(templates); + } + + Path templateFilePath = templates.resolve(templateFileName); + String templateFilePathStr = templateFilePath.toString(); + if (Files.exists(templateFilePath)) { + return templateFilePathStr; + } + + // 将项目中的模板拷贝至用户家目录中 + OutputStream os = null; + InputStream is = null; + try { + is = this.getClass().getClassLoader().getResourceAsStream("templates/" + templateFileName); + os = new FileOutputStream(templateFilePathStr); + IOUtils.copy(is, os); + } finally { + try { + if (is != null) { + is.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输入流出错:{}", errorMsg); + } + try { + if (os != null) { + os.close(); + } + } catch (IOException e) { + String errorMsg = ExceptionUtils.getThrowableErrorStackTrace(e); + log.error("【导出工作日志doc】关闭输出流出错:{}", errorMsg); + } + } + return templateFilePathStr; + } }