第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用Apache POI解密.doc / docx文件?

如何使用Apache POI解密.doc / docx文件?

慕桂英3389331 2021-04-15 14:15:03
我正在嘗試使用Apache POI打開受密碼保護的.doc文件。但是,我得到了錯誤。org.apache.poi.EncryptedDocumentException:無法處理加密的單詞文件誰能幫我解決這個問題。如果能得到我的代碼,我將不勝感激。
查看完整描述

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等),請查看加密部分以獲取代碼示例。


查看完整回答
反對 回復 2021-04-28
?
GCT1015

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();


 }

}


查看完整回答
反對 回復 2021-04-28
  • 2 回答
  • 0 關(guān)注
  • 514 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號