在WorkbookFactory.create(流)Apache POI中关闭流

时间:2022-03-15 20:22:43

I created a 'ResetOnCloseInputStream' which extends BufferedInputStream, because I'm passing it to WorkbookFactory.create(InputStream) and it closes the stream after reading workbook, whereas I need to use the stream again. ResetOnInputStream looks like this-

我创建了一个'ResetOnCloseInputStream',它扩展了BufferedInputStream,因为我将它传递给WorkbookFactory.create(InputStream),它在读取工作簿后关闭了流,而我需要再次使用该流。 ResetOnInputStream看起来像这样 -

public class ResetOnCloseInputStream extends BufferedInputStream {

private final InputStream decorated;

public ResetOnCloseInputStream(InputStream anInputStream) {
    super(anInputStream);
    if (!anInputStream.markSupported()) {
        throw new IllegalArgumentException("marking not supported");
    }

    anInputStream.mark( 1 << 24); // magic constant: BEWARE
    decorated = anInputStream;
}

@Override
public void close() throws IOException {
    decorated.reset();
}
public void realClose() throws IOException {
    decorated.close();
}

@Override
public int read() throws IOException {
    return decorated.read();
}
}

but when it's passed to

但是当它被传递给

workbook = WorkbookFactory.create(stream);

I get this error-

我得到这个错误 -

Caused by: java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145)
at java.io.BufferedInputStream.read(BufferedInputStream.java:308)

1 个解决方案

#1


0  

I don't get the exception with your given class ResetOnCloseInputStream

我没有得到你的给定类ResetOnCloseInputStream的异常

    File inputFile = new File("C:/Test/Test.xls");
    FileInputStream fileInput;
    try {
        fileInput = new FileInputStream(inputFile);
        BufferedInputStream bufferInput = new ResetOnCloseInputStream(new BufferedInputStream(fileInput));
        Workbook workbook = WorkbookFactory.create(bufferInput);
        Sheet s1 = workbook.getSheetAt(0);
        System.out.println(s1.getSheetName()); //Sheet1
    } catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
        e.printStackTrace();
    }

I guess the problem may come the InputStream stream you created, it may be closed before passing to WorkbookFactory.create(bufferInput).

我想问题可能来自你创建的InputStream流,它可能在传递给WorkbookFactory.create(bufferInput)之前关闭。

#1


0  

I don't get the exception with your given class ResetOnCloseInputStream

我没有得到你的给定类ResetOnCloseInputStream的异常

    File inputFile = new File("C:/Test/Test.xls");
    FileInputStream fileInput;
    try {
        fileInput = new FileInputStream(inputFile);
        BufferedInputStream bufferInput = new ResetOnCloseInputStream(new BufferedInputStream(fileInput));
        Workbook workbook = WorkbookFactory.create(bufferInput);
        Sheet s1 = workbook.getSheetAt(0);
        System.out.println(s1.getSheetName()); //Sheet1
    } catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
        e.printStackTrace();
    }

I guess the problem may come the InputStream stream you created, it may be closed before passing to WorkbookFactory.create(bufferInput).

我想问题可能来自你创建的InputStream流,它可能在传递给WorkbookFactory.create(bufferInput)之前关闭。