java - inputstream流读到最后为什么没有返回 -1 ?
ringa_lee
ringa_lee 2017-04-18 10:52:57
0
1
932
InputStream fileSource = req.getInputStream();
String tempFile = "/home/joy/桌面/tempFile";
FileOutputStream tempFileOutputStream = new FileOutputStream(tempFile);
byte[] bytes = new byte[10 * 1024];
int i = 0;
while ((i = fileSource.read(bytes, 0, bytes.length)) != -1) {
    System.out.println("已读字节:" + i + ", 剩余字节: " + fileSource.available());
    System.out.println(new String(bytes));
    tempFileOutputStream.write(bytes);
    tempFileOutputStream.flush();
    if (fileSource.available() < bytes.length) {
        bytes = new byte[fileSource.available()];
    }
}
tempFileOutputStream.close();
fileSource.close();

为什么该流读完的时候并没有返回-1,而是返回0,导致无限循环.(上传文档类型文件不会,字节文件就会发生这种情况)

ringa_lee
ringa_lee

ringa_lee

모든 응답(1)
黄舟

방금 파일 읽기를 마친 것은 버퍼의 최종 처리 때문일 것입니다

으아악

일반적으로 버퍼 크기를 재설정할 필요는 없습니다

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!