InputStream.available()方法返回0与实际文件大小不一致问题

在java文件操作中,需要从InputStream流中读取文件到byte数组中,使用了如下方法:

InputStream is = this.getClass().getResourceAsStream(filePath);
byte[] bytes = new byte[is.available()];
is.read(bytes);

结果发现在本地测试一切正常,而到了服务器上读取到的文件却一直有问题,经过打日志排查发现在服务器上通过is.available()方法获取到的文件大小是0,显然这是有问题的。点进方法注释即可发现,java已经告诉了我们这个方法不可靠。

/**
 * Returns an estimate of the number of bytes that can be read (or
 * skipped over) from this input stream without blocking by the next
 * invocation of a method for this input stream. The next invocation
 * might be the same thread or another thread.  A single read or skip of this
 * many bytes will not block, but may read or skip fewer bytes.
 *
 * <p> Note that while some implementations of {@code InputStream} will return
 * the total number of bytes in the stream, many will not.  It is
 * never correct to use the return value of this method to allocate
 * a buffer intended to hold all data in this stream.
 *
 * <p> A subclass' implementation of this method may choose to throw an
 * {@link java.io.IOException} if this input stream has been closed by
 * invoking the {@link #close()} method.
 *
 * <p> The {@code available} method for class {@code InputStream} always
 * returns {@code 0}.
 *
 * <p> This method should be overridden by subclasses.
 *
 * @return     an estimate of the number of bytes that can be read (or skipped
 *             over) from this input stream without blocking or {@code 0} when
 *             it reaches the end of the input stream.
 * @exception java.io.IOException if an I/O error occurs.
 */

主要关注Note that这一句:一些InputStream的实现类会返回stream流中总的bytes字节长度,但不是绝对的,不能使用该方法的返回值去分配一个缓冲的byte数组。

那么如果需要从InputStream流中读取文件到byte数组中,实际要怎么写呢?正常大部分项目都是使用的Spring,而Spring已经帮我们开发好了相应的工具类,我们直接调用即可。

InputStream is = this.getClass().getResourceAsStream(filePath);
byte[] bytes = StreamUtils.copyToByteArray(is);

觉得内容还不错?打赏个钢镚鼓励鼓励!!👍