inputStream.available() == 0 但实际文件不为空

it2023-05-09  72

现象:inputStream.available() == 0 但实际文件不为空 原因:从网络中读取InputStream后,可能因网络质量一次读取后InputStream长度为0 建议:多读几次

封装一个获取文件大小的方法:

/** * 现象:inputStream.available() == 0 但实际文件不为空 * 原因:从网络中读取InputStream后,可能因网络质量一次读取后InputStream长度为0,这里最多读5次,没读到就视为0 */ private static long getInputStreamSize(InputStream inputStream){ long fileSize = 0; int cunt = 1; try { while (fileSize == 0 && cunt <= 5) { fileSize = inputStream.available(); cunt++; } } catch (IOException ignored) { } return fileSize; }
最新回复(0)