取得檔案類型 Java – 使用 GroupDocs 擷取文件中繼資料

如果您需要 get file type java 並取得頁數、大小或作者資訊等細節,您來對地方了。無論您是在建置文件管理系統、法律科技工作流程,或是簡單的批次整理工具,程式化擷取中繼資料都能節省大量手動工作時間,並避免人為錯誤。在本教學中,我們將從基本設定到進階效能調校,完整說明如何使用 GroupDocs.Comparison 取得文件中繼資料。

快速解答

  • java get file type 是什麼意思? 這表示在 Java 應用程式中以程式方式判斷文件的格式(PDF、DOCX、PPTX 等)。
  • 我也可以取得 PDF 的頁數嗎? 可以 – 相同的 API 呼叫會回傳 info.getPageCount() 供 PDF 使用。
  • 我需要授權嗎? 免費試用可用於評估;完整授權會移除浮水印與使用限制。
  • 需要哪個 Java 版本? 支援 JDK 8+;JDK 11+ 提供更佳的記憶體處理與效能。
  • 這適合大量批次處理嗎? 絕對適合 – 只要妥善管理資源,即可同時處理上千個檔案。

什麼是 get file type java?

Get file type java 是指使用 Java 程式碼直接從二進位內容偵測文件格式的操作。GroupDocs.Comparison 會讀取檔案標頭、判斷 MIME 類型,並透過 IDocumentInfo 物件公開,讓您在不依賴副檔名的情況下取得格式資訊。

為什麼要使用 GroupDocs 擷取文件中繼資料?

GroupDocs.Comparison 支援 100+ 輸入與輸出格式——包括 PDF、DOCX、XLSX、PPTX、HTML 以及超過 30 種影像格式,且能在不將整個文件載入記憶體的情況下處理上百頁的檔案。此量化能力使其成為高容量、企業級管線的理想選擇,同時提供快速的中繼資料擷取,確保批次處理的低延遲。

前置條件與設定

您需要的項目

  • JDK 8 或以上(建議使用 JDK 11+ 以獲得更佳的垃圾回收)
  • MavenGradle 進行相依管理
  • IntelliJ IDEAEclipseVS Code 等 IDE
  • 用於正式環境的 GroupDocs.Comparison 授權(試用版為選擇性)

將 GroupDocs.Comparison 加入您的專案

將最新的 Maven 相依加入 pom.xml

<repositories>
   <repository>
      <id>repository.groupdocs.com</id>
      <name>GroupDocs Repository</name>
      <url>https://releases.groupdocs.com/comparison/java/</url>
   </repository>
</repositories>
<dependencies>
   <dependency>
      <groupId>com.groupdocs</groupId>
      <artifactId>groupdocs-comparison</artifactId>
      <version>25.2</version>
   </dependency>
</dependencies>

專業提示: 請始終參考 GroupDocs releases page 上的最新版本,以獲得安全性修補與新格式支援。

取得授權(千萬別跳過!)

  1. 免費試用 – 從 GroupDocs Downloads 頁面下載。
  2. 臨時授權 – 前往 Temporary License Page 申請開發用授權。
  3. 完整授權 – 於 Purchase Page 購買無限制的正式授權。

基本設定與初始化

Comparer 類別是 GroupDocs.Comparison 所有文件操作的入口點。它實作 AutoCloseable,因此使用 try‑with‑resources 區塊即可確保正確清理。

import com.groupdocs.comparison.Comparer;

