@ -22,6 +22,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
import org.springframework.util.CollectionUtils ;
import org.springframework.util.StringUtils ;
import javax.servlet.http.HttpServletRequest ;
@ -34,6 +35,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.Objects ;
import java.util.stream.IntStream ;
/**
* @author yudian-it
@ -44,6 +47,7 @@ public class FileHandlerService {
private final Logger logger = LoggerFactory . getLogger ( FileHandlerService . class ) ;
private final String fileDir = ConfigConstants . getFileDir ( ) ;
private final static String pdf2jpg_image_format = " .jpg " ;
private final CacheService cacheService ;
@Value ( " ${server.tomcat.uri-encoding:UTF-8} " )
@ -71,7 +75,7 @@ public class FileHandlerService {
* @param key pdf本地路径
* @return 已将pdf转换成图片的图片本地相对路径
*/
public Integer getConvertedPdfImag e ( String key ) {
public Integer getPdf2jpgCach e ( String key ) {
return cacheService . getPdfImageCache ( key ) ;
}
@ -112,7 +116,7 @@ public class FileHandlerService {
* @param pdfFilePath pdf文件绝对路径
* @param num 图片张数
*/
public void addConvertedPdfImag e ( String pdfFilePath , int num ) {
public void addPdf2jpgCach e ( String pdfFilePath , int num ) {
cacheService . putPdfImageCache ( pdfFilePath , num ) ;
}
@ -170,34 +174,60 @@ public class FileHandlerService {
}
/**
* pdf文件转换成jpg图片集
* @param pdfFilePath pdf文件路径
* @param pdfName pdf文件名称
* @param baseUrl 基础 访问地址
* @return 图片访问集合
* 获取本地 pdf 转 image 后的 web 访问地址
* @param pdfName pdf文件名
* @param index 图片索引
* @return 图片 访问地址
*/
public List < String > p df2jpg( String pdfFilePath , String pdfName , String baseUrl , FileAttribute fileAttribute ) {
List < String> imag eUrls = new ArrayList < > ( ) ;
Integer imageCount = null ;
String imageFileSuffix = " .jpg " ;
private String getP df2jpgUrl ( String pdfName , int index ) {
String bas eUrl = ConfigConstants . getBaseUrl ( ) ;
String pdfFolder = pdfName . substring ( 0 , pdfName . length ( ) - 4 ) ;
boolean forceUpdatedCache = fileAttribute . forceUpdatedCache ( ) ;
if ( ! forceUpdatedCache ) {
imageCount = this . getConvertedPdfImage ( pdfFilePath ) ;
}
String urlPrefix ;
try {
urlPrefix = baseUrl + URLEncoder . encode ( pdfFolder , uriEncoding ) . replaceAll ( " \\ + " , " %20 " ) ;
} catch ( UnsupportedEncodingException e ) {
logger . error ( " UnsupportedEncodingException " , e ) ;
urlPrefix = baseUrl + pdfFolder ;
}
if ( imageCount ! = null & & imageCount > 0 ) {
for ( int i = 0 ; i < imageCount ; i + + ) {
imageUrls . add ( urlPrefix + " / " + i + imageFileSuffix ) ;
return urlPrefix + " / " + index + pdf2jpg_image_format ;
}
/**
* 获取缓存中的 pdf 转换成 jpg 图片集
* @param pdfFilePath pdf文件路径
* @param pdfName pdf文件名称
* @return 图片访问集合
*/
private List < String > loadPdf2jpgCache ( String pdfFilePath , String pdfName ) {
List < String > imageUrls = new ArrayList < > ( ) ;
Integer imageCount = this . getPdf2jpgCache ( pdfFilePath ) ;
if ( Objects . isNull ( imageCount ) ) {
return imageUrls ;
}
IntStream . range ( 0 , imageCount ) . forEach ( i - > {
String imageUrl = this . getPdf2jpgUrl ( pdfName , i ) ;
imageUrls . add ( imageUrl ) ;
} ) ;
return imageUrls ;
}
/**
* pdf文件转换成jpg图片集
*
* @param pdfFilePath pdf文件路径
* @param pdfName pdf文件名称
* @return 图片访问集合
*/
public List < String > pdf2jpg ( String pdfFilePath , String pdfName , FileAttribute fileAttribute ) {
boolean forceUpdatedCache = fileAttribute . forceUpdatedCache ( ) ;
if ( ! forceUpdatedCache ) {
List < String > cacheResult = this . loadPdf2jpgCache ( pdfFilePath , pdfName ) ;
if ( ! CollectionUtils . isEmpty ( cacheResult ) ) {
return cacheResult ;
}
}
List < String > imageUrls = new ArrayList < > ( ) ;
try {
File pdfFile = new File ( pdfFilePath ) ;
if ( ! pdfFile . exists ( ) ) {
@ -217,13 +247,14 @@ public class FileHandlerService {
}
String imageFilePath ;
for ( int pageIndex = 0 ; pageIndex < pageCount ; pageIndex + + ) {
imageFilePath = folder + File . separator + pageIndex + imageFileSuffix ;
imageFilePath = folder + File . separator + pageIndex + pdf2jpg_image_format ;
BufferedImage image = pdfRenderer . renderImageWithDPI ( pageIndex , 105 , ImageType . RGB ) ;
ImageIOUtil . writeImage ( image , imageFilePath , 105 ) ;
imageUrls . add ( urlPrefix + " / " + pageIndex + im ageFileSuffi x) ;
String imageUrl = this . getPdf2jpgUrl ( pdfName , p ageInde x) ;
imageUrls . add ( imageUrl ) ;
}
doc . close ( ) ;
this . addConvertedPdfImag e ( pdfFilePath , pageCount ) ;
this . addPdf2jpgCach e ( pdfFilePath , pageCount ) ;
} catch ( IOException e ) {
logger . error ( " Convert pdf to jpg exception, pdfFilePath: {} " , pdfFilePath , e ) ;
}
@ -232,6 +263,7 @@ public class FileHandlerService {
/**
* cad文件转pdf
*
* @param inputFilePath cad文件路径
* @param outputFilePath pdf输出文件路径
* @return 转换是否成功
@ -349,6 +381,7 @@ public class FileHandlerService {
/**
* 添加转换后的视频文件缓存
*
* @param fileName
* @param value
*/