POIFSFileSystem java.io.IOException: Read error

使用POI解析word文档遇到java.io.IOException: Read error问题

jar包 poi-3.8.jar


private HWPFDocument getDocument() {
    if(this.document == null) {
        try {
            this.document = new HWPFDocument(this.inputStream);
        } catch (Exception var2) {
            logger.error("open failed!", var2);
            throw new WordParserException("1");
        }
    }

    return this.document;
}
HWPFDocument.class
public HWPFDocument(InputStream istream) throws IOException {
    this(verifyAndBuildPOIFS(istream));
}

HWPFDocumentCore.class

public static POIFSFileSystem verifyAndBuildPOIFS(InputStream istream) throws IOException {
    PushbackInputStream pis = new PushbackInputStream(istream, 6);
    byte[] first6 = new byte[6];
    pis.read(first6);
    if(first6[0] == 123 && first6[1] == 92 && first6[2] == 114 && first6[3] == 116 && first6[4] == 102) {
        throw new IllegalArgumentException("The document is really a RTF file");
    } else {
        pis.unread(first6);
        return new POIFSFileSystem(pis);
    }
}
报错信息:

java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(FileInputStream.java:199)
at java.io.FilterInputStream.read(FilterInputStream.java:116)
at java.io.PushbackInputStream.read(PushbackInputStream.java:169)
at java.io.FilterInputStream.read(FilterInputStream.java:90)
at org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:95)
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174)




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
出现这个错误是因为您正在尝试使用POIPOIFSFileSystem类来处理Office 2007+ XML格式的文件,而这个类主要用于处理旧版本的二进制格式(.xls)。对于Office 2007+ XML格式(.xlsx),您应该使用XSSFWorkbook类。 以下是一个示例代码,演示如何使用POI来解密密码保护的Office 2007+ XML格式的Excel文件(.xlsx): ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import java.io.FileInputStream; import java.io.FileOutputStream; public class ExcelDecryptionExample { public static void main(String[] args) { String inputFile = "encrypted.xlsx"; String outputFile = "decrypted.xlsx"; String password = "password"; try { FileInputStream fis = new FileInputStream(inputFile); Workbook workbook = new XSSFWorkbook(fis); // 如果文件受密码保护,请取消下面两行的注释并设置正确的密码 // POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fis); // EncryptionInfo encInfo = new EncryptionInfo(poifsFileSystem); // Decryptor decryptor = Decryptor.getInstance(encInfo); // decryptor.verifyPassword(password); // 解密文件 workbook.removeSheetAt(0); // 删除加密时自动添加的隐藏工作表 // 在这里进行其他操作,例如读取或修改工作表数据 FileOutputStream fos = new FileOutputStream(outputFile); workbook.write(fos); fis.close(); fos.close(); System.out.println("Excel file decrypted successfully."); } catch (Exception e) { e.printStackTrace(); } } } ``` 请确保将`encrypted.xlsx`替换为您要解密的实际文件名,并将`password`替换为正确的密码。解密后的文件将保存在`decrypted.xlsx`中。 注意:对于没有加密的文件,您无需使用`Decryptor`类验证密码。 希望这可以解决您的问题!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值