1

ネットワークドライブ上の画像を読み取るコードがあります。私は何千もの画像を読みましたが、たまに次の例外が発生することがあります。

java.io.IOException: An unexpected network error occurred
    at java.base/sun.nio.ch.FileDispatcherImpl.read0(Native Method)
    at java.base/sun.nio.ch.FileDispatcherImpl.read(FileDispatcherImpl.java:54)
    at java.base/sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:276)
    at java.base/sun.nio.ch.IOUtil.read(IOUtil.java:245)
    at java.base/sun.nio.ch.FileChannelImpl.read(FileChannelImpl.java:223)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:65)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:109)
    at java.base/sun.nio.ch.ChannelInputStream.read(ChannelInputStream.java:103)
    at java.base/java.io.InputStream.read(InputStream.java:205)

以下は私がそれを得るコードです

`

public static int getEPSSectionOffset(File file) throws Exception {
    int result = 0;
    try (InputStream inputStream =  
      Files.newInputStream(Paths.get(file.getAbsolutePath()),StandardOpenOption.READ);) {
      byte[] fourBytes = new byte[4];
      int totalBytesRead = inputStream.read(fourBytes);
      if (log.isDebugEnabled())
        log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());

      if (fourBytes[0] == (byte) 0xC5 && fourBytes[1] == (byte) 0xD0 && fourBytes[2] == (byte) 0xD3 
      && fourBytes[3] == (byte) 0xC6) {
        totalBytesRead = inputStream.read(fourBytes);
        if (log.isDebugEnabled())
          log.debug("Total bytes read is " + totalBytesRead + " for file " + file.getPath());

        result = makeInt(fourBytes);
      }
      return (result);
    } catch (Exception e) {
      log.error("Get EPS Section Offset - " + e.getMessage(), e);
    }
    return 0;
  }`

この行で例外が発生します- int totalBytesRead = inputStream.read(fourBytes);

4

1 に答える 1