Downloading a binary file

B

bruce_phipps

I want to download a 4 meg binary file from a Linux server to my
standalone Java client program.
An example I have seen first creates a URL object, then creates an
Input Stream, then downloads data via a data buffer using read() and
write():

URL url = new URL("http://test.com/files/myfile");
InputStream in = url.openStream();
byte[] buf = new byte[4*1024]; //4k buffer
int bytesRead;
while ((bytesRead=in.read(buf)) != -1) {
out.write(buf,0,bytesRead);
}

Are there any simpler methods to do this?

Thanks
Bruce
 
B

bruce_phipps

I want to download a 4 meg binary file from a Linux server to my
standalone Java client program. [...]
Are there any simpler methods to do this?

What part of the example do you think is too complex?

Perhaps this is easier for you:

Runtime.getRuntime().exec("curl -Ohttp://test.com/files/myfile");

Or maybe just:

dwim("http://test.com/files/myfile");

/gordon

--

Thanks. curl is not available on the Solaris client I am using.
Bruce
 
A

Arne Vajhøj

I want to download a 4 meg binary file from a Linux server to my
standalone Java client program.
An example I have seen first creates a URL object, then creates an
Input Stream, then downloads data via a data buffer using read() and
write():

URL url = new URL("http://test.com/files/myfile");
InputStream in = url.openStream();
byte[] buf = new byte[4*1024]; //4k buffer
int bytesRead;
while ((bytesRead=in.read(buf)) != -1) {
out.write(buf,0,bytesRead);
}

Are there any simpler methods to do this?

No. That is the method.

(it is possible to make it more advanced and check HTTP header etc.)

Arne
 
A

Arne Vajhøj

Gordon said:
I want to download a 4 meg binary file from a Linux server to my
standalone Java client program. [...]
Are there any simpler methods to do this?

What part of the example do you think is too complex?

Perhaps this is easier for you:

Runtime.getRuntime().exec("curl -O http://test.com/files/myfile");

That is simple per poster request, but I do not consider
it a recommendable solution.

Arne
 
C

Chase Preuninger

It may be simpler without the buffer, and I think you can also use the
read bytes method, which would read the data into an array of bytes.
 
G

Gordon Beaton

That is simple per poster request, but I do not consider
it a recommendable solution.

That suggestion, just like the one that you didn't quote, was not a
serious one. I'm still interested to hear what the OP thinks was too
complex in the example he posted though.

/gordon

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top