Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading files with Java

Tags:

I've written a bit of code for downloading an episode of a webcast I do. It gets the URL of the episode and gets the place to save it. However, it only downloads up to 16MB and then automatically cancels. I'm not entirely sure what value to change to increase this. Is it possible, and could someone please point me in the right direction? Thankyou!

The downloading code:

    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); 
like image 934
Ziddia Avatar asked Dec 06 '11 18:12

Ziddia


People also ask

How do I download a text file in Java?

We will use the copy(inputStream, fileOS) method to download a file into the local system. InputStream inputStream = new URL("http://example.com/my-file-path.txt").openStream(); FileOutputStream fileOS = new FileOutputStream("/Users/username/Documents/file_name. txt"); int i = IOUtils. copy(inpuStream, fileOS);

How do I download a file with FileOutputStream?

FileOutputStream fileOutputStream = new FileOutputStream(FILE_NAME); FileChannel fileChannel = fileOutputStream. getChannel(); We'll use the transferFrom() method from the ReadableByteChannel class to download the bytes from the given URL to our FileChannel: fileOutputStream.

How can I download Excel file in Java?

file = new File(home + "/Downloads/" + "excel" + filename + ". xls"); Runtime. getRuntime(). exec("cmd.exe /C start " + file);


1 Answers

A quick look at the documentation of transferFrom:

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

WELL.

The value 1<<24 for the count (from the original question) equals 16M

I guess that's the answer to your question :-)

like image 78
emesx Avatar answered Oct 23 '22 22:10

emesx