使用Java下载文件

我写了一些代码来下载我做的网络直播。 它获取剧集的URL并获取保存它的位置。 但是,它最多只下载16MB然后自动取消。 我不完全确定要改变什么值以增加它。 是否有可能,有人可以指出我正确的方向吗? 谢谢!

下载代码:

URL url = new URL(episode.getUrl()); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(episode.getLocalSave()); fos.getChannel().transferFrom(rbc, 0, 1 << 24); 

快速浏览transferFrom的文档:

 public abstract long transferFrom(ReadableByteChannel channel, long position, long count) 

好。

计数的值1 << 24(来自原始问题)等于16M

我猜这是你问题的答案:-)

这是另一个解决方案:

 import java.io.*; import java.net.*; public class DownloadFile { public static void main(String args[]) throws IOException { java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(episode.getUrl()).openStream()); java.io.FileOutputStream fos = new java.io.FileOutputStream(episode.getLocalSave()); java.io.BufferedOutputStream bout = new BufferedOutputStream(fos); byte data[] = new byte[1024]; int read; while((read = in.read(data,0,1024))>=0) { bout.write(data, 0, read); } bout.close(); in.close(); } }