Commit bf0e9ba0 authored by liangjf's avatar liangjf

fix: 转换格式异常后缓存处理

parent 1191cfc6
...@@ -116,6 +116,7 @@ public abstract class AbstractConversionTask implements OfficeTask { ...@@ -116,6 +116,7 @@ public abstract class AbstractConversionTask implements OfficeTask {
try { try {
cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties)); cast(XStorable.class, document).storeToURL(toUrl(outputFile), toUnoProperties(storeProperties));
} catch (ErrorCodeIOException errorCodeIOException) { } catch (ErrorCodeIOException errorCodeIOException) {
throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException); throw new OfficeException("could not store document: " + outputFile.getName() + "; errorCode: " + errorCodeIOException.ErrCode, errorCodeIOException);
} catch (IOException ioException) { } catch (IOException ioException) {
throw new OfficeException("could not store document: " + outputFile.getName(), ioException); throw new OfficeException("could not store document: " + outputFile.getName(), ioException);
......
package cn.keking.service; package cn.keking.service;
import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.OfficeException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -20,22 +21,29 @@ public class OfficeToPdfService { ...@@ -20,22 +21,29 @@ public class OfficeToPdfService {
this.officePluginManager = officePluginManager; this.officePluginManager = officePluginManager;
} }
public void openOfficeToPDF(String inputFilePath, String outputFilePath) { public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
office2pdf(inputFilePath, outputFilePath); return office2pdf(inputFilePath, outputFilePath);
} }
public static void converterFile(File inputFile, String outputFilePath_end, OfficeDocumentConverter converter) { public static boolean converterFile(File inputFile, String outputFilePath_end, OfficeDocumentConverter converter) {
File outputFile = new File(outputFilePath_end); File outputFile = new File(outputFilePath_end);
// 假如目标路径不存在,则新建该路径 // 假如目标路径不存在,则新建该路径
if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) { if (!outputFile.getParentFile().exists() && !outputFile.getParentFile().mkdirs()) {
logger.error("创建目录【{}】失败,请检查目录权限!",outputFilePath_end); logger.error("创建目录【{}】失败,请检查目录权限!",outputFilePath_end);
} }
converter.convert(inputFile, outputFile); try{
converter.convert(inputFile, outputFile);
} catch (OfficeException e) {
logger.error("{}",e);
return false;
}
return true;
} }
public void office2pdf(String inputFilePath, String outputFilePath) { public boolean office2pdf(String inputFilePath, String outputFilePath) {
OfficeDocumentConverter converter = officePluginManager.getDocumentConverter(); OfficeDocumentConverter converter = officePluginManager.getDocumentConverter();
if (null != inputFilePath) { if (null != inputFilePath) {
File inputFile = new File(inputFilePath); File inputFile = new File(inputFilePath);
...@@ -45,15 +53,16 @@ public class OfficeToPdfService { ...@@ -45,15 +53,16 @@ public class OfficeToPdfService {
String outputFilePath_end = getOutputFilePath(inputFilePath); String outputFilePath_end = getOutputFilePath(inputFilePath);
if (inputFile.exists()) { if (inputFile.exists()) {
// 找不到源文件, 则返回 // 找不到源文件, 则返回
converterFile(inputFile, outputFilePath_end,converter); return converterFile(inputFile, outputFilePath_end,converter);
} }
} else { } else {
if (inputFile.exists()) { if (inputFile.exists()) {
// 找不到源文件, 则返回 // 找不到源文件, 则返回
converterFile(inputFile, outputFilePath, converter); return converterFile(inputFile, outputFilePath, converter);
} }
} }
} }
return false;
} }
public static String getOutputFilePath(String inputFilePath) { public static String getOutputFilePath(String inputFilePath) {
......
...@@ -54,12 +54,13 @@ public class OfficeFilePreviewImpl implements FilePreview { ...@@ -54,12 +54,13 @@ public class OfficeFilePreviewImpl implements FilePreview {
} }
filePath = response.getContent(); filePath = response.getContent();
if (StringUtils.hasText(outFilePath)) { if (StringUtils.hasText(outFilePath)) {
officeToPdfService.openOfficeToPDF(filePath, outFilePath); // 这里将office文件转成pdf了,转换时出错还继续加缓存,加个布尔值限制缓存行为
boolean cast = officeToPdfService.openOfficeToPDF(filePath, outFilePath);
if (isHtml) { if (isHtml) {
// 对转换后的文件进行操作(改变编码方式) // 对转换后的文件进行操作(改变编码方式)
fileHandlerService.doActionConvertedFile(outFilePath); fileHandlerService.doActionConvertedFile(outFilePath);
} }
if (ConfigConstants.isCacheEnabled()) { if (cast && ConfigConstants.isCacheEnabled()) {
// 加入缓存 // 加入缓存
fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath)); fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment