* tif图片预览,在application.properties中加入tif.preview.type = ${KK_TIF_PREVIEW_TYPE:tif},可以控制使用tif、jpg、pdf方式预览。

修改相应代码,加入必要的判断处理。
This commit is contained in:
zhangzhen1979 2021-12-05 12:38:30 +08:00 committed by kl
parent b7760ab42a
commit 6dce47e47f
6 changed files with 86 additions and 38 deletions

View File

@ -260,6 +260,7 @@
<groupId>javax.media</groupId>
<artifactId>jai_core</artifactId>
<version>1.1.2-beta</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jai_core-1.1.2-beta.jar</systemPath>
</dependency>
@ -267,6 +268,7 @@
<groupId>javax.media</groupId>
<artifactId>jai_codec</artifactId>
<version>1.1.3</version>
<scope>system</scope>
<systemPath>${basedir}/lib/jai_codec-1.1.3.jar</systemPath>
</dependency>

View File

@ -106,3 +106,6 @@ watermark.height = ${WATERMARK_HEIGHT:80}
#水印倾斜度数要求设置在大于等于0小于90
watermark.angle = ${WATERMARK_ANGLE:10}
#Tif类型图片浏览模式tif利用前端js插件浏览jpg转换为jpg后前端显示pdf转换为pdf后显示便于打印
tif.preview.type = ${KK_TIF_PREVIEW_TYPE:tif}

View File

