Does anybody knows how to download file from internet, using URL (http://.../File.mp4)? I am trying using NIO, but always the stream end at Integer.MAX_VALUE. My file is 2.5GB.
My code:
String url = "http://.../Somefile.mp4";
String filename = "Path/to/file/Something.mp4";
boolean koncano = false;
URLConnection conn = new URL(url).openConnection();
conn.setRequestProperty("Range", "bytes=" + new File(filename).length() + "-");
ReadableByteChannel rbc = Channels.newChannel(conn.getInputStream());
long remain = conn.getContentLength();
FileOutputStream fos = new FileOutputStream(filename, true);
while (!koncano) {
long downloaded = new File(filename).length();
long buffer = (remain > 65536) ? 1 << 16 : remain;
while (remain > 0) {
long write = fos.getChannel().transferFrom(rbc, downloaded, buffer);
downloaded += write;
remain -= write;
if (write == 0) {
break;
}
}
if (remain <= 0) {
System.out.println("File is complete");
rbc.close();
fos.close();
koncano = true;
}
}