新增:pdf支持密码
This commit is contained in:
2
pom.xml
2
pom.xml
@ -33,6 +33,8 @@
|
|||||||
<ffmpeg.version>4.2.1-1.5.2</ffmpeg.version>
|
<ffmpeg.version>4.2.1-1.5.2</ffmpeg.version>
|
||||||
<itextpdf.version>5.5.13.3</itextpdf.version>
|
<itextpdf.version>5.5.13.3</itextpdf.version>
|
||||||
<httpclient.version>3.1</httpclient.version>
|
<httpclient.version>3.1</httpclient.version>
|
||||||
|
<aspose-cad.version>23.1</aspose-cad.version>
|
||||||
|
<bcprov-jdk15on.version>1.70</bcprov-jdk15on.version>
|
||||||
|
|
||||||
<commons-cli.version>1.2</commons-cli.version>
|
<commons-cli.version>1.2</commons-cli.version>
|
||||||
<commons-net.version>3.6</commons-net.version>
|
<commons-net.version>3.6</commons-net.version>
|
||||||
|
|||||||
@ -193,7 +193,13 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.aspose</groupId>
|
<groupId>com.aspose</groupId>
|
||||||
<artifactId>aspose-cad</artifactId>
|
<artifactId>aspose-cad</artifactId>
|
||||||
<version>23.1</version>
|
<version>${aspose-cad.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- 密钥算法 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.bouncycastle</groupId>
|
||||||
|
<artifactId>bcprov-jdk15on</artifactId>
|
||||||
|
<version>${bcprov-jdk15on.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- url 规范化 -->
|
<!-- url 规范化 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@ -15,6 +15,8 @@ import com.aspose.cad.Image;
|
|||||||
import com.aspose.cad.LoadOptions;
|
import com.aspose.cad.LoadOptions;
|
||||||
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
||||||
import com.aspose.cad.imageoptions.PdfOptions;
|
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.pdmodel.PDDocument;
|
||||||
import org.apache.pdfbox.rendering.ImageType;
|
import org.apache.pdfbox.rendering.ImageType;
|
||||||
import org.apache.pdfbox.rendering.PDFRenderer;
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
||||||
@ -218,8 +220,11 @@ public class FileHandlerService {
|
|||||||
* @param pdfName pdf文件名称
|
* @param pdfName pdf文件名称
|
||||||
* @return 图片访问集合
|
* @return 图片访问集合
|
||||||
*/
|
*/
|
||||||
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) {
|
public List<String> pdf2jpg(String pdfFilePath, String pdfName, FileAttribute fileAttribute) throws Exception {
|
||||||
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
||||||
|
String filePassword = fileAttribute.getFilePassword();
|
||||||
|
PDDocument doc = null;
|
||||||
|
PdfReader pdfReader = null;
|
||||||
if (!forceUpdatedCache) {
|
if (!forceUpdatedCache) {
|
||||||
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName);
|
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath, pdfName);
|
||||||
if (!CollectionUtils.isEmpty(cacheResult)) {
|
if (!CollectionUtils.isEmpty(cacheResult)) {
|
||||||
@ -232,14 +237,12 @@ public class FileHandlerService {
|
|||||||
if (!pdfFile.exists()) {
|
if (!pdfFile.exists()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
PDDocument doc = PDDocument.load(pdfFile);
|
doc = PDDocument.load(pdfFile,filePassword);
|
||||||
doc.setResourceCache(new NotResourceCache());
|
doc.setResourceCache(new NotResourceCache());
|
||||||
int pageCount = doc.getNumberOfPages();
|
int pageCount = doc.getNumberOfPages();
|
||||||
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
||||||
|
|
||||||
int index = pdfFilePath.lastIndexOf(".");
|
int index = pdfFilePath.lastIndexOf(".");
|
||||||
String folder = pdfFilePath.substring(0, index);
|
String folder = pdfFilePath.substring(0, index);
|
||||||
|
|
||||||
File path = new File(folder);
|
File path = new File(folder);
|
||||||
if (!path.exists() && !path.mkdirs()) {
|
if (!path.exists() && !path.mkdirs()) {
|
||||||
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
||||||
@ -252,10 +255,27 @@ public class FileHandlerService {
|
|||||||
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
|
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
|
||||||
imageUrls.add(imageUrl);
|
imageUrls.add(imageUrl);
|
||||||
}
|
}
|
||||||
doc.close();
|
try {
|
||||||
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
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) {
|
} 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;
|
return imageUrls;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -10,11 +10,14 @@ import cn.keking.utils.DownloadUtils;
|
|||||||
import cn.keking.utils.KkFileUtils;
|
import cn.keking.utils.KkFileUtils;
|
||||||
import cn.keking.utils.OfficeUtils;
|
import cn.keking.utils.OfficeUtils;
|
||||||
import cn.keking.web.filter.BaseUrlFilter;
|
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.jodconverter.core.office.OfficeException;
|
||||||
import org.springframework.stereotype.Service;
|
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.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.List;
|
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_IMAGE = "image";
|
||||||
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
public static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
|
||||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
private static final String OFFICE_PASSWORD_MSG = "password";
|
||||||
|
|
||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OfficeToPdfService officeToPdfService;
|
private final OfficeToPdfService officeToPdfService;
|
||||||
@ -60,7 +64,6 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
|||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||||||
}
|
}
|
||||||
String filePath = response.getContent();
|
String filePath = response.getContent();
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 1. 缓存判断-如果文件已经进行转换过,就直接返回,否则执行转换
|
* 1. 缓存判断-如果文件已经进行转换过,就直接返回,否则执行转换
|
||||||
* 2. 缓存判断-加密文件基于userToken进行缓存,如果没有就不缓存
|
* 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) {
|
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();
|
String suffix = fileAttribute.getSuffix();
|
||||||
boolean isPPT = suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx");
|
boolean isPPT = suffix.equalsIgnoreCase("ppt") || suffix.equalsIgnoreCase("pptx");
|
||||||
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
|
List<String> 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) {
|
if (imageUrls == null || imageUrls.size() < 1) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,9 +7,12 @@ import cn.keking.service.FilePreview;
|
|||||||
import cn.keking.utils.DownloadUtils;
|
import cn.keking.utils.DownloadUtils;
|
||||||
import cn.keking.service.FileHandlerService;
|
import cn.keking.service.FileHandlerService;
|
||||||
import cn.keking.web.filter.BaseUrlFilter;
|
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.stereotype.Service;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -23,6 +26,7 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
private final FileHandlerService fileHandlerService;
|
private final FileHandlerService fileHandlerService;
|
||||||
private final OtherFilePreviewImpl otherFilePreview;
|
private final OtherFilePreviewImpl otherFilePreview;
|
||||||
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
private static final String FILE_DIR = ConfigConstants.getFileDir();
|
||||||
|
private static final String PDF_PASSWORD_MSG = "password";
|
||||||
|
|
||||||
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
|
||||||
this.fileHandlerService = fileHandlerService;
|
this.fileHandlerService = fileHandlerService;
|
||||||
@ -50,7 +54,21 @@ public class PdfFilePreviewImpl implements FilePreview {
|
|||||||
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
List<String> imageUrls = fileHandlerService.pdf2jpg(outFilePath, pdfName, fileAttribute);
|
List<String> 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) {
|
if (imageUrls == null || imageUrls.size() < 1) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user