调整 tif文件生成图片 添加后缀 防止同名产生 简化代码
This commit is contained in:
@ -49,32 +49,32 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||||||
return TIFF_FILE_PREVIEW_PAGE;
|
return TIFF_FILE_PREVIEW_PAGE;
|
||||||
} else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
|
} else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
|
||||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
|
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
|
||||||
String jpgName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "jpg" ; //生成文件添加类型后缀 防止同名文件
|
String jpgName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix; //生成文件添加类型后缀 防止同名文件
|
||||||
String strLocalTif = fileDir + fileName;
|
String strLocalTif = fileDir + fileName;
|
||||||
String outFilePath = fileDir + pdfName;
|
String outFilePath = fileDir + pdfName;
|
||||||
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
|
||||||
//当文件不存在时,就去下载
|
//当文件不存在时,就去下载
|
||||||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||||
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
|
||||||
if (response.isFailure()) {
|
if (response.isFailure()) {
|
||||||
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
|
||||||
}
|
|
||||||
String filePath = response.getContent();
|
|
||||||
if(ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath)){
|
|
||||||
if(ConfigConstants.getdeletesourcefile()){ //是否保留TIFF源文件
|
|
||||||
KkFileUtils.deleteFileByPath(filePath);
|
|
||||||
}
|
|
||||||
if (ConfigConstants.isCacheEnabled()) {
|
|
||||||
// 加入缓存
|
|
||||||
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
|
||||||
}
|
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
|
||||||
return PDF_FILE_PREVIEW_PAGE;
|
|
||||||
}else {
|
|
||||||
return NOT_SUPPORTED_FILE_PAGE;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else {
|
String filePath = response.getContent();
|
||||||
|
if(ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath)){
|
||||||
|
if(ConfigConstants.getdeletesourcefile()){ //是否保留TIFF源文件
|
||||||
|
KkFileUtils.deleteFileByPath(filePath);
|
||||||
|
}
|
||||||
|
if (ConfigConstants.isCacheEnabled()) {
|
||||||
|
// 加入缓存
|
||||||
|
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
|
||||||
|
}
|
||||||
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
|
return PDF_FILE_PREVIEW_PAGE;
|
||||||
|
}else {
|
||||||
|
return NOT_SUPPORTED_FILE_PAGE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
model.addAttribute("pdfUrl", pdfName);
|
model.addAttribute("pdfUrl", pdfName);
|
||||||
return PDF_FILE_PREVIEW_PAGE;
|
return PDF_FILE_PREVIEW_PAGE;
|
||||||
}
|
}
|
||||||
@ -88,10 +88,8 @@ public class TiffFilePreviewImpl implements FilePreview {
|
|||||||
}
|
}
|
||||||
strLocalTif = response.getContent();
|
strLocalTif = response.getContent();
|
||||||
}
|
}
|
||||||
// 以JPG模式预览的过程
|
|
||||||
String strJpgFilePathName = fileDir + jpgName;
|
|
||||||
// 将tif转换为jpg,返回转换后的文件路径、文件名的list
|
// 将tif转换为jpg,返回转换后的文件路径、文件名的list
|
||||||
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, strJpgFilePathName);
|
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, jpgName);
|
||||||
// 将返回页面的图片url的list对象
|
// 将返回页面的图片url的list对象
|
||||||
List<String> listImageUrls = new ArrayList<>();
|
List<String> listImageUrls = new ArrayList<>();
|
||||||
// 循环,拼装url的list对象
|
// 循环,拼装url的list对象
|
||||||
|
|||||||
@ -45,46 +45,26 @@ public class ConvertPicUtil {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
strInputFile = strInputFile.replaceAll("\\\\", "/");
|
strInputFile = strInputFile.replaceAll("\\\\", "/");
|
||||||
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
|
|
||||||
FileSeekableStream fileSeekStream = null;
|
FileSeekableStream fileSeekStream = null;
|
||||||
try {
|
try {
|
||||||
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
|
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
|
||||||
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
|
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
|
||||||
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
|
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
|
||||||
tiffEncodeParam.setLittleEndian(false);
|
tiffEncodeParam.setLittleEndian(false);
|
||||||
String jpgname = strInputFile.replace(fileDir.replace("\\","/"), "");
|
|
||||||
int index = jpgname.lastIndexOf(".");
|
|
||||||
String strFilePrefix = jpgname.substring(0, index);
|
|
||||||
fileSeekStream = new FileSeekableStream(strInputFile);
|
fileSeekStream = new FileSeekableStream(strInputFile);
|
||||||
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
|
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
|
||||||
int intTifCount = imageDecoder.getNumPages();
|
int intTifCount = imageDecoder.getNumPages();
|
||||||
logger.info("该tif文件共有【" + intTifCount + "】页");
|
logger.info("该tif文件共有【" + intTifCount + "】页");
|
||||||
String strJpgPath;
|
String strJpgPath = fileDir+strOutputFile;
|
||||||
String strJpgUrl;
|
|
||||||
if (intTifCount == 1) {
|
|
||||||
// 如果是单页tif文件,则转换的目标文件夹就在指定的位置
|
|
||||||
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("/"));
|
|
||||||
} else {
|
|
||||||
// 如果是多页tif文件,则在目标文件夹下,按照文件名再创建子目录,将转换后的文件放入此新建的子目录中
|
|
||||||
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("."));
|
|
||||||
}
|
|
||||||
|
|
||||||
// 处理目标文件夹,如果不存在则自动创建
|
// 处理目标文件夹,如果不存在则自动创建
|
||||||
File fileJpgPath = new File(strJpgPath);
|
File fileJpgPath = new File(strJpgPath);
|
||||||
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
|
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
|
||||||
logger.error("{} 创建失败", strJpgPath);
|
logger.error("{} 创建失败", strJpgPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 循环,处理每页tif文件,转换为jpg
|
// 循环,处理每页tif文件,转换为jpg
|
||||||
for (int i = 0; i < intTifCount; i++) {
|
for (int i = 0; i < intTifCount; i++) {
|
||||||
String strJpg;
|
String strJpg= strJpgPath + "/" + i + ".jpg";
|
||||||
if (intTifCount == 1) {
|
String strJpgUrl = strOutputFile + "/" + i + ".jpg";
|
||||||
strJpg = strJpgPath + "/" + strFilePrefix + ".jpg";
|
|
||||||
strJpgUrl = strFilePrefix + ".jpg";
|
|
||||||
} else {
|
|
||||||
strJpg = strJpgPath + "/" + i + ".jpg";
|
|
||||||
strJpgUrl = strFilePrefix + "/" + i + ".jpg";
|
|
||||||
}
|
|
||||||
File fileJpg = new File(strJpg);
|
File fileJpg = new File(strJpg);
|
||||||
// 如果文件不存在,则生成
|
// 如果文件不存在,则生成
|
||||||
if (!fileJpg.exists()) {
|
if (!fileJpg.exists()) {
|
||||||
@ -98,7 +78,7 @@ public class ConvertPicUtil {
|
|||||||
renderedOp.dispose();
|
renderedOp.dispose();
|
||||||
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
|
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
|
||||||
} else {
|
} else {
|
||||||
// logger.info("JPG文件已存在: " + fileJpg.getCanonicalPath());
|
// logger.info("JPG文件已存在: " + fileJpg.getCanonicalPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
listImageFiles.add(strJpgUrl);
|
listImageFiles.add(strJpgUrl);
|
||||||
@ -154,7 +134,7 @@ public class ConvertPicUtil {
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
System.out.println("错误:"+ e.getMessage());
|
System.out.println("错误:"+ e.getMessage());
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
document.close();
|
document.close();
|
||||||
|
|||||||
Reference in New Issue
Block a user