This commit is contained in:
wr
2023-04-26 20:19:53 +08:00
parent 499c920196
commit 12b91d0b42
2 changed files with 18 additions and 7 deletions

View File

@@ -3,6 +3,7 @@ package com.njcn.harmonic.utils;
import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.*;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@@ -20,12 +21,12 @@ public class WordUtil2 {
public void getWord(String path, Map<String, Object> params, String fileName, HttpServletResponse response) public void getWord(String path, Map<String, Object> params, String fileName, HttpServletResponse response)
throws Exception { throws Exception {
path = ClearPathUtil.cleanString(path); path = ClearPathUtil.cleanString(path);
File file = new File(path); // File file = new File(path);
InputStream inStream = null; InputStream inStream = null;
CustomXWPFDocument doc = null; CustomXWPFDocument doc = null;
//读取报告模板 //读取报告模板
try { try {
inStream = new FileInputStream(file); inStream = new ClassPathResource(path).getInputStream();;
doc = new CustomXWPFDocument(inStream); doc = new CustomXWPFDocument(inStream);
this.replaceInTable(doc, params); // 替换表格里面的变量 this.replaceInTable(doc, params); // 替换表格里面的变量
this.replaceInPara(doc, params); // 替换文本里面的变量 this.replaceInPara(doc, params); // 替换文本里面的变量

View File

@@ -28,6 +28,7 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
@@ -169,10 +170,13 @@ public class ExportModelController extends BaseController {
} else { } else {
rtfPath = "file/reportModel.docx"; rtfPath = "file/reportModel.docx";
} }
ClassPathResource rtfPathResource=null;
ClassPathResource picPathResource=null;
try { try {
rtfPath = getClass().getClassLoader().getResource(rtfPath).getPath(); rtfPathResource = new ClassPathResource(rtfPath);
picPath = getClass().getClassLoader().getResource(picPath).getPath(); picPathResource = new ClassPathResource(picPath);
// rtfPath = getClass().getClassLoader().getResource(rtfPath).getPath();
// picPath = getClass().getClassLoader().getResource(picPath).getPath();
rtfPath = rtfPath.replaceAll("%20", " "); rtfPath = rtfPath.replaceAll("%20", " ");
picPath = picPath.replaceAll("%20", " "); picPath = picPath.replaceAll("%20", " ");
} catch (Exception e1) { } catch (Exception e1) {
@@ -189,10 +193,15 @@ public class ExportModelController extends BaseController {
if (!tmpfile.exists()) { if (!tmpfile.exists()) {
return "pathfail"+rtfPath; return "pathfail"+rtfPath;
} }
// File tmpfile = new File(rtfPath);
// if (!tmpfile.exists()) {
// return "pathfail";
// }
InputStream ins = null; InputStream ins = null;
try { try {
ins = new FileInputStream(rtfPath); // ins = new FileInputStream(rtfPath);
ins = rtfPathResource.getInputStream();
if (null != ins) { if (null != ins) {
flagPath = true; flagPath = true;
} }
@@ -225,7 +234,8 @@ public class ExportModelController extends BaseController {
InputStream inStream = null; InputStream inStream = null;
byte[] data = null; byte[] data = null;
try { try {
inStream = new FileInputStream(new File(picPath)); // inStream = new FileInputStream(new File(picPath));
inStream = picPathResource.getInputStream();
data = new byte[inStream.available()]; data = new byte[inStream.available()];
inStream.read(data); inStream.read(data);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {