当前位置: 技术问答>java相关
奇怪的死循环
来源: 互联网 发布时间:2015-09-12
本文导语: 最后一句一直不输出,while循环出不来。 int len = request.getContentLength(); System.out.println(len); byte[] buffer = new byte[1024]; InputStream in = request.getInputStream(); request.getInputStream();; ByteArrayOutputStream bout = new ByteArrayOutputStre...
最后一句一直不输出,while循环出不来。
int len = request.getContentLength();
System.out.println(len);
byte[] buffer = new byte[1024];
InputStream in = request.getInputStream();
request.getInputStream();;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int tempLen =0;
while((tempLen = in.read(buffer,0,buffer.length)) != -1) {
System.out.println(tempLen);//经验正,累计总数与输入文件大小一致
bout.write(buffer,0,tempLen);
}
System.out.println("out while");
int len = request.getContentLength();
System.out.println(len);
byte[] buffer = new byte[1024];
InputStream in = request.getInputStream();
request.getInputStream();;
ByteArrayOutputStream bout = new ByteArrayOutputStream();
int tempLen =0;
while((tempLen = in.read(buffer,0,buffer.length)) != -1) {
System.out.println(tempLen);//经验正,累计总数与输入文件大小一致
bout.write(buffer,0,tempLen);
}
System.out.println("out while");
|
The read() methd didn't return -1 except disk error, network error etc.
So first thing, you should get the file's size.
And the second, you should compare it with the data size which has been read from file.
Last, if one value is equal to another, then quit loop.
So first thing, you should get the file's size.
And the second, you should compare it with the data size which has been read from file.
Last, if one value is equal to another, then quit loop.
|
tempLen = in.read(buffer,0,buffer.length)) != -1
改為:
tempLen = in.read(buffer)) != -1
改為:
tempLen = in.read(buffer)) != -1