一、问题
1.1、环境
电脑环境:Windows 10;
开发工具:IntelliJ IDEA;
数据库环境:Redis 3.2.100
JDK环境: Jdk1.8;
1.2、问题
Check the return value of the “read” call to see how many bytes were read.
二、解答
1、源代码
try {
String fileName = ExcelUtils.encodeFileName(templateName, request);
URL fileUrl = new URL(tempFileUrl);
HttpURLConnection httpUrl = (HttpURLConnection) fileUrl.openConnection();
httpUrl.connect();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(httpUrl.getInputStream());
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
}catch (Exception e){
log.error(e.getMessage(), e);
}
这段代码的问题在哪里?
我们先来看看 read(byte[] by)这个方法的定义:
/**
* Reads some number of bytes from the input stream and stores them into
* the buffer array <code>b</code>. The number of bytes actually read is
* returned as an integer. This method blocks until input data is
* available, end of file is detected, or an exception is thrown.
*
* <p> If the length of <code>b</code> is zero, then no bytes are read and
* <code>0</code> is returned; otherwise, there is an attempt to read at
* least one byte. If no byte is available because the stream is at the
* end of the file, the value <code>-1</code> is returned; otherwise, at
* least one byte is read and stored into <code>b</code>.
*
* <p> The first byte read is stored into element <code>b[0]</code>, the
* next one into <code>b[1]</code>, and so on. The number of bytes read is,
* at most, equal to the length of <code>b</code>. Let <i>k</i> be the
* number of bytes actually read; these bytes will be stored in elements
* <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
* leaving elements <code>b[</code><i>k</i><code>]</code> through
* <code>b[b.length-1]</code> unaffected.
*
* <p> The <code>read(b)</code> method for class <code>InputStream</code>
* has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
*
* @param b the buffer into which the data is read.
* @return the total number of bytes read into the buffer, or
* <code>-1</code> if there is no more data because the end of
* the stream has been reached.
* @exception IOException If the first byte cannot be read for any reason
* other than the end of the file, if the input stream has been closed, or
* if some other I/O error occurs.
* @exception NullPointerException if <code>b</code> is <code>null</code>.
* @see java.io.InputStream#read(byte[], int, int)
*/
public int read(byte b[]) throws IOException {
return read(b, 0, b.length);
}
这里大致翻译一下:
1、从输入流中读取一定数量的字节,并且保存在缓存。
2、读到的返回值,是整数。
3、直到读取可读的文件到达末尾,或者异常才会终止。
4、如果数组长度是 0,则读取不到字节,返回值是 0;否则,至少读取 1 个字节。
5、流被读取到末尾时,则没有任何有效字节,返回 -1;否则,则会被正常读取,并存储到 b 数组中。
6、真正读取到的字节的长度,跟数组的长度是一致的。
所以,我们这里要判断,文件读取完毕了,再关掉输入流。修改后的代码如下:
try {
URL fileUrl = new URL("tempFileName");
HttpURLConnection httpUrl = (HttpURLConnection) fileUrl.openConnection();
httpUrl.connect();
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(httpUrl.getInputStream());
byte[] buffer = new byte[fis.available()];
while (true) {
int read = fis.read(buffer);
//判断是不是读到了数据流的末尾 ,防止出现死循环。
if (read == -1) {
break;
}
}
fis.close();
}catch (Exception e){
log.error(e.getMessage(), e);
}
完毕~
三、总结
承接项目开发(电商,金融,直播等互联网开发项目),承接外包等互联网业务~
欢迎关注我的
CSDN博客: https://blog.csdn.net/River_Continent
微信公众号:幕桥社区
知乎:张牧野, https://www.zhihu.com/people/zhang-mu-ye-37-76/activities
简书: https://www.jianshu.com/u/02c0096cbfd3