diff --git a/pom.xml b/pom.xml
index 3023f4c7..c4148ef5 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,8 @@
4.2.1-1.5.2
5.5.13.3
3.1
+ 23.1
+ 1.70
1.2
3.6
diff --git a/server/pom.xml b/server/pom.xml
index 45fdd83c..23dc67d5 100644
--- a/server/pom.xml
+++ b/server/pom.xml
@@ -193,7 +193,13 @@
com.aspose
aspose-cad
- 23.1
+ ${aspose-cad.version}
+
+
+
+ org.bouncycastle
+ bcprov-jdk15on
+ ${bcprov-jdk15on.version}
diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java
index 98b3bbe4..e19b3e73 100644
--- a/server/src/main/java/cn/keking/service/FileHandlerService.java
+++ b/server/src/main/java/cn/keking/service/FileHandlerService.java
@@ -15,6 +15,8 @@ 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.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
@@ -218,8 +220,11 @@ public class FileHandlerService {
* @param pdfName pdf文件名称
* @return 图片访问集合
*/
- public List pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) {
+ public List pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
+ String filePassword = fileAttribute.getFilePassword();
+ PDDocument doc = null;
+ PdfReader pdfReader = null;
if (!forceUpdatedCache) {
List cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName);
if (!CollectionUtils.isEmpty(cacheResult)) {
@@ -232,14 +237,12 @@ public class FileHandlerService {
if (!pdfFile.exists()) {
return null;
}
- PDDocument doc = PDDocument.load(pdfFile);
+ doc = PDDocument.load(pdfFile,filePassword);
doc.setResourceCache(new NotResourceCache());
int pageCount = doc.getNumberOfPages();
PDFRenderer pdfRenderer = new PDFRenderer(doc);
-
int index = pdfFilePath.lastIndexOf(".");
String folder = pdfFilePath.substring(0, index);
-
File path = new File(folder);
if (!path.exists() && !path.mkdirs()) {
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
@@ -252,10 +255,27 @@ public class FileHandlerService {
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
imageUrls.add(imageUrl);
}
- doc.close();
- this.addPdf2jpgCache(pdfFilePath, pageCount);
+ try {
+ pdfReader = new PdfReader(pdfFilePath); //判断pdf文件是否加密 缓存不加密文件
+ this.addPdf2jpgCache(pdfFilePath, pageCount);
+ } catch (BadPasswordException e) {
+
+ } catch (Exception e) {
+ e.printStackTrace();
+ }finally {
+ if (pdfReader != null) { //关闭
+ pdfReader.close();
+ }
+ }
+
} catch (IOException e) {
- logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
+ System.out.println("发生错误:"+e);
+ // logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
+ throw new Exception(e);
+ }finally {
+ if (doc != null) { //关闭
+ doc.close();
+ }
}
return imageUrls;
}
diff --git a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
index f2d81f64..212401be 100644
--- a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
+++ b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java
@@ -10,11 +10,14 @@ import cn.keking.utils.DownloadUtils;
import cn.keking.utils.KkFileUtils;
import cn.keking.utils.OfficeUtils;
import cn.keking.web.filter.BaseUrlFilter;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.poi.EncryptedDocumentException;
import org.jodconverter.core.office.OfficeException;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
+import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
@@ -28,6 +31,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
private static final String FILE_DIR = ConfigConstants.getFileDir();
+ private static final String OFFICE_PASSWORD_MSG = "password";
private final FileHandlerService fileHandlerService;
private final OfficeToPdfService officeToPdfService;
@@ -60,7 +64,6 @@ public class OfficeFilePreviewImpl implements FilePreview {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
}
String filePath = response.getContent();
-
/*
* 1. 缓存判断-如果文件已经进行转换过,就直接返回,否则执行转换
* 2. 缓存判断-加密文件基于userToken进行缓存,如果没有就不缓存
@@ -133,7 +136,20 @@ public class OfficeFilePreviewImpl implements FilePreview {
static String getPreviewType(Model model, FileAttribute fileAttribute, String officePreviewType, String baseUrl, String pdfName, String outFilePath, FileHandlerService fileHandlerService, String officePreviewTypeImage, OtherFilePreviewImpl otherFilePreview) {
String suffix = fileAttribute.getSuffix();
boolean isPPT = suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx");
- List imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
+ List imageUrls = null;
+ try {
+ imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
+ } catch (Exception e) {
+ Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
+ for (Throwable throwable : throwableArray) {
+ if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
+ if (e.getMessage().toLowerCase().contains(OFFICE_PASSWORD_MSG)) {
+ model.addAttribute("needFilePassword", true);
+ return EXEL_FILE_PREVIEW_PAGE;
+ }
+ }
+ }
+ }
if (imageUrls == null || imageUrls.size() < 1) {
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
}
diff --git a/server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
index 782df512..2c44e475 100644
--- a/server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
+++ b/server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java
@@ -7,9 +7,12 @@ import cn.keking.service.FilePreview;
import cn.keking.utils.DownloadUtils;
import cn.keking.service.FileHandlerService;
import cn.keking.web.filter.BaseUrlFilter;
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.poi.EncryptedDocumentException;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
+import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
@@ -23,6 +26,7 @@ public class PdfFilePreviewImpl implements FilePreview {
private final FileHandlerService fileHandlerService;
private final OtherFilePreviewImpl otherFilePreview;
private static final String FILE_DIR = ConfigConstants.getFileDir();
+ private static final String PDF_PASSWORD_MSG = "password";
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
this.fileHandlerService = fileHandlerService;
@@ -50,7 +54,21 @@ public class PdfFilePreviewImpl implements FilePreview {
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
}
}
- List imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
+ List imageUrls;
+ try {
+ imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
+ } 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)) {
+ model.addAttribute("needFilePassword", true);
+ return EXEL_FILE_PREVIEW_PAGE;
+ }
+ }
+ }
+ return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
+ }
if (imageUrls == null || imageUrls.size() < 1) {
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
}