@ -22,6 +22,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory ;
import org.slf4j.LoggerFactory ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.beans.factory.annotation.Value ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import org.springframework.util.CollectionUtils ;
import org.springframework.util.StringUtils ;
import org.springframework.util.StringUtils ;
import javax.servlet.http.HttpServletRequest ;
import javax.servlet.http.HttpServletRequest ;
@ -34,6 +35,8 @@ import java.nio.charset.StandardCharsets;
import java.util.ArrayList ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.List ;
import java.util.Map ;
import java.util.Map ;
import java.util.Objects ;
import java.util.stream.IntStream ;
/**
/**
* @author yudian-it
* @author yudian-it
@ -44,6 +47,7 @@ public class FileHandlerService {
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 ;
@Value ( " ${server.tomcat.uri-encoding:UTF-8} " )
@Value ( " ${server.tomcat.uri-encoding:UTF-8} " )
@ -71,7 +75,7 @@ public class FileHandlerService {
* @param key pdf本地路径
* @param key pdf本地路径
* @return 已将pdf转换成图片的图片本地相对路径
* @return 已将pdf转换成图片的图片本地相对路径
*/
*/
public Integer getConvertedPdfImag e ( String key ) {
public Integer getPdf2jpgCach e ( String key ) {
return cacheService . getPdfImageCache ( key ) ;
return cacheService . getPdfImageCache ( key ) ;
}
}
@ -112,7 +116,7 @@ public class FileHandlerService {
* @param pdfFilePath pdf文件绝对路径
* @param pdfFilePath pdf文件绝对路径
* @param num 图片张数
* @param num 图片张数
*/
*/
public void addConvertedPdfImag e ( String pdfFilePath , int num ) {
public void addPdf2jpgCach e ( String pdfFilePath , int num ) {
cacheService . putPdfImageCache ( pdfFilePath , num ) ;
cacheService . putPdfImageCache ( pdfFilePath , num ) ;
}
}
@ -170,34 +174,60 @@ public class FileHandlerService {
}
}
/**
/**
* pdf文件转换成jpg图片集
* 获取本地 pdf 转 image 后的 web 访问地址
* @param pdfFilePath pdf文件路径
* @param pdfName pdf文件名
* @param pdfName pdf文件名称
* @param index 图片索引
* @param baseUrl 基础 访问地址
* @return 图片 访问地址
* @return 图片访问集合
*/
*/
public List < String > p df2jpg( String pdfFilePath , String pdfName , String baseUrl , FileAttribute fileAttribute ) {
private String getP df2jpgUrl ( String pdfName , int index ) {
List < String> imag eUrls = new ArrayList < > ( ) ;
String bas eUrl = ConfigConstants . getBaseUrl ( ) ;
Integer imageCount = null ;
String imageFileSuffix = " .jpg " ;
String pdfFolder = pdfName . substring ( 0 , pdfName . length ( ) - 4 ) ;
String pdfFolder = pdfName . substring ( 0 , pdfName . length ( ) - 4 ) ;
boolean forceUpdatedCache = fileAttribute . forceUpdatedCache ( ) ;
if ( ! forceUpdatedCache ) {
imageCount = this . getConvertedPdfImage ( pdfFilePath ) ;
}
String urlPrefix ;
String urlPrefix ;
try {
try {
urlPrefix = baseUrl + URLEncoder . encode ( pdfFolder , uriEncoding ) . replaceAll ( " \\ + " , " %20 " ) ;
urlPrefix = baseUrl + URLEncoder . encode ( pdfFolder , uriEncoding ) . replaceAll ( " \\ + " , " %20 " ) ;
} catch ( UnsupportedEncodingException e ) {
} catch ( UnsupportedEncodingException e ) {
logger . error ( " UnsupportedEncodingException " , e ) ;
logger . error ( " UnsupportedEncodingException " , e ) ;
urlPrefix = baseUrl + pdfFolder ;
urlPrefix = baseUrl + pdfFolder ;
}
}
if ( imageCount ! = null & & imageCount > 0 ) {
return urlPrefix + " / " + index + pdf2jpg_image_format ;
for ( int i = 0 ; i < imageCount ; i + + ) {
}
imageUrls . add ( urlPrefix + " / " + i + imageFileSuffix ) ;
}
/**
* 获取缓存中的 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 ;
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 {
try {
File pdfFile = new File ( pdfFilePath ) ;
File pdfFile = new File ( pdfFilePath ) ;
if ( ! pdfFile . exists ( ) ) {
if ( ! pdfFile . exists ( ) ) {
@ -217,13 +247,14 @@ 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 + imageFileSuffix ;
imageFilePath = folder + File . separator + pageIndex + pdf2jpg_image_format ;
BufferedImage image = pdfRenderer . renderImageWithDPI ( pageIndex , 105 , ImageType . RGB ) ;
BufferedImage image = pdfRenderer . renderImageWithDPI ( pageIndex , 105 , ImageType . RGB ) ;
ImageIOUtil . writeImage ( image , imageFilePath , 105 ) ;
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 ( ) ;
doc . close ( ) ;
this . addConvertedPdfImag e ( pdfFilePath , pageCount ) ;
this . addPdf2jpgCach e ( 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 ) ;
}
}
@ -232,11 +263,12 @@ public class FileHandlerService {
/**
/**
* cad文件转pdf
* cad文件转pdf
* @param inputFilePath cad文件路径
*
* @param inputFilePath cad文件路径
* @param outputFilePath pdf输出文件路径
* @param outputFilePath pdf输出文件路径
* @return 转换是否成功
* @return 转换是否成功
*/
*/
public boolean cadToPdf ( String inputFilePath , String outputFilePath ) {
public boolean cadToPdf ( String inputFilePath , String outputFilePath ) {
File outputFile = new File ( outputFilePath ) ;
File outputFile = new File ( outputFilePath ) ;
LoadOptions opts = new LoadOptions ( ) ;
LoadOptions opts = new LoadOptions ( ) ;
opts . setSpecifiedEncoding ( CodePages . SimpChinese ) ;
opts . setSpecifiedEncoding ( CodePages . SimpChinese ) ;
@ -246,7 +278,7 @@ public class FileHandlerService {
cadRasterizationOptions . setPageWidth ( 1400 ) ;
cadRasterizationOptions . setPageWidth ( 1400 ) ;
cadRasterizationOptions . setPageHeight ( 650 ) ;
cadRasterizationOptions . setPageHeight ( 650 ) ;
cadRasterizationOptions . setAutomaticLayoutsScaling ( true ) ;
cadRasterizationOptions . setAutomaticLayoutsScaling ( true ) ;
cadRasterizationOptions . setNoScaling ( false ) ;
cadRasterizationOptions . setNoScaling ( false ) ;
cadRasterizationOptions . setDrawType ( 1 ) ;
cadRasterizationOptions . setDrawType ( 1 ) ;
PdfOptions pdfOptions = new PdfOptions ( ) ;
PdfOptions pdfOptions = new PdfOptions ( ) ;
pdfOptions . setVectorRasterizationOptions ( cadRasterizationOptions ) ;
pdfOptions . setVectorRasterizationOptions ( cadRasterizationOptions ) ;
@ -259,8 +291,8 @@ public class FileHandlerService {
return true ;
return true ;
} catch ( IOException e ) {
} catch ( IOException e ) {
logger . error ( " PDFFileNotFoundException, inputFilePath: {} " , inputFilePath , e ) ;
logger . error ( " PDFFileNotFoundException, inputFilePath: {} " , inputFilePath , e ) ;
} finally {
} finally {
if ( cadImage ! = null ) { //关闭
if ( cadImage ! = null ) { //关闭
cadImage . close ( ) ;
cadImage . close ( ) ;
}
}
}
}
@ -297,12 +329,12 @@ public class FileHandlerService {
suffix = WebUtils . suffixFromUrl ( url ) ;
suffix = WebUtils . suffixFromUrl ( url ) ;
}
}
if ( url . contains ( " ?fileKey= " ) ) {
if ( url . contains ( " ?fileKey= " ) ) {
fileName = urlStrr ;
fileName = urlStrr ;
attribute . setSkipDownLoad ( true ) ;
attribute . setSkipDownLoad ( true ) ;
}
}
// System.out.println(fileName);
// System.out.println(fileName);
url = WebUtils . encodeUrlFileName ( url ) ;
url = WebUtils . encodeUrlFileName ( url ) ;
fileName = KkFileUtils . htmlEscape ( fileName ) ; //文件名处理
fileName = KkFileUtils . htmlEscape ( fileName ) ; //文件名处理
attribute . setType ( type ) ;
attribute . setType ( type ) ;
attribute . setName ( fileName ) ;
attribute . setName ( fileName ) ;
attribute . setSuffix ( suffix ) ;
attribute . setSuffix ( suffix ) ;
@ -310,14 +342,14 @@ public class FileHandlerService {
if ( req ! = null ) {
if ( req ! = null ) {
String officePreviewType = req . getParameter ( " officePreviewType " ) ;
String officePreviewType = req . getParameter ( " officePreviewType " ) ;
String forceUpdatedCache = req . getParameter ( " forceUpdatedCache " ) ;
String forceUpdatedCache = req . getParameter ( " forceUpdatedCache " ) ;
String fileKey = WebUtils . getUrlParameterReg ( url , " fileKey " ) ;
String fileKey = WebUtils . getUrlParameterReg ( url , " fileKey " ) ;
if ( StringUtils . hasText ( officePreviewType ) ) {
if ( StringUtils . hasText ( officePreviewType ) ) {
attribute . setOfficePreviewType ( officePreviewType ) ;
attribute . setOfficePreviewType ( officePreviewType ) ;
}
}
if ( StringUtils . hasText ( fileKey ) ) {
if ( StringUtils . hasText ( fileKey ) ) {
attribute . setFileKey ( fileKey ) ;
attribute . setFileKey ( fileKey ) ;
}
}
if ( " true " . equalsIgnoreCase ( forceUpdatedCache ) ) {
if ( " true " . equalsIgnoreCase ( forceUpdatedCache ) ) {
attribute . setforceUpdatedCache ( true ) ;
attribute . setforceUpdatedCache ( true ) ;
}
}
@ -349,6 +381,7 @@ public class FileHandlerService {
/**
/**
* 添加转换后的视频文件缓存
* 添加转换后的视频文件缓存
*
* @param fileName
* @param fileName
* @param value
* @param value
*/
*/