mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-06-28 13:34:24 +08:00
调整 tif文件生成图片 添加后缀 防止同名产生 简化代码
This commit is contained in:
parent
8649f20841
commit
a1abdfb70a
@ -49,7 +49,7 @@ 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)) {
|
||||||
@ -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()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user