diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index 9c3080f0..a7080cde 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/server/src/main/java/cn/keking/service/FileHandlerService.java @@ -296,7 +296,7 @@ public class FileHandlerService { * @param outputFilePath pdf输出文件路径 * @return 转换是否成功 */ - public boolean cadToPdf(String inputFilePath, String outputFilePath) { + public String cadToPdf(String inputFilePath, String outputFilePath) throws Exception { File outputFile = new File(outputFilePath); LoadOptions opts = new LoadOptions(); opts.setSpecifiedEncoding(CodePages.SimpChinese); @@ -310,21 +310,22 @@ public class FileHandlerService { cadRasterizationOptions.setDrawType(1); PdfOptions pdfOptions = new PdfOptions(); pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions); - OutputStream stream; + OutputStream stream = null; try { stream = new FileOutputStream(outputFile); cadImage.save(stream, pdfOptions); - stream.close(); - cadImage.close(); - return true; } catch (IOException e) { logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e); + return "null"; } finally { + if (stream != null) { //关闭 + stream.close(); + } if (cadImage != null) { //关闭 cadImage.close(); } } - return false; + return "true"; } /** * diff --git a/server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java index 7d08d282..2c59b956 100644 --- a/server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java @@ -12,6 +12,8 @@ import org.springframework.stereotype.Service; import org.springframework.ui.Model; import org.springframework.util.StringUtils; +import java.util.List; + import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType; /** @@ -51,10 +53,15 @@ public class CadFilePreviewImpl implements FilePreview { return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg()); } filePath = response.getContent(); + String imageUrls = null; if (StringUtils.hasText(outFilePath)) { - boolean convertResult = fileHandlerService.cadToPdf(filePath, outFilePath); - if (!convertResult) { - return otherFilePreview.notSupportedFile(model, fileAttribute, "cad文件转换异常,请联系管理员"); + try { + imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath); + } catch (Exception e) { + e.printStackTrace(); + } + if (imageUrls == null ) { + return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员"); } //是否保留CAD源文件 if( ConfigConstants.getDeleteSourceFile()) {