2 回答

TA貢獻1828條經(jīng)驗 獲得超3個贊
一個EncryptedDocumentException信號,你正在嘗試處理先前沒有“解鎖”的加密文檔。
以下代碼段適合檢查基于XML的格式(.xlsx,.pptx,.docx等)是否適用,以便以后可以安全地對其進行處理:
String password = "secret"; // set password
File fileToProcess; // obtain/read/open the file here....
NPOIFSFileSystem filesystem = new NPOIFSFileSystem(fileToProcess);
EncryptionInfo info = new EncryptionInfo(filesystem);
Decryptor d = Decryptor.getInstance(info);
try {
if (!d.verifyPassword(password)) {
throw new RuntimeException("Unable to process: document is encrypted");
}
InputStream dataStream = d.getDataStream(filesystem);
// parse dataStream as the document is now processable from here on
// ...
} catch (GeneralSecurityException ex) {
throw new RuntimeException("Unable to process encrypted document", ex);
}
上面的示例摘自官方POI文檔的加密部分,并根據(jù)項目的JavaDoc進行了修改。您可能要檢查/閱讀類Decryptor和/或的JavaDoc NPOIFSFileSystem。
如果要轉(zhuǎn)換二進制文件格式(.xls,.ppt,.doc等),請查看加密部分以獲取代碼示例。

TA貢獻1827條經(jīng)驗 獲得超4個贊
由于問題最初是關(guān)于解密二進制*.doc格式的,所以:
Apache POI加密支持中的代碼二進制格式需要稍作更新以用于HWPF。無法從創(chuàng)建HWPFDocumentNPOIFSFileSystem。有POIFSFileSystem需要。但是其他都是一樣的。
在file.doc運行此代碼后,使用密碼“ pass”進行了加密,新文件將fileDecrypted.doc被解密,并且無需密碼即可打開。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class ReadEncryptedHWPF {
public static void main(String[] args) throws Exception {
Biff8EncryptionKey.setCurrentUserPassword("pass");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream("file.doc"));
HWPFDocument doc = new HWPFDocument(fs);
Biff8EncryptionKey.setCurrentUserPassword(null);
doc.write(new FileOutputStream("fileDecrypted.doc"));
doc.close();
doc = new HWPFDocument(new FileInputStream("fileDecrypted.doc"));
org.apache.poi.hwpf.extractor.WordExtractor extractor = new org.apache.poi.hwpf.extractor.WordExtractor(doc);
System.out.println(extractor.getText());
extractor.close();
}
}
添加回答
舉報