fix FileHandlerService#pdf2jpg npe (github issue #455)

This commit is contained in:
陈精华
2023-05-12 23:09:57 +08:00
parent ad46dd8b5b
commit 5dbc8a4980

View File

@@ -47,11 +47,11 @@ import java.util.stream.IntStream;
@Component @Component
public class FileHandlerService { public class FileHandlerService {
private static final String PDF2JPG_IMAGE_FORMAT = ".jpg";
private static final String PDF_PASSWORD_MSG = "password";
private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class); private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class);
private final String fileDir = ConfigConstants.getFileDir(); private final String fileDir = ConfigConstants.getFileDir();
private final static String pdf2jpg_image_format = ".jpg";
private final CacheService cacheService; private final CacheService cacheService;
private static final String pdf_password_msg = "password";
@Value("${server.tomcat.uri-encoding:UTF-8}") @Value("${server.tomcat.uri-encoding:UTF-8}")
private String uriEncoding; private String uriEncoding;
@@ -193,7 +193,7 @@ public class FileHandlerService {
logger.error("UnsupportedEncodingException", e); logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder; urlPrefix = baseUrl + pdfFolder;
} }
return urlPrefix + "/" + index + pdf2jpg_image_format; return urlPrefix + "/" + index + PDF2JPG_IMAGE_FORMAT;
} }
/** /**
@@ -252,7 +252,7 @@ public class FileHandlerService {
} }
String imageFilePath; String imageFilePath;
for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) { for (int pageIndex = 0; pageIndex < pageCount; pageIndex++) {
imageFilePath = folder + File.separator + pageIndex + pdf2jpg_image_format; imageFilePath = folder + File.separator + pageIndex + PDF2JPG_IMAGE_FORMAT;
BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB); BufferedImage image = pdfRenderer.renderImageWithDPI(pageIndex, ConfigConstants.getPdf2JpgDpi(), ImageType.RGB);
ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi()); ImageIOUtil.writeImage(image, imageFilePath, ConfigConstants.getPdf2JpgDpi());
String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex); String imageUrl = this.getPdf2jpgUrl(pdfName, pageIndex);
@@ -264,24 +264,25 @@ public class FileHandlerService {
Throwable[] throwableArray = ExceptionUtils.getThrowables(e); Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
for (Throwable throwable : throwableArray) { for (Throwable throwable : throwableArray) {
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) { if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
if (e.getMessage().toLowerCase().contains(pdf_password_msg)) { if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
pdfPassword = pdf_password_msg; pdfPassword = PDF_PASSWORD_MSG;
} }
} }
} }
logger.error("Convert pdf exception, pdfFilePath{}", pdfFilePath, e); logger.error("Convert pdf exception, pdfFilePath{}", pdfFilePath, e);
}finally { } finally {
if (pdfReader != null) { //关闭 if (pdfReader != null) { //关闭
pdfReader.close(); pdfReader.close();
} }
} }
if(!pdfPassword.equals(pdf_password_msg)){ //判断是否加密文件 加密文件不缓存 //判断是否加密文件 加密文件不缓存
if (PDF_PASSWORD_MSG.equals(pdfPassword)) {
this.addPdf2jpgCache(pdfFilePath, pageCount); this.addPdf2jpgCache(pdfFilePath, pageCount);
} }
} catch (IOException e) { } catch (IOException e) {
logger.error("Convert pdf to jpg exception, pdfFilePath{}", pdfFilePath, e); logger.error("Convert pdf to jpg exception, pdfFilePath{}", pdfFilePath, e);
throw new Exception(e); throw new Exception(e);
}finally { } finally {
if (doc != null) { //关闭 if (doc != null) { //关闭
doc.close(); doc.close();
} }