@ -37,6 +37,7 @@ public class ConfigConstants {
private static CopyOnWriteArraySet<String> trustHostSet;
private static String pdfDownloadDisable;
private static Boolean fileUploadDisable;
private static String tifPreviewType;
public static final String DEFAULT_CACHE_ENABLED = "true";
public static final String DEFAULT_TXT_TYPE = "txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd";
@ -52,7 +53,7 @@ public class ConfigConstants {
public static final String DEFAULT_TRUST_HOST = "default";
public static final String DEFAULT_PDF_DOWNLOAD_DISABLE = "true";
public static final String DEFAULT_FILE_UPLOAD_DISABLE = "false";
public static final String DEFAULT_TIF_PREVIEW_TYPE = "jpg";
public static Boolean isCacheEnabled() {
return cacheEnabled;
@ -284,4 +285,18 @@ public class ConfigConstants {
public static void setFileUploadDisableValue(Boolean fileUploadDisable) {
ConfigConstants.fileUploadDisable = fileUploadDisable;
}
public static String getTifPreviewType() {
return tifPreviewType;
}
@Value("${tif.preview.type:tif}")
public void setTifPreviewType(String tifPreviewType) {
setTifPreviewTypeValue(tifPreviewType);
}
public static void setTifPreviewTypeValue(String tifPreviewType) {
ConfigConstants.tifPreviewType = tifPreviewType;
}
}

View File

@ -49,6 +49,8 @@ public class ConfigRefreshComponent {
String trustHost;
String pdfDownloadDisable;
boolean fileUploadDisable;
String tifPreviewType;
while (true) {
FileReader fileReader = new FileReader(configFilePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
@ -68,6 +70,8 @@ public class ConfigRefreshComponent {
trustHost = properties.getProperty("trust.host", ConfigConstants.DEFAULT_TRUST_HOST);
pdfDownloadDisable = properties.getProperty("pdf.download.disable", ConfigConstants.DEFAULT_PDF_DOWNLOAD_DISABLE);
fileUploadDisable = Boolean.parseBoolean(properties.getProperty("file.upload.disable", ConfigConstants.DEFAULT_FILE_UPLOAD_DISABLE));
tifPreviewType = properties.getProperty("tif.preview.type", ConfigConstants.DEFAULT_TIF_PREVIEW_TYPE);
ConfigConstants.setCacheEnabledValueValue(cacheEnabled);
ConfigConstants.setSimTextValue(textArray);
ConfigConstants.setMediaValue(mediaArray);
@ -80,6 +84,7 @@ public class ConfigRefreshComponent {
ConfigConstants.setOfficePreviewSwitchDisabledValue(officePreviewSwitchDisabled);
ConfigConstants.setPdfDownloadDisableValue(pdfDownloadDisable);
ConfigConstants.setFileUploadDisableValue(fileUploadDisable);
ConfigConstants.setTifPreviewTypeValue(tifPreviewType);
setWatermarkConfig(properties);
bufferedReader.close();
fileReader.close();

View File

@ -15,6 +15,7 @@ public interface FilePreview {
String COMPRESS_FILE_PREVIEW_PAGE = "compress";
String MEDIA_FILE_PREVIEW_PAGE = "media";
String PICTURE_FILE_PREVIEW_PAGE = "picture";
String TIFF_FILE_PREVIEW_PAGE = "tiff";
String OFD_FILE_PREVIEW_PAGE = "ofd";
String OFFICE_PICTURE_FILE_PREVIEW_PAGE = "officePicture";
String TXT_FILE_PREVIEW_PAGE = "txt";

View File

@ -7,9 +7,11 @@ import cn.keking.model.ReturnResponse;
import cn.keking.service.FilePreview;
import cn.keking.utils.ConvertPicUtil;
import cn.keking.utils.DownloadUtils;
import cn.keking.utils.WebUtils;
import cn.keking.web.filter.BaseUrlFilter;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import java.io.File;
import java.util.ArrayList;
@ -25,6 +27,9 @@ import java.util.UUID;
public class TiffFilePreviewImpl implements FilePreview {
private final PictureFilePreviewImpl pictureFilePreview;
private static final String INITIALIZE_MEMORY_SIZE = "initializeMemorySize";
//默认初始化 50MB 内存
private static final long INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT = 1024L * 1024 * 50;
private final String fileDir = ConfigConstants.getFileDir();
@ -34,55 +39,72 @@ public class TiffFilePreviewImpl implements FilePreview {
@Override
public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
String inputFileName = url.substring(url.lastIndexOf("/") + 1);
String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1);
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String tiffFileName = uuid + "." + inputFileExt;
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, tiffFileName);
if (response.isFailure()) {
return NOT_SUPPORTED_FILE_PAGE;
}
String strTiffPath = response.getContent();
String tifPreviewType = ConfigConstants.getTifPreviewType();
File fileTiff = new File(strTiffPath);
if("tif".equalsIgnoreCase(tifPreviewType)){
File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg");
pictureFilePreview.filePreviewHandle(url,model,fileAttribute);
String fileSize = WebUtils.getUrlParameterReg(url,INITIALIZE_MEMORY_SIZE);
if(StringUtils.hasText(fileSize)){
model.addAttribute(INITIALIZE_MEMORY_SIZE,fileSize);
}else {
model.addAttribute(INITIALIZE_MEMORY_SIZE,Long.toString(INITIALIZE_MEMORY_SIZE_VALUE_DEFAULT));
}
return TIFF_FILE_PREVIEW_PAGE;
if(fileJpg.exists()){
// 转换后的tif没用了可以删掉了
fileTiff.delete();
}else if("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)){
String inputFileName = url.substring(url.lastIndexOf("/") + 1);
String inputFileExt = inputFileName.substring(inputFileName.lastIndexOf(".") + 1);
String uuid = UUID.randomUUID().toString().replaceAll("-","");
String tiffFileName = uuid + "." + inputFileExt;
String baseUrl = BaseUrlFilter.getBaseUrl();
if("pdf".equalsIgnoreCase(fileAttribute.getOfficePreviewType())){
File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf");
if(filePdf.exists()){
// 转换后的jpg没用了可以删掉了
fileJpg.delete();
ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, tiffFileName);
if (response.isFailure()) {
return NOT_SUPPORTED_FILE_PAGE;
}
String strTiffPath = response.getContent();
String pdfUrl = baseUrl + uuid + ".pdf";
model.addAttribute("pdfUrl", pdfUrl);
File fileTiff = new File(strTiffPath);
return PDF_FILE_PREVIEW_PAGE;
File fileJpg = ConvertPicUtil.convertPic2Jpg(strTiffPath, fileDir + uuid + ".jpg");
if(fileJpg.exists()){
// 转换后的tif没用了可以删掉了
fileTiff.delete();
String baseUrl = BaseUrlFilter.getBaseUrl();
if("pdf".equalsIgnoreCase(tifPreviewType)){
File filePdf = ConvertPicUtil.convertJpg2Pdf(fileDir + uuid + ".jpg", fileDir + uuid + ".pdf");
if(filePdf.exists()){
// 转换后的jpg没用了可以删掉了
fileJpg.delete();
String pdfUrl = baseUrl + uuid + ".pdf";
model.addAttribute("pdfUrl", pdfUrl);
return PDF_FILE_PREVIEW_PAGE;
}
}else{
String jpgUrl = baseUrl + uuid + ".jpg";
fileAttribute.setName(uuid + ".jpg");
fileAttribute.setType(FileType.PICTURE);
fileAttribute.setSuffix("jpg");
fileAttribute.setUrl(jpgUrl);
List<String> imgUrls = new ArrayList<>();
imgUrls.add(jpgUrl);
model.addAttribute("imgUrls", imgUrls);
model.addAttribute("currentUrl", jpgUrl);
}
}else{
String jpgUrl = baseUrl + uuid + ".jpg";
fileAttribute.setName(uuid + ".jpg");
fileAttribute.setType(FileType.PICTURE);
fileAttribute.setSuffix("jpg");
fileAttribute.setUrl(jpgUrl);
List<String> imgUrls = new ArrayList<>();
imgUrls.add(jpgUrl);
model.addAttribute("imgUrls", imgUrls);
model.addAttribute("currentUrl", jpgUrl);
}
return PICTURE_FILE_PREVIEW_PAGE;
}
return PICTURE_FILE_PREVIEW_PAGE;
return NOT_SUPPORTED_FILE_PAGE;
}
}