!116 调整 tif文件生成图片 添加后缀 防止同名产生 简化代码

Merge pull request !116 from 高雄/tif
This commit is contained in:
kailing 2023-04-24 08:43:01 +00:00 committed by Gitee
commit e3b9df09f4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 28 additions and 50 deletions

View File

@ -49,32 +49,32 @@ public class TiffFilePreviewImpl implements FilePreview {
return TIFF_FILE_PREVIEW_PAGE;
} else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
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 outFilePath = fileDir + pdfName;
if ("pdf".equalsIgnoreCase(tifPreviewType)) {
//当文件不存在时就去下载
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (response.isFailure()) {
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;
}
//当文件不存在时就去下载
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
if (response.isFailure()) {
return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
}
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);
return PDF_FILE_PREVIEW_PAGE;
}
@ -88,10 +88,8 @@ public class TiffFilePreviewImpl implements FilePreview {
}
strLocalTif = response.getContent();
}
// 以JPG模式预览的过程
String strJpgFilePathName = fileDir + jpgName;
// 将tif转换为jpg返回转换后的文件路径文件名的list
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, strJpgFilePathName);
List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, jpgName);
// 将返回页面的图片url的list对象
List<String> listImageUrls = new ArrayList<>();
// 循环拼装url的list对象

View File

@ -45,46 +45,26 @@ public class ConvertPicUtil {
return null;
}
strInputFile = strInputFile.replaceAll("\\\\", "/");
strOutputFile = strOutputFile.replaceAll("\\\\", "/");
FileSeekableStream fileSeekStream = null;
try {
JPEGEncodeParam jpegEncodeParam = new JPEGEncodeParam();
TIFFEncodeParam tiffEncodeParam = new TIFFEncodeParam();
tiffEncodeParam.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
tiffEncodeParam.setLittleEndian(false);
String jpgname = strInputFile.replace(fileDir.replace("\\","/"), "");
int index = jpgname.lastIndexOf(".");
String strFilePrefix = jpgname.substring(0, index);
fileSeekStream = new FileSeekableStream(strInputFile);
ImageDecoder imageDecoder = ImageCodec.createImageDecoder("TIFF", fileSeekStream, null);
int intTifCount = imageDecoder.getNumPages();
logger.info("该tif文件共有【" + intTifCount + "】页");
String strJpgPath;
String strJpgUrl;
if (intTifCount == 1) {
// 如果是单页tif文件则转换的目标文件夹就在指定的位置
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("/"));
} else {
// 如果是多页tif文件则在目标文件夹下按照文件名再创建子目录将转换后的文件放入此新建的子目录中
strJpgPath = strOutputFile.substring(0, strOutputFile.lastIndexOf("."));
}
String strJpgPath = fileDir+strOutputFile;
// 处理目标文件夹如果不存在则自动创建
File fileJpgPath = new File(strJpgPath);
if (!fileJpgPath.exists() && !fileJpgPath.mkdirs()) {
logger.error("{} 创建失败", strJpgPath);
}
// 循环处理每页tif文件转换为jpg
for (int i = 0; i < intTifCount; i++) {
String strJpg;
if (intTifCount == 1) {
strJpg = strJpgPath + "/" + strFilePrefix + ".jpg";
strJpgUrl = strFilePrefix + ".jpg";
} else {
strJpg = strJpgPath + "/" + i + ".jpg";
strJpgUrl = strFilePrefix + "/" + i + ".jpg";
}
String strJpg= strJpgPath + "/" + i + ".jpg";
String strJpgUrl = strOutputFile + "/" + i + ".jpg";
File fileJpg = new File(strJpg);
// 如果文件不存在则生成
if (!fileJpg.exists()) {
@ -98,7 +78,7 @@ public class ConvertPicUtil {
renderedOp.dispose();
logger.info("每页分别保存至: " + fileJpg.getCanonicalPath());
} else {
// logger.info("JPG文件已存在 " + fileJpg.getCanonicalPath());
// logger.info("JPG文件已存在 " + fileJpg.getCanonicalPath());
}
listImageFiles.add(strJpgUrl);
@ -154,7 +134,7 @@ public class ConvertPicUtil {
}
catch (Exception e)
{
System.out.println("错误:"+ e.getMessage());
System.out.println("错误:"+ e.getMessage());
}
finally {
document.close();