mirror of
https://gitee.com/kekingcn/file-online-preview.git
synced 2025-04-05 17:37:49 +08:00
CAD格式新增支持 转换成svg tif 格式
CAD 转换新增 超时结束方法
This commit is contained in:
parent
778a381b5b
commit
6f8416365f
@ -137,4 +137,6 @@ delete.source.file = ${KK_DELETE_SOURCE_FILE:true}
|
||||
#配置PDF文件生成图片的像素大小,dpi 越高,图片质量越清晰,同时也会消耗更多的计算资源。
|
||||
pdf2jpg.dpi = ${KK_PDF2JPG_DPI:144}
|
||||
#xlsx格式前端解析
|
||||
office.type.web = ${KK_OFFICE_TYPE_WEB:web}
|
||||
office.type.web = ${KK_OFFICE_TYPE_WEB:web}
|
||||
#Cad类型图片浏览模式:tif(利用前端js插件浏览);svg(转换为svg显示);pdf(转换为pdf后显示,便于打印)
|
||||
cad.preview.type = ${KK_CAD_PREVIEW_TYPE:pdf}
|
@ -48,6 +48,7 @@ public class ConfigConstants {
|
||||
private static String password;
|
||||
private static int pdf2JpgDpi;
|
||||
private static String officeTypeWeb;
|
||||
private static String cadPreviewType;
|
||||
private static Boolean deleteSourceFile;
|
||||
|
||||
public static final String DEFAULT_CACHE_ENABLED = "true";
|
||||
@ -69,6 +70,7 @@ public class ConfigConstants {
|
||||
public static final String DEFAULT_PDF_BOOKMARK_DISABLE = "true";
|
||||
public static final String DEFAULT_FILE_UPLOAD_DISABLE = "false";
|
||||
public static final String DEFAULT_TIF_PREVIEW_TYPE = "tif";
|
||||
public static final String DEFAULT_CAD_PREVIEW_TYPE = "pdf";
|
||||
public static final String DEFAULT_BEIAN = "无";
|
||||
public static final String DEFAULT_SIZE = "500MB";
|
||||
public static final String DEFAULT_PROHIBIT = "exe,dll";
|
||||
@ -445,6 +447,21 @@ public class ConfigConstants {
|
||||
setDeleteSourceFileValue(deleteSourceFile);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static String getCadPreviewType() {
|
||||
return cadPreviewType;
|
||||
}
|
||||
|
||||
@Value("${cad.preview.type:pdf}")
|
||||
public void setCadPreviewType(String cadPreviewType) {
|
||||
setCadPreviewTypeValue(cadPreviewType);
|
||||
}
|
||||
|
||||
public static void setCadPreviewTypeValue(String cadPreviewType) {
|
||||
ConfigConstants.cadPreviewType = cadPreviewType;
|
||||
}
|
||||
|
||||
public static void setDeleteSourceFileValue(Boolean deleteSourceFile) {
|
||||
ConfigConstants.deleteSourceFile = deleteSourceFile;
|
||||
}
|
||||
|
@ -60,6 +60,7 @@ public class ConfigRefreshComponent {
|
||||
String password;
|
||||
int pdf2JpgDpi;
|
||||
String officeTypeWeb;
|
||||
String cadPreviewType;
|
||||
boolean deleteSourceFile;
|
||||
while (true) {
|
||||
FileReader fileReader = new FileReader(configFilePath);
|
||||
@ -85,6 +86,7 @@ public class ConfigRefreshComponent {
|
||||
pdfBookmarkDisable = properties.getProperty("pdf.bookmark.disable", ConfigConstants.DEFAULT_PDF_BOOKMARK_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);
|
||||
cadPreviewType = properties.getProperty("cad.preview.type", ConfigConstants.DEFAULT_CAD_PREVIEW_TYPE);
|
||||
size = properties.getProperty("spring.servlet.multipart.max-file-size", ConfigConstants.DEFAULT_SIZE);
|
||||
beian = properties.getProperty("beian", ConfigConstants.DEFAULT_BEIAN);
|
||||
prohibit = properties.getProperty("prohibit", ConfigConstants.DEFAULT_PROHIBIT);
|
||||
@ -111,6 +113,7 @@ public class ConfigRefreshComponent {
|
||||
ConfigConstants.setPdfBookmarkDisableValue(pdfBookmarkDisable);
|
||||
ConfigConstants.setFileUploadDisableValue(fileUploadDisable);
|
||||
ConfigConstants.setTifPreviewTypeValue(tifPreviewType);
|
||||
ConfigConstants.setCadPreviewTypeValue(cadPreviewType);
|
||||
ConfigConstants.setBeianValue(beian);
|
||||
ConfigConstants.setSizeValue(size);
|
||||
ConfigConstants.setProhibitValue(prohibitArray);
|
||||
|
@ -9,12 +9,13 @@ import cn.keking.utils.EncodingDetects;
|
||||
import cn.keking.utils.KkFileUtils;
|
||||
import cn.keking.utils.WebUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import com.aspose.cad.CodePages;
|
||||
import com.aspose.cad.Color;
|
||||
import com.aspose.cad.Image;
|
||||
import com.aspose.cad.LoadOptions;
|
||||
import com.aspose.cad.*;
|
||||
import com.aspose.cad.fileformats.tiff.enums.TiffExpectedFormat;
|
||||
import com.aspose.cad.imageoptions.CadRasterizationOptions;
|
||||
import com.aspose.cad.imageoptions.PdfOptions;
|
||||
import com.aspose.cad.imageoptions.SvgOptions;
|
||||
import com.aspose.cad.imageoptions.TiffOptions;
|
||||
import com.aspose.cad.internal.Exceptions.TimeoutException;
|
||||
import com.itextpdf.text.pdf.PdfReader;
|
||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||
import org.apache.pdfbox.pdmodel.PDDocument;
|
||||
@ -38,6 +39,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
/**
|
||||
@ -52,7 +54,7 @@ public class FileHandlerService {
|
||||
private final Logger logger = LoggerFactory.getLogger(FileHandlerService.class);
|
||||
private final String fileDir = ConfigConstants.getFileDir();
|
||||
private final CacheService cacheService;
|
||||
|
||||
private final InterruptionTokenSource source = new com.aspose.cad.InterruptionTokenSource(); //CAD延时
|
||||
@Value("${server.tomcat.uri-encoding:UTF-8}")
|
||||
private String uriEncoding;
|
||||
|
||||
@ -143,6 +145,10 @@ public class FileHandlerService {
|
||||
cacheService.putImgCache(fileKey, imgs);
|
||||
}
|
||||
|
||||
/**
|
||||
cad定义线程池
|
||||
*/
|
||||
private static final ExecutorService pool = Executors.newFixedThreadPool(1);
|
||||
/**
|
||||
* 对转换后的文件进行操作(改变编码方式)
|
||||
*
|
||||
@ -297,11 +303,12 @@ public class FileHandlerService {
|
||||
* @param outputFilePath pdf输出文件路径
|
||||
* @return 转换是否成功
|
||||
*/
|
||||
public String cadToPdf(String inputFilePath, String outputFilePath) throws Exception {
|
||||
public String cadToPdf(String inputFilePath, String outputFilePath ,String cadPreviewType) throws Exception {
|
||||
Callable<String> call = () -> {
|
||||
File outputFile = new File(outputFilePath);
|
||||
LoadOptions opts = new LoadOptions();
|
||||
opts.setSpecifiedEncoding(CodePages.SimpChinese);
|
||||
com.aspose.cad.Image cadImage = Image.load(inputFilePath, opts);
|
||||
Image cadImage = Image.load(inputFilePath, opts);
|
||||
CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
|
||||
cadRasterizationOptions.setBackgroundColor(Color.getWhite());
|
||||
cadRasterizationOptions.setPageWidth(1400);
|
||||
@ -309,12 +316,40 @@ public class FileHandlerService {
|
||||
cadRasterizationOptions.setAutomaticLayoutsScaling(true);
|
||||
cadRasterizationOptions.setNoScaling(false);
|
||||
cadRasterizationOptions.setDrawType(1);
|
||||
PdfOptions pdfOptions = new PdfOptions();
|
||||
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
||||
SvgOptions SvgOptions = null;
|
||||
PdfOptions pdfOptions = null;
|
||||
TiffOptions TiffOptions = null;
|
||||
switch (cadPreviewType) { //新增格式方法
|
||||
case "svg":
|
||||
SvgOptions = new SvgOptions();
|
||||
SvgOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
||||
SvgOptions.setInterruptionToken(source.getToken());
|
||||
break;
|
||||
case "pdf":
|
||||
pdfOptions = new PdfOptions();
|
||||
pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
||||
pdfOptions.setInterruptionToken(source.getToken());
|
||||
break;
|
||||
case "tif":
|
||||
TiffOptions = new TiffOptions(TiffExpectedFormat.TiffJpegRgb);
|
||||
TiffOptions.setVectorRasterizationOptions(cadRasterizationOptions);
|
||||
TiffOptions.setInterruptionToken(source.getToken());
|
||||
break;
|
||||
}
|
||||
OutputStream stream = null;
|
||||
try {
|
||||
stream = new FileOutputStream(outputFile);
|
||||
cadImage.save(stream, pdfOptions);
|
||||
switch (cadPreviewType) {
|
||||
case "svg":
|
||||
cadImage.save(stream, SvgOptions);
|
||||
break;
|
||||
case "pdf":
|
||||
cadImage.save(stream, pdfOptions);
|
||||
break;
|
||||
case "tif":
|
||||
cadImage.save(stream, TiffOptions);
|
||||
break;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
|
||||
return "null";
|
||||
@ -325,6 +360,27 @@ public class FileHandlerService {
|
||||
if (cadImage != null) { //关闭
|
||||
cadImage.close();
|
||||
}
|
||||
source.interrupt(); //结束任务
|
||||
source.dispose();
|
||||
}
|
||||
return "true";
|
||||
};
|
||||
Future<String> result = pool.submit(call);
|
||||
try {
|
||||
// 如果在超时时间内,没有数据返回:则抛出TimeoutException异常
|
||||
result.get(60, TimeUnit.SECONDS);
|
||||
} catch (InterruptedException e) {
|
||||
System.out.println("InterruptedException发生");
|
||||
return "null";
|
||||
} catch (ExecutionException e) {
|
||||
System.out.println("ExecutionException发生");
|
||||
return "null";
|
||||
} catch (TimeoutException e) {
|
||||
System.out.println("TimeoutException发生,意味着线程超时报错");
|
||||
return "null";
|
||||
} finally {
|
||||
source.interrupt(); //结束任务
|
||||
source.dispose();
|
||||
}
|
||||
return "true";
|
||||
}
|
||||
|
@ -3,17 +3,15 @@ package cn.keking.service.impl;
|
||||
import cn.keking.config.ConfigConstants;
|
||||
import cn.keking.model.FileAttribute;
|
||||
import cn.keking.model.ReturnResponse;
|
||||
import cn.keking.service.FileHandlerService;
|
||||
import cn.keking.service.FilePreview;
|
||||
import cn.keking.utils.DownloadUtils;
|
||||
import cn.keking.service.FileHandlerService;
|
||||
import cn.keking.utils.KkFileUtils;
|
||||
import cn.keking.web.filter.BaseUrlFilter;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
|
||||
|
||||
/**
|
||||
@ -43,7 +41,8 @@ public class CadFilePreviewImpl implements FilePreview {
|
||||
boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
|
||||
String fileName = fileAttribute.getName();
|
||||
String suffix = fileAttribute.getSuffix();
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
|
||||
String cadPreviewType = ConfigConstants.getCadPreviewType();
|
||||
String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + cadPreviewType ; //生成文件添加类型后缀 防止同名文件
|
||||
String outFilePath = FILE_DIR + pdfName;
|
||||
// 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
|
||||
if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
|
||||
@ -56,7 +55,7 @@ public class CadFilePreviewImpl implements FilePreview {
|
||||
String imageUrls = null;
|
||||
if (StringUtils.hasText(outFilePath)) {
|
||||
try {
|
||||
imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath);
|
||||
imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath,cadPreviewType);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -73,12 +72,17 @@ public class CadFilePreviewImpl implements FilePreview {
|
||||
}
|
||||
}
|
||||
}
|
||||
if("tif".equalsIgnoreCase(cadPreviewType)){
|
||||
model.addAttribute("currentUrl", pdfName);
|
||||
return TIFF_FILE_PREVIEW_PAGE;
|
||||
}else if("svg".equalsIgnoreCase(cadPreviewType)){
|
||||
model.addAttribute("currentUrl", pdfName);
|
||||
return SVG_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
|
||||
return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview);
|
||||
}
|
||||
model.addAttribute("pdfUrl", pdfName);
|
||||
return PDF_FILE_PREVIEW_PAGE;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user