我用Java做了一个简单的下载管理器,我的下载管理器能够下载却无法从网上下载大文件
我无法从网上下载大文件(超过 1 MB 的文件)。但是,我的程序能够从本地主机下载这些大文件。下载大文件还需要做什么吗? 这是代码片段:
try {
//connection to the remote object referred to by the URL.
url = new URL(urlPath);
// connection to the Server
conn = (HttpURLConnection) url.openConnection();
// get the input stream from conn
in = new BufferedInputStream(conn.getInputStream());
// save the contents to a file
raf = new RandomAccessFile("output","rw");
byte[] buf = new byte[ BUFFER_SIZE ];
int read;
while( ((read = in.read(buf,0,BUFFER_SIZE)) != -1) )
{
raf.write(buf,0,BUFFER_SIZE);
}
} catch ( IOException e ) {
}
finally {
}
提前致谢。
I am not able to download large files from the net ( more than 1 mb file ). However, my program is able to download those large files from the localhost. Is there anything else that i need to do to download large files?
Here is the code snippet:
try {
//connection to the remote object referred to by the URL.
url = new URL(urlPath);
// connection to the Server
conn = (HttpURLConnection) url.openConnection();
// get the input stream from conn
in = new BufferedInputStream(conn.getInputStream());
// save the contents to a file
raf = new RandomAccessFile("output","rw");
byte[] buf = new byte[ BUFFER_SIZE ];
int read;
while( ((read = in.read(buf,0,BUFFER_SIZE)) != -1) )
{
raf.write(buf,0,BUFFER_SIZE);
}
} catch ( IOException e ) {
}
finally {
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忽略了实际读取的字节数:
您的
write
调用总是写入整个缓冲区,即使您没有用read 填充它调用。你想要:
You're ignoring how many bytes you've actually read:
Your
write
call always writes the whole buffer, even if you didn't fill it with theread
call. You want: