!139 优化CAD 释放资源方法

Merge pull request !139 from 高雄/master
This commit is contained in:
kailing
2023-05-12 02:14:58 +00:00
committed by Gitee
2 changed files with 17 additions and 9 deletions

View File

@ -296,7 +296,7 @@ public class FileHandlerService {
* @param outputFilePath pdf输出文件路径 * @param outputFilePath pdf输出文件路径
* @return 转换是否成功 * @return 转换是否成功
*/ */
public boolean cadToPdf(String inputFilePath, String outputFilePath) { public String cadToPdf(String inputFilePath, String outputFilePath) throws Exception {
File outputFile = new File(outputFilePath); File outputFile = new File(outputFilePath);
LoadOptions opts = new LoadOptions(); LoadOptions opts = new LoadOptions();
opts.setSpecifiedEncoding(CodePages.SimpChinese); opts.setSpecifiedEncoding(CodePages.SimpChinese);
@ -310,21 +310,22 @@ public class FileHandlerService {
cadRasterizationOptions.setDrawType(1); cadRasterizationOptions.setDrawType(1);
PdfOptions pdfOptions = new PdfOptions(); PdfOptions pdfOptions = new PdfOptions();
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions); pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
OutputStream stream; OutputStream stream = null;
try { try {
stream = new FileOutputStream(outputFile); stream = new FileOutputStream(outputFile);
cadImage.save(stream, pdfOptions); cadImage.save(stream, pdfOptions);
stream.close();
cadImage.close();
return true;
} catch (IOException e) { } catch (IOException e) {
logger.error("PDFFileNotFoundExceptioninputFilePath{}", inputFilePath, e); logger.error("PDFFileNotFoundExceptioninputFilePath{}", inputFilePath, e);
return "null";
} finally { } finally {
if (stream != null) { //关闭
stream.close();
}
if (cadImage != null) { //关闭 if (cadImage != null) { //关闭
cadImage.close(); cadImage.close();
} }
} }
return false; return "true";
} }
/** /**
* *

View File

@ -12,6 +12,8 @@ import org.springframework.stereotype.Service;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import java.util.List;
import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType; import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
/** /**
@ -51,10 +53,15 @@ public class CadFilePreviewImpl implements FilePreview {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg()); return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
} }
filePath = response.getContent(); filePath = response.getContent();
String imageUrls = null;
if (StringUtils.hasText(outFilePath)) { if (StringUtils.hasText(outFilePath)) {
boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath); try {
if (!convertResult) { imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath);
return otherFilePreview.notSupportedFile(model, fileAttribute, "cad文件转换异常请联系管理员"); } catch (Exception e) {
e.printStackTrace();
}
if (imageUrls == null ) {
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常请联系管理员");
} }
//是否保留CAD源文件 //是否保留CAD源文件
if( ConfigConstants.getDeleteSourceFile()) { if( ConfigConstants.getDeleteSourceFile()) {