public class DocumentMetadataExtractor {
    public static void main(String[] args) {
        String sourceFilePath = "YOUR_DOCUMENT_DIRECTORY/sample.docx";
        
        try (Comparer comparer = new Comparer(sourceFilePath)) {
            System.out.println("GroupDocs.Comparison is ready to use!");
            // We'll add metadata extraction code here
        } catch (Exception e) {
            System.err.println("Error initializing GroupDocs: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

如何使用 GroupDocs 擷取檔案類型?

getDocumentInfo() 會回傳包含已載入文件中繼資料的 IDocumentInfo 實例。使用 Comparer 載入文件後呼叫 getDocumentInfo(),即可立即取得檔案格式、頁數、大小等屬性。這行單一呼叫即完成 get file type java 的所有需求。該方法同時支援本機檔案與串流,適用於各種儲存情境。

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.result.IDocumentInfo;

public class FilePathMetadataExtraction {
    public static void extractMetadataFromPath(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            
            System.out.printf("
File Analysis Results:
File type: %s
Number of pages: %d
Document size: %d bytes (%.2f KB)%n", 
                info.getFileType().getFileFormat(), 
                info.getPageCount(), 
                info.getSize(),
                info.getSize() / 1024.0);
        } catch (Exception e) {
            System.err.println("Failed to extract metadata: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        String documentPath = "YOUR_DOCUMENT_DIRECTORY/sample.pdf";
        extractMetadataFromPath(documentPath);
    }
}

何時使用此方法

  • 檔案儲存在同一伺服器的本機磁碟上。
  • 需要快速、低開銷的中繼資料讀取。
  • 批次工作在檔案系統上執行,路徑存取成本低。

如何使用 GroupDocs 取得 PDF 頁數?

getPageCount() 會回傳文件的總頁數。IDocumentInfo.getPageCount() 方法可直接取得 PDF、Word 以及其他分頁格式的頁數,且不必開啟完整文件,保持記憶體使用量低。開發者因此能在進行密集處理或轉換前,快速評估文件大小。

import com.groupdocs.comparison.Comparer;
import com.groupdocs.comparison.result.IDocumentInfo;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.IOException;

public class InputStreamMetadataExtraction {
    public static void extractMetadataFromStream(String filePath) {
        try (InputStream sourceStream = new FileInputStream(filePath);
             Comparer comparer = new Comparer(sourceStream)) {
            
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            
            System.out.println("Document Metadata Analysis:");
            System.out.println("==========================");
            System.out.printf("File Format: %s%n", info.getFileType().getFileFormat());
            System.out.printf("Total Pages: %d%n", info.getPageCount());
            System.out.printf("File Size: %d bytes%n", info.getSize());
            System.out.printf("Size (Human Readable): %s%n", formatFileSize(info.getSize()));
            
        } catch (IOException e) {
            System.err.println("IO Error: " + e.getMessage());
        } catch (Exception e) {
            System.err.println("Metadata extraction failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    // Helper method to make file sizes more readable
    private static String formatFileSize(long size) {
        if (size < 1024) return size + " bytes";
        if (size < 1024 * 1024) return String.format("%.2f KB", size / 1024.0);
        if (size < 1024 * 1024 * 1024) return String.format("%.2f MB", size / (1024.0 * 1024.0));
        return String.format("%.2f GB", size / (1024.0 * 1024.0 * 1024.0));
    }
    
    public static void main(String[] args) {
        String documentPath = "YOUR_DOCUMENT_DIRECTORY/report.xlsx";
        extractMetadataFromStream(documentPath);
    }
}

為什麼頁數很重要

  • 法務團隊驗證合約是否符合規定長度。
  • 出版管線執行頁數上限政策。
  • 分析儀表板顯示文件大小趨勢。

如何從 InputStream 讀取文件中繼資料?

當文件存放於資料庫、雲端儲存桶,或透過 HTTP 接收時,您可以直接將 InputStream 傳給 Comparer。此方式避免產生暫存檔,降低 I/O 延遲。串流內容同時減少磁碟使用,提升高容量攝取管線的吞吐量。

public class DocumentCatalogSystem {
    public void catalogDocument(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            
            // Store in database or index for search
            DocumentRecord record = new DocumentRecord();
            record.setFileType(info.getFileType().getFileFormat());
            record.setPageCount(info.getPageCount());
            record.setFileSize(info.getSize());
            record.setFilePath(filePath);
            
            // Save to your database here
            saveDocumentRecord(record);
            
        } catch (Exception e) {
            logError("Failed to catalog document: " + filePath, e);
        }
    }
}

InputStream 處理的好處

  • 資料庫儲存 – 直接讀取 BLOB,無需寫入磁碟。
  • 網路來源 – 從 S3、Azure Blob 或 REST 端點串流檔案。
  • 安全性 – 透過記憶體保留資料,降低檔案系統暴露風險。
  • 可擴充性 – 結合 Java NIO channel 實作非阻塞處理。

真實案例與使用情境

1. 內容管理系統整合

自動為上傳的檔案標記格式、頁數與大小,讓 CMS 能正確分類與顯示。

public class LegalDocumentValidator {
    public boolean validateSubmission(String documentPath) {
        try (Comparer comparer = new Comparer(documentPath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            
            // Check if document meets legal requirements
            boolean isValidFormat = isAcceptedFormat(info.getFileType().getFileFormat());
            boolean hasValidPageCount = info.getPageCount() > 0 && info.getPageCount() <= 50;
            boolean isValidSize = info.getSize() <= 10 * 1024 * 1024; // 10MB max
            
            return isValidFormat && hasValidPageCount && isValidSize;
            
        } catch (Exception e) {
            return false; // Invalid if we can't process it
        }
    }
    
    private boolean isAcceptedFormat(String format) {
        return Arrays.asList("PDF", "DOCX", "DOC").contains(format.toUpperCase());
    }
}

2. 法律系統的文件驗證

驗證每份提交的合約皆為 PDF,且頁數至少達到規定最小值,才允許進入審核流程。

public class BatchDocumentProcessor {
    public void processDocumentDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        File[] files = directory.listFiles((dir, name) -> 
            name.toLowerCase().endsWith(".pdf") || 
            name.toLowerCase().endsWith(".docx") ||
            name.toLowerCase().endsWith(".xlsx"));
        
        if (files == null) {
            System.out.println("No documents found in directory");
            return;
        }
        
        System.out.println("Processing " + files.length + " documents...");
        
        for (File file : files) {
            processDocument(file.getAbsolutePath());
        }
    }
    
    private void processDocument(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            
            System.out.printf("%s: %s, %d pages, %s%n", 
                new File(filePath).getName(),
                info.getFileType().getFileFormat(),
                info.getPageCount(),
                formatFileSize(info.getSize()));
                
        } catch (Exception e) {
            System.err.println("Error processing " + filePath + ": " + e.getMessage());
        }
    }
}

3. 批次文件處理

安排夜間工作掃描共享資料夾、擷取中繼資料,並將結果寫入關聯式資料庫供報表使用。

java.io.FileNotFoundException: YOUR_DOCUMENT_DIRECTORY/document.pdf (No such file or directory)

常見問題與疑難排解

問題 1:FileNotFoundException

Direct answer: 確認傳遞給 Comparer 的路徑正確,使用絕對路徑,且確保 Java 行程具備讀取權限。
Solution: 檢查作業系統的檔案權限,並建議使用 Paths.get(...).toAbsolutePath() 以避免相對路徑混淆。

public static boolean processDocumentSafely(String filePath) {
    File file = new File(filePath);
    
    if (!file.exists()) {
        System.err.println("File not found: " + filePath);
        return false;
    }
    
    if (!file.canRead()) {
        System.err.println("Cannot read file: " + filePath);
        return false;
    }
    
    try (Comparer comparer = new Comparer(filePath)) {
        // Your metadata extraction code here
        return true;
    } catch (Exception e) {
        System.err.println("Processing failed: " + e.getMessage());
        return false;
    }
}

問題 2:不支援的檔案格式

Direct answer: 在處理前呼叫 Comparer.isSupported(fileExtension) 以確認格式是否在支援清單內。
Solution: isSupported() 會檢查給定的副檔名是否屬於 GroupDocs 可處理的格式。若不支援,可先在上游轉檔或通知使用者。

public static boolean isSupportedFormat(String filePath) {
    String extension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
    Set<String> supportedFormats = Set.of(
        "pdf", "doc", "docx", "xls", "xlsx", "ppt", "pptx", 
        "txt", "rtf", "odt", "ods", "odp"
    );
    return supportedFormats.contains(extension);
}

問題 3:大型檔案的記憶體問題

Direct answer: 使用串流 API(Comparer 搭配 InputStream)並啟用 Comparer.setLoadOptions(LoadOptions.memoryOptimized()),即使是 500 頁的 PDF 也能將記憶體佔用控制在 100 MB 以下。
Solution: LoadOptions.memoryOptimized() 會在讀取大型檔案時使用最小記憶體。必要時可將檔案分塊處理,或調整 JVM 堆大小(如 -Xmx2g)。

public static void processLargeDocument(String filePath) {
    // Set JVM options: -Xmx2g -XX:+UseG1GC
    
    System.gc(); // Suggest garbage collection before processing
    
    try (Comparer comparer = new Comparer(filePath)) {
        IDocumentInfo info = comparer.getSource().getDocumentInfo();
        
        if (info.getSize() > 100 * 1024 * 1024) { // 100 MB
            System.out.println("Warning: Processing large file (" + 
                formatFileSize(info.getSize()) + ")");
        }
        
        // Process document
        
    } catch (OutOfMemoryError e) {
        System.err.println("File too large to process: " + filePath);
        // Consider splitting or using a streaming approach
    }
}

問題 4:授權相關錯誤

Direct answer: 在應用程式啟動時一次載入授權檔案,例如 License license = new License(); license.setLicense("license_path");,可避免重複授權檢查造成的效能損耗。
Solution: License 會載入並套用 GroupDocs 授權至 API。請將授權檔存放於安全位置,並透過環境變數引用。

public class LicenseManager {
    private static boolean licenseSet = false;
    
    public static void setLicense() {
        if (!licenseSet) {
            try {
                License license = new License();
                license.setLicense("path/to/your/license.lic");
                licenseSet = true;
                System.out.println("License applied successfully");
            } catch (Exception e) {
                System.err.println("License error: " + e.getMessage());
                System.out.println("Running in evaluation mode");
            }
        }
    }
}

效能最佳化技巧

1. 資源管理

盡可能重複使用單一 Comparer 實例處理多個檔案,並始終以 try‑with‑resources 關閉。

public class OptimizedDocumentProcessor {
    private static final int MAX_CONCURRENT_PROCESSES = Runtime.getRuntime().availableProcessors();
    private ExecutorService executorService = Executors.newFixedThreadPool(MAX_CONCURRENT_PROCESSES);
    
    public void processDocumentsConcurrently(List<String> filePaths) {
        List<Future<DocumentMetadata>> futures = new ArrayList<>();
        
        for (String filePath : filePaths) {
            Future<DocumentMetadata> future = executorService.submit(() -> {
                return extractMetadata(filePath);
            });
            futures.add(future);
        }
        
        // Collect results
        for (Future<DocumentMetadata> future : futures) {
            try {
                DocumentMetadata metadata = future.get(30, TimeUnit.SECONDS);
                processMetadata(metadata);
            } catch (TimeoutException e) {
                System.err.println("Document processing timed out");
            }
        }
    }
}

2. 快取策略

對重複處理的檔案快取 IDocumentInfo 結果。簡單的 ConcurrentHashMap<String, DocumentInfo> 可在高吞吐量情境下減少多達 70 % 的重複 I/O。

public class CachedMetadataExtractor {
    private static final Map<String, DocumentMetadata> metadataCache = new ConcurrentHashMap<>();
    
    public DocumentMetadata getDocumentMetadata(String filePath) {
        File file = new File(filePath);
        String cacheKey = filePath + "_" + file.lastModified();
        
        return metadataCache.computeIfAbsent(cacheKey, key -> {
            return extractMetadataInternal(filePath);
        });
    }
    
    private DocumentMetadata extractMetadataInternal(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            return new DocumentMetadata(
                info.getFileType().getFileFormat(),
                info.getPageCount(),
                info.getSize()
            );
        } catch (Exception e) {
            throw new RuntimeException("Failed to extract metadata", e);
        }
    }
}

3. 記憶體效能處理

啟用 LoadOptions.memoryOptimized(),且在僅需中繼資料時避免載入完整文件。此做法可將大型 PDF 的 RAM 使用量降低約 80 %。

public class MemoryEfficientProcessor {
    public void processLargeDirectory(String directoryPath) {
        try (Stream<Path> paths = Files.walk(Paths.get(directoryPath))) {
            paths.filter(Files::isRegularFile)
                 .filter(path -> isSupportedFormat(path.toString()))
                 .forEach(path -> {
                     processDocument(path.toString());
                     System.gc(); // Suggest cleanup after each document
                 });
        } catch (IOException e) {
            System.err.println("Error accessing directory: " + e.getMessage());
        }
    }
}

進階使用情境

建立文件分析儀表板

從數千個檔案收集中繼資料,存入 Elasticsearch,並視覺化顯示如每種格式的平均頁數、各類型的總儲存量以及最常見的副檔名等趨勢。

public class DocumentAnalytics {
    public Map<String, Integer> getFormatDistribution(List<String> filePaths) {
        Map<String, Integer> formatCounts = new HashMap<>();
        
        for (String filePath : filePaths) {
            try (Comparer comparer = new Comparer(filePath)) {
                IDocumentInfo info = comparer.getSource().getDocumentInfo();
                String format = info.getFileType().getFileFormat();
                formatCounts.merge(format, 1, Integer::sum);
            } catch (Exception e) {
                formatCounts.merge("ERROR", 1, Integer::sum);
            }
        }
        
        return formatCounts;
    }
    
    public long getTotalDocumentSize(List<String> filePaths) {
        return filePaths.stream()
                .mapToLong(this::getDocumentSize)
                .sum();
    }
    
    private long getDocumentSize(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            return comparer.getSource().getDocumentInfo().getSize();
        } catch (Exception e) {
            return 0;
        }
    }
}

最佳實踐與專業提示

1. 永遠使用 Try‑With‑Resources

確保原生資源即時釋放,防止檔案鎖定與記憶體洩漏。

// Good - automatic resource management
try (Comparer comparer = new Comparer(filePath)) {
    // Your code here
} catch (Exception e) {
    // Handle errors
}

// Avoid - manual resource management (error‑prone)
Comparer comparer = new Comparer(filePath);
// If exception occurs here, resources might not be cleaned up
comparer.close();

2. 實作適當的錯誤處理

將中繼資料擷取包在 try‑catch 區塊中,記錄檔名與具體例外,然後繼續處理下一個檔案。

public class RobustDocumentProcessor {
    public Optional<DocumentMetadata> extractMetadata(String filePath) {
        try (Comparer comparer = new Comparer(filePath)) {
            IDocumentInfo info = comparer.getSource().getDocumentInfo();
            return Optional.of(new DocumentMetadata(info));
        } catch (Exception e) {
            logError("Failed to process: " + filePath, e);
            return Optional.empty();
        }
    }
}

3. 驗證輸入參數

在呼叫 API 前檢查 null 串流、零長度檔案與不支援的副檔名。

public void processDocument(String filePath) {
    Objects.requireNonNull(filePath, "File path cannot be null");
    
    if (filePath.trim().isEmpty()) {
        throw new IllegalArgumentException("File path cannot be empty");
    }
    
    if (!new File(filePath).exists()) {
        throw new IllegalArgumentException("File does not exist: " + filePath);
    }
    
    // Process the document
}

4. 密碼保護的文件

在呼叫 getDocumentInfo() 前,透過 LoadOptions.setPassword("yourPassword") 提供密碼,以解鎖加密的 PDF。

LoadOptions loadOptions = new LoadOptions();
loadOptions.setPassword("your-password");
try (Comparer comparer = new Comparer(filePath, loadOptions)) {
    // Extract metadata from password‑protected document
}

5. 雲端儲存(例如 AWS S3)

使用 AWS SDK 取得 S3ObjectInputStream,直接傳入 Comparer。此方式可免除暫存本機副本的需求。

// Example with AWS S3
S3Object object = s3Client.getObject("bucket-name", "document-key");
try (InputStream stream = object.getObjectContent();
     Comparer comparer = new Comparer(stream)) {
    // Extract metadata
}

常見問答

Q: 我可以在商業應用中使用嗎?
A: 可以,只要套用有效的 GroupDocs.Comparison 授權,該函式庫即完全支援商業部署。

Q: API 能處理受密碼保護的 PDF 嗎?
A: 完全支援。於呼叫 getDocumentInfo() 前,使用 LoadOptions.setPassword() 提供密碼即可。

Q: 官方支援哪些 Java 版本?
A: GroupDocs.Comparison 支援 JDK 8、11、17 以及後續的 LTS 版本。

Q: 函式庫如何處理極大型檔案(例如 >1 GB)?
A: 透過串流 API 與記憶體最佳化載入選項,可在不將整個檔案載入 RAM 的情況下處理多 GB 檔案。

Q: 有辦法平行批次處理檔案嗎?
A: 有的——結合 Java 的 ExecutorService 與執行緒安全的 Comparer 實例(或建立 Comparer 池),即可在多核心伺服器上實現線性擴充。

結論與後續步驟

您現在已掌握完整、可投入生產環境的 get file type java 與文件中繼資料擷取方式,使用 GroupDocs.Comparison 可:

  1. 以單一 API 呼叫取得格式、頁數、大小與自訂屬性。
  2. 依儲存架構選擇路徑或串流方式擷取。
  3. 應用快取、串流與記憶體最佳化技巧,將每日處理千文件的規模擴展至數十萬。

接下來,您可以探索 GroupDocs.Metadata 以取得更深入的作者與修訂資訊,或將中繼資料擷取整合至提供可搜尋文件目錄的 REST 服務。


Last Updated: 2026-05-21
Tested With: GroupDocs.Comparison 25.2
Author: GroupDocs

Resources for Continued Learning:

相關教學