From 900389160fd0b05020de9576d3321a6d82016908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AB=98=E9=9B=84?= Date: Fri, 12 May 2023 01:24:10 +0000 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96PDF=E5=8A=A0=E5=AF=86?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E7=BC=93=E5=AD=98=E6=96=B9=E6=B3=95=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96PDF=E5=8A=A0=E5=AF=86=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=BC=93=E5=AD=98=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 高雄 --- .../cn/keking/service/FileHandlerService.java | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index 393c2389..9c3080f0 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/server/src/main/java/cn/keking/service/FileHandlerService.java @@ -15,12 +15,13 @@ import com.aspose.cad.Image; import com.aspose.cad.LoadOptions; import com.aspose.cad.imageoptions.CadRasterizationOptions; import com.aspose.cad.imageoptions.PdfOptions; -import com.itextpdf.text.exceptions.BadPasswordException; import com.itextpdf.text.pdf.PdfReader; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.rendering.ImageType; import org.apache.pdfbox.rendering.PDFRenderer; import org.apache.pdfbox.tools.imageio.ImageIOUtil; +import org.apache.poi.EncryptedDocumentException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -50,6 +51,7 @@ public class FileHandlerService { private final String fileDir = ConfigConstants.getFileDir(); private final static String pdf2jpg_image_format = ".jpg"; private final CacheService cacheService; + private static final String pdf_password_msg = "password"; @Value("${server.tomcat.uri-encoding:UTF-8}") private String uriEncoding; @@ -223,6 +225,7 @@ public class FileHandlerService { public List pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception { boolean forceUpdatedCache = fileAttribute.forceUpdatedCache(); String filePassword = fileAttribute.getFilePassword(); + String pdfPassword = null; PDDocument doc = null; PdfReader pdfReader = null; if (!forceUpdatedCache) { @@ -256,18 +259,25 @@ public class FileHandlerService { imageUrls.add(imageUrl); } try { - pdfReader = new PdfReader(pdfFilePath); //判断pdf文件是否加密 缓存不加密文件 - this.addPdf2jpgCache(pdfFilePath, pageCount); - } catch (BadPasswordException e) { - - } catch (Exception e) { - e.printStackTrace(); + pdfReader = new PdfReader(pdfFilePath); //读取PDF文件 + } catch (Exception e) { //获取异常方法 判断是否有加密字符串 + Throwable[] throwableArray = ExceptionUtils.getThrowables(e); + for (Throwable throwable : throwableArray) { + if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) { + if (e.getMessage().toLowerCase().contains(pdf_password_msg)) { + pdfPassword = pdf_password_msg; + } + } + } + logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e); }finally { if (pdfReader != null) { //关闭 pdfReader.close(); } } - + if(!pdfPassword.equals(pdf_password_msg)){ //判断是否加密文件 加密文件不缓存 + this.addPdf2jpgCache(pdfFilePath, pageCount); + } } catch (IOException e) { logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e); throw new Exception(e);