使用 GroupDocs.Search 為受密碼保護的檔案建立 document index java

在現代企業中,以密碼保護敏感資料是必要的,但這往往會使得快速檢索變得困難,尤其是 create document index java。本教學將完整說明如何使用 GroupDocs.Search for Java 建立受密碼保護檔案的可搜尋索引,同時確保工作流程的安全與高效。

快速解答

  • 本教學涵蓋什麼內容? 使用密碼字典與事件監聽器對受密碼保護的文件進行索引。
  • 需要哪個函式庫? GroupDocs.Search for Java(最新版本)。
  • 是否需要授權? 可取得臨時免費試用授權以進行評估。
  • 可以索引其他檔案類型嗎? 可以,GroupDocs.Search 支援多種格式,如 PDF、DOCX、XLSX 等。
  • 需要哪個 Java 版本? JDK 8 或更高版本。

什麼是 “create document index java”?

在 Java 中建立文件索引是指構建一個可搜尋的資料結構,將關鍵字對應到出現該關鍵字的檔案。使用 GroupDocs.Search 時,此過程會自動處理加密文件,無需手動解鎖每個檔案。

為何使用 GroupDocs.Search 處理受密碼保護的檔案?

  • 零接觸解鎖 – 只需透過字典或事件處理程式一次性提供密碼。
  • 高效能 – 經過最佳化的索引引擎,可擴展至百萬文件。
  • 豐富的查詢語言 – 支援布林運算子、萬用字元與模糊搜尋。
  • 跨格式支援 – 開箱即支援超過 100 種檔案類型。

前置條件

  1. Java Development Kit (JDK) 8+ – 已安裝並在 PATH 中設定。
  2. IDE – IntelliJ IDEA、Eclipse 或任何相容 Java 的編輯器。
  3. Maven – 用於相依性管理。
  4. GroupDocs.Search for Java – 透過 Maven 新增函式庫(見下方)。

設定 GroupDocs.Search for Java

使用 Maven

將以下儲存庫與相依性加入 pom.xml 檔案:

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/search/java/</url>
   </repository>
</repositories>

<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-search</artifactId>
      <version>25.4</version>
   </dependency>
</dependencies>

直接下載

或者,您也可以直接從 GroupDocs.Search for Java releases 下載最新版本。

若要使用試用授權,請前往 GroupDocs’ temporary license page 並依照說明取得免費試用。

如何使用 GroupDocs.Search 進行 create document index java

以下提供兩種實作方式。兩者皆可在自動處理密碼的同時 create document index java

方法 1 – 使用密碼字典進行索引

概觀

將文件密碼儲存在字典中,讓引擎即時解鎖檔案。

步驟 1:定義索引與文件資料夾

String indexFolder = "YOUR_OUTPUT_DIRECTORY/IndexUsingPasswordDictionary";
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY"; // Path to password‑protected documents

步驟 2:建立索引

// Initialize the Index object in the specified directory
Index index = new Index(indexFolder);

步驟 3:新增文件密碼

// Add passwords for specific files using their absolute paths
String path1 = new File(documentsFolder + "/English.docx").getAbsolutePath();
index.getDictionaries().getDocumentPasswords().add(path1, "123456");

String path2 = new File(documentsFolder + "/Lorem ipsum.docx").getAbsolutePath();
index.getDictionaries().getDocumentPasswords().add(path2, "123456");

步驟 4:索引文件

// Automatically retrieve passwords from the dictionary during indexing
index.add(documentsFolder);

步驟 5:在索引中搜尋

String query = "ipsum OR increasing";
SearchResult result = index.search(query);

// Handle search results (e.g., display or process them)

提示: 若檔案數量眾多,建議從安全儲存(資料庫、Azure Key Vault 等)載入密碼,而非硬編碼。

疑難排解

  • 確認每個密碼與檔案實際的保護密碼相符。
  • 再次檢查檔案路徑;錯誤的路徑會拋出 FileNotFoundException

方法 2 – 使用密碼需求事件監聽器進行索引

概觀

當引擎觸發密碼需求事件時,動態提供密碼。

步驟 1:定義索引與文件資料夾

String indexFolder = "YOUR_OUTPUT_DIRECTORY/IndexUsingPasswordEvent";
String documentsFolder = "YOUR_DOCUMENT_DIRECTORY"; // Path to password‑protected documents

步驟 2:建立索引

// Initialize the Index object in the specified directory
Index index = new Index(indexFolder);

步驟 3:訂閱 Password‑Required 事件

index.getEvents().PasswordRequired.add(new EventHandler<PasswordRequiredEventArgs>() {
    @Override
    public void invoke(Object sender, PasswordRequiredEventArgs args) {
        // Provide password for DOCX files when needed
        if (args.getDocumentFullPath().endsWith(".docx")) {
            args.setPassword("123456");
        }
    }
});

步驟 4:索引文件

// The event handler will supply passwords as required during indexing
index.add(documentsFolder);

步驟 5:在索引中搜尋

String query = "ipsum OR increasing";
SearchResult result = index.search(query);

// Handle search results (e.g., display or process them)

疑難排解

  • 確保事件處理程式涵蓋所有需要索引的檔案副檔名。
  • 先以少量樣本檔案測試,確認密碼已正確套用。

實務應用

  1. 企業文件管理: 自動索引機密合約、人力資源檔案與財務報表。
  2. 法律檔案庫: 快速檢索案件檔案,同時保持靜態加密。
  3. 醫療紀錄: 索引患者 PDF 與 Word 文件,避免洩漏 PHI。

效能考量

  • 記憶體配置: 為大型批次分配足夠的堆積記憶體(-Xmx2g 或更高)。
  • 平行索引: 使用 index.addAsync(...) 或啟動多個索引執行緒以提升吞吐量。
  • 索引維護: 定期呼叫 index.optimize() 以壓縮索引並提升查詢速度。

常見問題

Q: 如何處理不同的檔案格式?
A: GroupDocs.Search 支援 PDF、DOCX、XLSX、PPTX 等多種格式。如有需要,請安裝相應的格式外掛。

Q: 若密碼錯誤會發生什麼事?
A: 該文件會被跳過,並記錄警告。請再次確認密碼字典或事件處理程式的邏輯。

Q: 能否索引雲端儲存的檔案?
A: 可以,但必須先下載至本機暫存資料夾,因為引擎只能處理檔案系統路徑。

Q: 如何提升搜尋相關性?
A: 透過 IndexOptions 調整計分設定、使用同義詞,並利用進階查詢語法(如 field:term~ 進行模糊匹配)。

Q: 若部分檔案索引失敗該怎麼辦?
A: 檢查日誌輸出;常見原因包括缺少密碼、檔案損毀或不支援的格式。

資源

依照本指南,您現在已了解如何 create document index java 受密碼保護的檔案,提升應用程式的安全性與可發現性。


最後更新: 2026-01-06
測試環境: GroupDocs.Search 25.4 for Java
作者: GroupDocs