jodconverter转换带密码office文件
parent
d761d0cc88
commit
f52169ec27
|
@ -1,12 +1,17 @@
|
||||||
package cn.keking.service;
|
package cn.keking.service;
|
||||||
|
|
||||||
|
import cn.keking.model.FileAttribute;
|
||||||
|
import com.sun.star.document.UpdateDocMode;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.jodconverter.core.office.OfficeException;
|
import org.jodconverter.core.office.OfficeException;
|
||||||
import org.jodconverter.local.JodConverter;
|
import org.jodconverter.local.LocalConverter;
|
||||||
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;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yudian-it
|
* @author yudian-it
|
||||||
|
@ -16,22 +21,33 @@ public class OfficeToPdfService {
|
||||||
|
|
||||||
private final static Logger logger = LoggerFactory.getLogger(OfficeToPdfService.class);
|
private final static Logger logger = LoggerFactory.getLogger(OfficeToPdfService.class);
|
||||||
|
|
||||||
public void openOfficeToPDF(String inputFilePath, String outputFilePath) throws OfficeException {
|
public void openOfficeToPDF(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException {
|
||||||
office2pdf(inputFilePath, outputFilePath);
|
office2pdf(inputFilePath, outputFilePath, fileAttribute);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void converterFile(File inputFile, String outputFilePath_end) throws OfficeException {
|
public static void converterFile(File inputFile, String outputFilePath_end, FileAttribute fileAttribute) throws OfficeException {
|
||||||
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);
|
||||||
}
|
}
|
||||||
JodConverter.convert(inputFile).to(outputFile).execute();
|
LocalConverter.Builder builder;
|
||||||
|
if (StringUtils.isNotBlank(fileAttribute.getFilePassword())) {
|
||||||
|
Map<String, Object> loadProperties = new HashMap<>();
|
||||||
|
loadProperties.put("Hidden", true);
|
||||||
|
loadProperties.put("ReadOnly", true);
|
||||||
|
loadProperties.put("UpdateDocMode", UpdateDocMode.NO_UPDATE);
|
||||||
|
loadProperties.put("Password", fileAttribute.getFilePassword());
|
||||||
|
builder = LocalConverter.builder().loadProperties(loadProperties);
|
||||||
|
} else {
|
||||||
|
builder = LocalConverter.builder();
|
||||||
|
}
|
||||||
|
builder.build().convert(inputFile).to(outputFile).execute();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void office2pdf(String inputFilePath, String outputFilePath) throws OfficeException {
|
public void office2pdf(String inputFilePath, String outputFilePath, FileAttribute fileAttribute) throws OfficeException {
|
||||||
if (null != inputFilePath) {
|
if (null != inputFilePath) {
|
||||||
File inputFile = new File(inputFilePath);
|
File inputFile = new File(inputFilePath);
|
||||||
// 判断目标文件路径是否为空
|
// 判断目标文件路径是否为空
|
||||||
|
@ -40,12 +56,12 @@ public class OfficeToPdfService {
|
||||||
String outputFilePath_end = getOutputFilePath(inputFilePath);
|
String outputFilePath_end = getOutputFilePath(inputFilePath);
|
||||||
if (inputFile.exists()) {
|
if (inputFile.exists()) {
|
||||||
// 找不到源文件, 则返回
|
// 找不到源文件, 则返回
|
||||||
converterFile(inputFile, outputFilePath_end);
|
converterFile(inputFile, outputFilePath_end, fileAttribute);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (inputFile.exists()) {
|
if (inputFile.exists()) {
|
||||||
// 找不到源文件, 则返回
|
// 找不到源文件, 则返回
|
||||||
converterFile(inputFile, outputFilePath);
|
converterFile(inputFile, outputFilePath, fileAttribute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -92,7 +92,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
|
||||||
} else {
|
} else {
|
||||||
if (StringUtils.hasText(outFilePath)) {
|
if (StringUtils.hasText(outFilePath)) {
|
||||||
try {
|
try {
|
||||||
officeToPdfService.openOfficeToPDF(filePath, outFilePath);
|
officeToPdfService.openOfficeToPDF(filePath, outFilePath, fileAttribute);
|
||||||
} catch (OfficeException e) {
|
} catch (OfficeException e) {
|
||||||
if (isPwdProtectedOffice && !OfficeUtils.isCompatible(filePath, filePassword)) {
|
if (isPwdProtectedOffice && !OfficeUtils.isCompatible(filePath, filePassword)) {
|
||||||
// 加密文件密码错误,提示重新输入
|
// 加密文件密码错误,提示重新输入
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
package cn.keking.utils;
|
package cn.keking.utils;
|
||||||
|
|
||||||
import org.apache.commons.lang3.exception.ExceptionUtils;
|
import org.apache.commons.lang3.exception.ExceptionUtils;
|
||||||
|
import org.apache.poi.EncryptedDocumentException;
|
||||||
import org.apache.poi.extractor.ExtractorFactory;
|
import org.apache.poi.extractor.ExtractorFactory;
|
||||||
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
|
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Office工具类
|
* Office工具类
|
||||||
|
@ -16,8 +16,6 @@ import java.io.IOException;
|
||||||
*/
|
*/
|
||||||
public class OfficeUtils {
|
public class OfficeUtils {
|
||||||
|
|
||||||
private static final String POI_INVALID_PASSWORD_MSG = "Invalid password specified";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 判断office(word,excel,ppt)文件是否受密码保护
|
* 判断office(word,excel,ppt)文件是否受密码保护
|
||||||
*
|
*
|
||||||
|
@ -27,14 +25,12 @@ public class OfficeUtils {
|
||||||
public static boolean isPwdProtected(String path) {
|
public static boolean isPwdProtected(String path) {
|
||||||
try {
|
try {
|
||||||
ExtractorFactory.createExtractor(new FileInputStream(path));
|
ExtractorFactory.createExtractor(new FileInputStream(path));
|
||||||
} catch (IOException e) {
|
} catch (EncryptedDocumentException e) {
|
||||||
if (POI_INVALID_PASSWORD_MSG.equals(e.getMessage())) {
|
return true;
|
||||||
return true;
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
|
||||||
for (Throwable throwable : throwableArray) {
|
for (Throwable throwable : throwableArray) {
|
||||||
if (throwable instanceof IOException && POI_INVALID_PASSWORD_MSG.equals(throwable.getMessage())) {
|
if (throwable instanceof EncryptedDocumentException) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, user-scalable=yes, initial-scale=1.0">
|
||||||
<title>文件预览</title>
|
<title>文件预览</title>
|
||||||
<#include "*/commonHeader.ftl">
|
<#include "*/commonHeader.ftl">
|
||||||
|
<#include "*/needFilePasswordHeader.ftl">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<iframe src="${pdfUrl}" width="100%" frameborder="0"></iframe>
|
<iframe src="${pdfUrl}" width="100%" frameborder="0"></iframe>
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
<#setting classic_compatible=true>
|
||||||
|
<link rel="icon" href="./favicon.ico" type="image/x-icon">
|
||||||
|
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css"/>
|
||||||
|
<script src="js/jquery-3.6.1.min.js" type="text/javascript"></script>
|
||||||
|
<script src="bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
|
||||||
|
<script src="js/bootbox.min.js" type="text/javascript"></script>
|
||||||
|
<script>
|
||||||
|
// 中文环境
|
||||||
|
var locale_zh_CN = {
|
||||||
|
OK: '确定',
|
||||||
|
CONFIRM: '确认',
|
||||||
|
CANCEL: '取消'
|
||||||
|
};
|
||||||
|
bootbox.addLocale('locale_zh_CN', locale_zh_CN);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 需要文件密码
|
||||||
|
*/
|
||||||
|
function needFilePassword() {
|
||||||
|
if ('${needFilePassword}' == 'true') {
|
||||||
|
let promptTitle = "你正在预览加密文件,请输入文件密码。";
|
||||||
|
if ('${filePasswordError}' == 'true') {
|
||||||
|
promptTitle = "密码错误,请重新输入密码。";
|
||||||
|
}
|
||||||
|
bootbox.prompt({
|
||||||
|
title: promptTitle,
|
||||||
|
inputType: 'password',
|
||||||
|
centerVertical: true,
|
||||||
|
locale: 'locale_zh_CN',
|
||||||
|
callback: function (filePassword) {
|
||||||
|
if (filePassword != null) {
|
||||||
|
const locationHref = window.location.href;
|
||||||
|
const isInclude = locationHref.includes("filePassword=");
|
||||||
|
let redirectUrl = null;
|
||||||
|
if (isInclude) {
|
||||||
|
const url = new URL(locationHref);
|
||||||
|
url.searchParams.set("filePassword", filePassword);
|
||||||
|
redirectUrl = url.href;
|
||||||
|
} else {
|
||||||
|
redirectUrl = locationHref + '&filePassword=' + filePassword;
|
||||||
|
}
|
||||||
|
window.location.replace(redirectUrl);
|
||||||
|
} else {
|
||||||
|
location.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
* {
|
||||||
|
margin: 0;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
html, body {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
Loading…
Reference in New Issue