报表文件(word、excel、pdf、ppt)加密工具类实现。
1 pom文件
<!-- 报表相关架包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.1.4</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>fr.opensagres.xdocreport</groupId>
<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
<version>${xdocreport.version}</version>
</dependency>
<!-- 报表相关架包 结束 -->
2 代码实现
package com.loep.ie.data.service.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.poifs.crypt.EncryptionInfo;
import org.apache.poi.poifs.crypt.EncryptionMode;
import org.apache.poi.poifs.crypt.Encryptor;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.itextpdf.kernel.pdf.EncryptionConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.ReaderProperties;
import com.itextpdf.kernel.pdf.WriterProperties;
public class FileEncryUtils{
private static Logger LOGGER = LoggerFactory.getLogger(FileEncryUtils.class);
/**
* 加密WORD文档(doc)
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
* @throws Exception
*/
public static boolean encrypDOC(String sourceFilePath, String targetFilePath, String password) throws Exception{
return encrypDOC(sourceFilePath, new FileOutputStream(targetFilePath), password);
}
public static boolean encrypDOC(String sourceFilePath, OutputStream out, String password){
POIFSFileSystem fs = null;
HWPFDocument doc = null;
try {
fs = new POIFSFileSystem(new FileInputStream(sourceFilePath));
doc = new HWPFDocument(fs);
Biff8EncryptionKey.setCurrentUserPassword(password);
doc.write(out);
return true;
} catch (Exception e) {
LOGGER.error("DOC文档加密失败:{}",e.getMessage());
return false;
}finally {
FileUtils.close(doc);
FileUtils.close(fs);
FileUtils.close(out);
}
}
/**
* 加密EXCEL文档(xls)
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
* @throws Exception
*/
public static boolean encrypXLS(String sourceFilePath, String targetFilePath, String password) throws Exception{
return encrypXLS(sourceFilePath, new FileOutputStream(targetFilePath), password);
}
public static boolean encrypXLS(String sourceFilePath, OutputStream out, String password){
POIFSFileSystem fs = null;
HSSFWorkbook hwb = null;
try {
fs = new POIFSFileSystem(new FileInputStream(sourceFilePath));
hwb = new HSSFWorkbook(fs);
Biff8EncryptionKey.setCurrentUserPassword(password);
hwb.write(out);
return true;
} catch (Exception e) {
LOGGER.error("XLS文档加密失败:{}",e.getMessage());
return false;
}finally {
FileUtils.close(hwb);
FileUtils.close(fs);
FileUtils.close(out);
}
}
/**
* 加密PPT文档(ppt)
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
* @throws Exception
*/
public static boolean encrypPPT(String sourceFilePath, String targetFilePath, String password) throws Exception{
return encrypPPT(sourceFilePath, new FileOutputStream(targetFilePath), password);
}
public static boolean encrypPPT(String sourceFilePath, OutputStream out, String password){
POIFSFileSystem fs = null;
HSLFSlideShow hss = null;
try {
fs = new POIFSFileSystem(new FileInputStream(sourceFilePath));
hss = new HSLFSlideShow(fs);
Biff8EncryptionKey.setCurrentUserPassword(password);
hss.write(out);
return true;
} catch (Exception e) {
LOGGER.error("PPT文档加密失败:{}",e.getMessage());
return false;
}finally {
FileUtils.close(hss);
FileUtils.close(fs);
FileUtils.close(out);
}
}
/**
* 加密PDF文档
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
* @throws Exception
*/
public static boolean encrypPDF(String sourceFilePath, String targetFilePath, String password) throws Exception{
return encrypPDF(sourceFilePath, new FileOutputStream(targetFilePath), password);
}
public static boolean encrypPDF(String sourceFilePath, OutputStream out, String password){
PdfDocument pdfDoc = null;
PdfReader reader = null;
try {
reader = new PdfReader(sourceFilePath, new ReaderProperties().setPassword("World".getBytes()));
PdfWriter writer = new PdfWriter(out,
new WriterProperties().setStandardEncryption(password.getBytes(), password.getBytes(),
EncryptionConstants.EMBEDDED_FILES_ONLY,
EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
pdfDoc = new PdfDocument(reader, writer);
return true;
} catch (Exception e) {
LOGGER.error("PDF文档加密失败:{}",e.getMessage());
return false;
}finally {
FileUtils.close(pdfDoc);
FileUtils.close(reader);
}
}
/**
* 加密xml文档(docx,xlsx,pptx)
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
* @throws Exception
*/
public static boolean encrypXML(String sourceFilePath, String targetFilePath, String password) throws Exception{
return encrypXML(sourceFilePath, new FileOutputStream(targetFilePath), password);
}
public static boolean encrypXML(String sourceFilePath, OutputStream out, String password) {
POIFSFileSystem fs = null;
try {
fs = new POIFSFileSystem();
EncryptionInfo info = new EncryptionInfo(EncryptionMode.agile);
Encryptor enc = info.getEncryptor();
enc.confirmPassword(password);
try (OPCPackage opc = OPCPackage.open(new File(sourceFilePath), PackageAccess.READ_WRITE);
OutputStream os = enc.getDataStream(fs)) {
opc.save(os);
}
fs.writeFilesystem(out);
return true;
} catch (Exception e) {
LOGGER.error("XML文档加密失败:{}",e.getMessage());
return false;
} finally {
FileUtils.close(fs);
FileUtils.close(out);
}
}
/**
* 加密文档
* @param sourceFilePath 源文件
* @param targetFilePath 目标文件
* @param password 密码
* @return
*/
public static boolean encryFile(String sourceFilePath, String targetFilePath, String password){
try {
return encryFile(sourceFilePath, new FileOutputStream(targetFilePath), password);
} catch (Exception e) {
LOGGER.error("文档加密失败:{}",e.getMessage());
}
return false;
}
public static boolean encryFile(String sourceFilePath, OutputStream out, String password){
boolean flag = false;
try {
int index = sourceFilePath.indexOf(".");
if(index > 0) {
String suffix = sourceFilePath.substring(index+1);
if("doc".equalsIgnoreCase(suffix)) {
flag = encrypDOC(sourceFilePath, out, password);
}else if("xls".equalsIgnoreCase(suffix)) {
flag = encrypXLS(sourceFilePath, out, password);
}else if("ppt".equalsIgnoreCase(suffix)) {
flag = encrypPPT(sourceFilePath, out, password);
}else if("pdf".equalsIgnoreCase(suffix)) {
flag = encrypPDF(sourceFilePath, out, password);
}else if("docx".equalsIgnoreCase(suffix) || "xlsx".equalsIgnoreCase(suffix) || "pptx".equalsIgnoreCase(suffix)){
flag = encrypXML(sourceFilePath, out, password);
}else {
LOGGER.warn("无法识别的文件类型");
}
}
} catch (Exception e) {
LOGGER.error("文档加密失败:{}",e.getMessage());
}
return flag;
}
}