Commit 4a2e0097 authored by wangqiang's avatar wangqiang

卷内目录生成pdf文件后,将临时目录及文件删除

parent a2a8e48a
...@@ -325,12 +325,13 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic ...@@ -325,12 +325,13 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic
public String convertPdf(String modelName, Map<String, Object> dataMap) { public String convertPdf(String modelName, Map<String, Object> dataMap) {
String targetPath = System.getProperty("user.dir") + "/TEMP/"; String targetPath = System.getProperty("user.dir") + "/TEMP/";
File wordFile = new File(targetPath + modelName); //File wordFile = new File(targetPath + modelName);
String tempFullPath = targetPath + modelName;
File pdfFile2 = new File("TEMP/" + dataMap.get("contextName") + "案卷目录" +"("+dataMap.get("mj") +")"+ ".pdf"); File pdfFile2 = new File("TEMP/" + dataMap.get("contextName") + "案卷目录" +"("+dataMap.get("mj") +")"+ ".pdf");
try { try {
InputStream tempStream2 = Files.newInputStream(wordFile.toPath()); //InputStream tempStream2 = Files.newInputStream(wordFile.toPath());
OutputStream outputStream = Files.newOutputStream(pdfFile2.toPath()); OutputStream outputStream = Files.newOutputStream(pdfFile2.toPath());
byte[] bytes2 = WordUtils.fillWord(dataMap, tempStream2); byte[] bytes2 = WordUtils.fillWord(dataMap, tempFullPath);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes2); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes2);
WordUtils.wordToPDF(byteArrayInputStream, outputStream); WordUtils.wordToPDF(byteArrayInputStream, outputStream);
byteArrayInputStream.close(); byteArrayInputStream.close();
...@@ -342,29 +343,25 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic ...@@ -342,29 +343,25 @@ public class ExtFilesServiceImpl<V extends ExtFilesVO> implements ExtFilesServic
public String fillWordAndConvertPdf(String modelName, Map<String, Object> dataMap, String pdfFileName,String tempDir) { public String fillWordAndConvertPdf(String modelName, Map<String, Object> dataMap, String pdfFileName,String tempDir) {
String targetPath = System.getProperty("user.dir") + "/TEMP/"; String targetPath = System.getProperty("user.dir") + "/TEMP/";
File wordFile = new File(targetPath + modelName); String tempFullPath = targetPath + modelName;
File pdfFile2 = new File(tempDir + File.separator+pdfFileName + ".pdf"); File pdfFile = new File(tempDir + File.separator+pdfFileName + ".pdf");
InputStream tempStream = null;
OutputStream outputStream = null;
ByteArrayInputStream byteArrayInputStream = null; ByteArrayInputStream byteArrayInputStream = null;
try { try(OutputStream outputStream = new FileOutputStream(pdfFile)) {
tempStream = Files.newInputStream(wordFile.toPath()); byte[] wordBytes = WordUtils.fillWord(dataMap, tempFullPath);
outputStream = Files.newOutputStream(pdfFile2.toPath()); byteArrayInputStream = new ByteArrayInputStream(wordBytes);
byte[] bytes2 = WordUtils.fillWord(dataMap, tempStream);
byteArrayInputStream = new ByteArrayInputStream(bytes2);
WordUtils.wordToPDF(byteArrayInputStream, outputStream); WordUtils.wordToPDF(byteArrayInputStream, outputStream);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
}finally { }finally {
try { if(byteArrayInputStream != null){
byteArrayInputStream.close(); try {
tempStream.close(); byteArrayInputStream.close();
outputStream.close(); } catch (IOException e) {
} catch (IOException e) { throw new RuntimeException(e);
throw new RuntimeException(e); }
} }
} }
return pdfFile2.getAbsolutePath(); return pdfFile.getAbsolutePath();
} }
......
...@@ -308,11 +308,11 @@ public class WordUtils { ...@@ -308,11 +308,11 @@ public class WordUtils {
* 填充word模板,获取填充后的word文件流: * 填充word模板,获取填充后的word文件流:
* *
* @param datas: 填充数据 * @param datas: 填充数据
* @param fileStream: 文件模板流 -- 可从OSS/FastDFS上获取流,直接传入; * @param tempPath: 模板地址
* @return * @return
* @throws IOException * @throws IOException
*/ */
public static byte[] fillWord(Map<String, Object> datas, InputStream fileStream) throws IOException { public static byte[] fillWord(Map<String, Object> datas, String tempPath) throws IOException {
XWPFTemplate template = null; XWPFTemplate template = null;
ByteArrayOutputStream wordOut = new ByteArrayOutputStream(1024); ByteArrayOutputStream wordOut = new ByteArrayOutputStream(1024);
try { try {
...@@ -324,7 +324,7 @@ public class WordUtils { ...@@ -324,7 +324,7 @@ public class WordUtils {
.build(); .build();
//填充文本域: //填充文本域:
template = XWPFTemplate.compile(fileStream, config).render(datas); template = XWPFTemplate.compile(tempPath, config).render(datas);
template.write(wordOut); template.write(wordOut);
} catch (RenderException ex) { } catch (RenderException ex) {
log.error("模板填充失败,请保证模板文本域与参数保持一致。错误信息::{}", ex); log.error("模板填充失败,请保证模板文本域与参数保持一致。错误信息::{}", ex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment