我有一个文件大小5MB &想通过HttpURLConnection下载(目标就像是“http://...../songs/Beatles_And I Love Her.mp3”)。HttpURLConnection读取输入流失败
我想如下
URL url = new URL("http://........../songs/Beatles_And I Love Her.mp3");
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
httpURLConnection.getResponseCode();
InputStream stream = httpURLConnection.getInputStream();
byte buf[] = new byte[128];
do
{
int numread = stream.read(buf);
if (numread <= 0)
{
stream.close();
break;
}
} while (true);
做它,但问题是,它只是只读取2KB数据的3KB和它返回后-1和退出。
你怎么会知道这只是读取多少数据? –
嗨,布赖恩,因为读了几个字节后“stream.read(buf);”返回“-1(减一)”,请帮助我。 –
欲了解更多信息,每次只从5Mb源读取“2774”字节。 –