You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PDF文件对比代码抛出ClassCastException错误求助

PDF对比代码出现ClassCastException错误求助

执行PDF对比代码时遇到如下错误:

Exception in thread "main" java.lang.ClassCastException: java.io.FileInputStream cannot be cast to org.apache.pdfbox.io.RandomAccessRead
    at pdfComparator.PDFComparator.main(PDFComparator.java:29)

以下是我的代码:

package xyz;

import java.io.IOException;
import java.io.File;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.io.RandomAccessBufferedFileInputStream;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class PDFComparator {

    public static void main(String[] args) throws IOException{
        if(args.length !=2){
            System.out.println("Usage: java PDFComparator file1 file2");
            System.exit(1);
        }
        String firstFilePath = args[0];
        String secondFilePath = args[1];
        
        File pdfFile1 = new File(firstFilePath);

        File pdfFile2 = new File(secondFilePath);
        
    RandomAccessBufferedFileInputStream raFile1 = new RandomAccessBufferedFileInputStream(pdfFile1);
    RandomAccessBufferedFileInputStream raFile2 = new RandomAccessBufferedFileInputStream(pdfFile2);    
    
        
    PDFParser parser1 = new PDFParser(raFile1);
    parser1.parse();
    COSDocument cosDoc1 = parser1.getDocument();
    PDDocument firstPDF = new PDDocument(cosDoc1);

    PDFParser parser2 = new PDFParser(raFile2);
    parser2.parse();
    COSDocument cosDoc2 = parser2.getDocument();
    PDDocument secondPDF = new PDDocument(cosDoc2);
    

    PDFTextStripper stripper = new PDFTextStripper();
    String text1 = stripper.getText(firstPDF).trim();
    String text2 = stripper.getText(secondPDF).trim();

    if (text1.equals(text2)){
        System.out.println("The two PDF files are identical");
        }else{
        System.out.println("The two PDF files have differences");
        }
    firstPDF.close();
    secondPDF.close();
    }

}

错误原因分析

这个错误源于PDFBox版本(大概率是2.x系列)的API变更:PDFParser的构造方法要求传入RandomAccessRead类型参数,但RandomAccessBufferedFileInputStream在该版本中仅继承自FileInputStream,并未实现RandomAccessRead接口,因此触发类型转换异常。

另外,PDFBox 2.x已经简化了PDF读取流程,无需手动通过PDFParser和COSDocument构建PDDocument,直接调用静态加载方法即可完成操作。

修复后的代码

package xyz;

import java.io.IOException;
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

public class PDFComparator {

    public static void main(String[] args) throws IOException{
        if(args.length !=2){
            System.out.println("Usage: java PDFComparator file1 file2");
            System.exit(1);
        }
        String firstFilePath = args[0];
        String secondFilePath = args[1];
        
        File pdfFile1 = new File(firstFilePath);
        File pdfFile2 = new File(secondFilePath);
        
        // 直接用PDDocument静态方法加载文件,省略Parser和COSDocument的手动处理
        try (PDDocument firstPDF = PDDocument.load(pdfFile1);
             PDDocument secondPDF = PDDocument.load(pdfFile2)) {
            
            PDFTextStripper stripper = new PDFTextStripper();
            String text1 = stripper.getText(firstPDF).trim();
            String text2 = stripper.getText(secondPDF).trim();

            if (text1.equals(text2)){
                System.out.println("The two PDF files are identical");
            } else {
                System.out.println("The two PDF files have differences");
            }
        } // try-with-resources语法自动关闭文档,避免资源泄漏
    }
}

额外说明

  • 使用try-with-resources语法可以自动管理PDDocument资源,无需手动调用close()方法。
  • 若坚持使用PDFParser,需传入实现RandomAccessRead接口的类(如RandomAccessFile包装后的实例),但显然PDDocument.load()的方式更简洁可靠。

内容的提问来源于stack exchange,提问作者Giovanni De Maio Langella

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 04:35:02