|
|
@ -14,8 +14,8 @@ import com.aspose.cad.*;
|
|
|
|
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
|
|
|
|
import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
|
|
|
|
import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
|
|
|
|
import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
|
|
|
|
import com.aspose.cad.imageoptions.*;
|
|
|
|
import com.aspose.cad.imageoptions.*;
|
|
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
|
|
|
|
|
|
|
import org.apache.pdfbox.Loader;
|
|
|
|
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;
|
|
|
@ -37,7 +37,10 @@ import java.io.*;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.net.URLDecoder;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.net.URLEncoder;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.*;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
import java.util.stream.IntStream;
|
|
|
|
|
|
|
|
|
|
|
@ -236,9 +239,9 @@ public class FileHandlerService implements InitializingBean {
|
|
|
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
|
|
|
boolean forceUpdatedCache = fileAttribute.forceUpdatedCache();
|
|
|
|
boolean usePasswordCache = fileAttribute.getUsePasswordCache();
|
|
|
|
boolean usePasswordCache = fileAttribute.getUsePasswordCache();
|
|
|
|
String filePassword = fileAttribute.getFilePassword();
|
|
|
|
String filePassword = fileAttribute.getFilePassword();
|
|
|
|
String pdfPassword = null;
|
|
|
|
PDDocument doc;
|
|
|
|
PDDocument doc = null;
|
|
|
|
final String[] pdfPassword = {null};
|
|
|
|
PdfReader pdfReader = null;
|
|
|
|
final int[] pageCount = new int[1];
|
|
|
|
if (!forceUpdatedCache) {
|
|
|
|
if (!forceUpdatedCache) {
|
|
|
|
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath);
|
|
|
|
List<String> cacheResult = this.loadPdf2jpgCache(pdfFilePath);
|
|
|
|
if (!CollectionUtils.isEmpty(cacheResult)) {
|
|
|
|
if (!CollectionUtils.isEmpty(cacheResult)) {
|
|
|
@ -246,64 +249,77 @@ public class FileHandlerService implements InitializingBean {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
List<String> imageUrls = new ArrayList<>();
|
|
|
|
List<String> imageUrls = new ArrayList<>();
|
|
|
|
|
|
|
|
File pdfFile = new File(fileNameFilePath);
|
|
|
|
|
|
|
|
if (!pdfFile.exists()) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = pdfFilePath.lastIndexOf(".");
|
|
|
|
|
|
|
|
String folder = pdfFilePath.substring(0, index);
|
|
|
|
|
|
|
|
File path = new File(folder);
|
|
|
|
|
|
|
|
if (!path.exists() && !path.mkdirs()) {
|
|
|
|
|
|
|
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
|
|
|
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
File pdfFile = new File(fileNameFilePath);
|
|
|
|
doc = Loader.loadPDF(pdfFile, filePassword);
|
|
|
|
if (!pdfFile.exists()) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
doc = PDDocument.load(pdfFile, filePassword);
|
|
|
|
|
|
|
|
doc.setResourceCache(new NotResourceCache());
|
|
|
|
doc.setResourceCache(new NotResourceCache());
|
|
|
|
int pageCount = doc.getNumberOfPages();
|
|
|
|
pageCount[0] = doc.getNumberOfPages();
|
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
} catch (IOException e) {
|
|
|
|
int index = pdfFilePath.lastIndexOf(".");
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
String folder = pdfFilePath.substring(0, index);
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
File path = new File(folder);
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
if (!path.exists() && !path.mkdirs()) {
|
|
|
|
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
logger.error("创建转换文件【{}】目录失败,请检查目录权限!", folder);
|
|
|
|
pdfPassword[0] = PDF_PASSWORD_MSG; //查询到该文件是密码文件 输出带密码的值
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
String imageFilePath;
|
|
|
|
if (!PDF_PASSWORD_MSG.equals(pdfPassword[0])) { //该文件异常 错误原因非密码原因输出错误
|
|
|
|
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
|
|
|
|
logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
|
|
|
|
|
|
|
|
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
|
|
|
|
|
|
|
|
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
|
|
|
|
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfFilePath, pageIndex);
|
|
|
|
|
|
|
|
imageUrls.add(imageUrl);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Callable <List<String>> call = () -> {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (!ObjectUtils.isEmpty(filePassword)) { //获取到密码 判断是否是加密文件
|
|
|
|
String imageFilePath;
|
|
|
|
pdfReader = new PdfReader(fileNameFilePath); //读取PDF文件 通过异常获取该文件是否有密码字符
|
|
|
|
BufferedImage image = null;
|
|
|
|
|
|
|
|
PDFRenderer pdfRenderer = new PDFRenderer(doc);
|
|
|
|
|
|
|
|
pdfRenderer.setSubsamplingAllowed(true);
|
|
|
|
|
|
|
|
for (int pageIndex = 0; pageIndex < pageCount[0]; pageIndex++) {
|
|
|
|
|
|
|
|
imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
|
|
|
|
|
|
|
|
image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
|
|
|
|
|
|
|
|
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
|
|
|
|
|
|
|
|
String imageUrl = this.getPdf2jpgUrl(pdfFilePath, pageIndex);
|
|
|
|
|
|
|
|
imageUrls.add(imageUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) { //获取异常方法 判断是否有加密字符串
|
|
|
|
image.flush();
|
|
|
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
for (Throwable throwable : throwableArray) {
|
|
|
|
throw new Exception(e);
|
|
|
|
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
|
|
|
|
|
|
|
|
if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
|
|
|
pdfPassword = PDF_PASSWORD_MSG; //查询到该文件是密码文件 输出带密码的值
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!PDF_PASSWORD_MSG.equals(pdfPassword)) { //该文件异常 错误原因非密码原因输出错误
|
|
|
|
|
|
|
|
logger.error("Convert pdf exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (pdfReader != null) { //关闭
|
|
|
|
doc.close();
|
|
|
|
pdfReader.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (usePasswordCache || !PDF_PASSWORD_MSG.equals(pdfPassword)) { //加密文件 判断是否启用缓存命令
|
|
|
|
|
|
|
|
this.addPdf2jpgCache(pdfFilePath, pageCount);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
if (!e.getMessage().contains(PDF_PASSWORD_MSG)) {
|
|
|
|
|
|
|
|
logger.error("Convert pdf to jpg exception, pdfFilePath:{}", pdfFilePath, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageUrls;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
Future<List<String>> result = pool.submit(call);
|
|
|
|
|
|
|
|
int pdftimeout;
|
|
|
|
|
|
|
|
if(pageCount[0] <=50){
|
|
|
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout();
|
|
|
|
|
|
|
|
}else if(pageCount[0] <=200){
|
|
|
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout80();
|
|
|
|
|
|
|
|
}else {
|
|
|
|
|
|
|
|
pdftimeout = ConfigConstants.getPdfTimeout200();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
result.get(pdftimeout, TimeUnit.SECONDS);
|
|
|
|
|
|
|
|
// 如果在超时时间内,没有数据返回:则抛出TimeoutException异常
|
|
|
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new Exception(e);
|
|
|
|
throw new Exception(e);
|
|
|
|
|
|
|
|
} catch (TimeoutException e) {
|
|
|
|
|
|
|
|
throw new Exception("overtime");
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
if (doc != null) { //关闭
|
|
|
|
//关闭
|
|
|
|
doc.close();
|
|
|
|
doc.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (usePasswordCache || ObjectUtils.isEmpty(filePassword)) { //加密文件 判断是否启用缓存命令
|
|
|
|
|
|
|
|
this.addPdf2jpgCache(pdfFilePath, pageCount[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return imageUrls;
|
|
|
|
return imageUrls;
|
|
|
|
}
|
|
|
|
}
|
|
|
|