views:

2812

answers:

6

There is an online file (i.e. http://www.website.com/information.asp) I need to grab and save to a directory. I know there are several methods for grabbing and reading online files line-by-line (i.e. URL), but is there a way to just download and save the file using Java?

A: 

Grab the file and read it line-by-line, as you mention, and save the lines to a local file.

Pesto
+3  A: 

Downloading a file requires you to read it, either way you will have to go through the file in some way. Instead of line by line, you can just read it by bytes from the stream:

BufferedInputStream in = new BufferedInputStream(new Url("http://www.website.com/information.asp").openStream())
    byte data[] = new byte[1024];
    while(int count = in.read(data,0,1024) != null)
    {
        out.write(data, 0, count);
    }
yx
+1 - Nevermind my post :)
willcodejavaforfood
+1  A: 

I found this example which is probably overkill but you can extract the relavant parts yourself.

willcodejavaforfood
+20  A: 

give a try to Java NIO:

    URL google = new URL("http://www.google.it");
    ReadableByteChannel rbc = Channels.newChannel(google.openStream());
    FileOutputStream fos = new FileOutputStream("google.html");
    fos.getChannel().transferFrom(rbc, 0, 1 << 24);

using transferForm() is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.

Check more about it here.

dfa
+1 for being awesome
willcodejavaforfood
hey what is that shifty bit?
willcodejavaforfood
quoting you, +1 - Nevermind my post :)
yx
@willcodejavaforfood: it is just an alias for 16777216. An arbitrary long block size. A magic costant (beware it!)
dfa
@will, the shift is just another way of representing 2^24 (the amount he is reading from the channel)
yx
What if that file is bigger than 16777216 bytes?
serg
@dfa, yx - Ahh thanks
willcodejavaforfood
sorry to sound like a total noob, but should i be closing fos? regardless of that, thanks, it works perfectly!
echoblaze
absolutely yes :)
dfa
+5  A: 
public void saveUrl(String filename, String urlString) throws MalformedURLException, IOException
    {
     BufferedInputStream in = null;
     FileOutputStream fout = null;
     try
     {
      in = new BufferedInputStream(new URL(urlString).openStream());
      fout = new FileOutputStream(filename);

      byte data[] = new byte[1024];
      int count;
      while ((count = in.read(data, 0, 1024)) != -1)
      {
       fout.write(data, 0, count);
      }
     }
     finally
     {
      if (in != null)
       in.close();
      if (fout != null)
       fout.close();
     }
    }

You'll need to handle exceptions, probably external to this method.

Ben Noland
+1 for being complete
willcodejavaforfood
+2  A: 

Personally, I've found Apache's HttpClient to be more than capable of everything I've needed to do with regards to this. Here is a great tutorial on using HttpClient

belgariontheking
also commons-io is a great library
dfa