private byte[] SjwGetBuffer(String url) throws IOException {
URL sjwurl = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
sjwurl = new URL(url);
//建立链接
httpUrl = (HttpURLConnection) sjwurl.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
long s = 0;
while(bis.available() > 0 ) {
int bytes_read = bis.read( buf );
if( bytes_read > 0 ) {
bos.write(buf, 0, bytes_read);
s += bytes_read;
}else {
break;
}
}
System.out.println(s);
bis.close();
httpUrl.disconnect();
return bos.toByteArray();
}
我觉得有可能是程序哪个地方有BUG导致这样的?你们知道什么原因吗?谢谢
URL sjwurl = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
sjwurl = new URL(url);
//建立链接
httpUrl = (HttpURLConnection) sjwurl.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
long s = 0;
while(bis.available() > 0 ) {
int bytes_read = bis.read( buf );
if( bytes_read > 0 ) {
bos.write(buf, 0, bytes_read);
s += bytes_read;
}else {
break;
}
}
System.out.println(s);
bis.close();
httpUrl.disconnect();
return bos.toByteArray();
}
我觉得有可能是程序哪个地方有BUG导致这样的?你们知道什么原因吗?谢谢
private byte[] SjwGetBuffer(String url) throws IOException {
URL sjwurl = null;
HttpURLConnection httpUrl = null;
BufferedInputStream bis = null;
byte[] buf = new byte[1024];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
sjwurl = new URL(url);
//建立链接
httpUrl = (HttpURLConnection) sjwurl.openConnection();
//连接指定的资源
httpUrl.connect();
//获取网络输入流
bis = new BufferedInputStream(httpUrl.getInputStream());
long s = 0;
int bytes_read=0;
while((bytes_read = bis.read( buf ))>0) {
bos.write(buf, 0, bytes_read);
s += bytes_read;
}
System.out.println(s);
bis.close();
httpUrl.disconnect();
return bos.toByteArray();
}
available()只是流当前不受阻塞可读的估计数,不意味着是整个流长度...
要老老实实的read到最后一个字节