2
public void download(String url, String destination) {
        BufferedOutputStream localBufferedOutputStream = null;
        URLConnection localURLConnection = null;
        InputStream localInputStream = null;
        try {
            URL localURL = new URL(url);

            localBufferedOutputStream = new BufferedOutputStream(new FileOutputStream(destination));
            localURLConnection = localURL.openConnection();
            localInputStream = localURLConnection.getInputStream();

            byte[] arrayOfByte = new byte[1024];
            int i;
            while ((i = localInputStream.read(arrayOfByte)) != -1) {
                localBufferedOutputStream.write(arrayOfByte, 0, i);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                if (localInputStream != null) {
                    localInputStream.close();
                }
                if (localBufferedOutputStream != null) {
                    localBufferedOutputStream.close();
                }
            } catch (IOException localIOException3) {
                System.out.println(localIOException3);
            }
        }
    }

I'm debugging my application and it seems a bit slow. I'm wondering if it's my internet. Is this the proper way to download a file in java? The file is 26mb.

jzd
  • 23,473
  • 9
  • 54
  • 76
Kyle
  • 3,004
  • 15
  • 52
  • 79

4 Answers4

3

You should always look to libraries such as Apache. They have done all the hard work for you: http://commons.apache.org/io/api-release/org/apache/commons/io/FileUtils.html

I use

static String   readFileToString(File file)
          Reads the contents of a file into a String using the default encoding for the VM.

quite a lot.

If you know you have a URL (and so stream) look at: http://commons.apache.org/io/api-1.4/org/apache/commons/io/IOUtils.html

peter.murray.rust
  • 37,407
  • 44
  • 153
  • 217
1

As an alternative and just for reference, you can investigate HTMLUnit. This framework will allow you to download files even on sites where there are browser redirects.

http://htmlunit.sourceforge.net/

James P.
  • 19,313
  • 27
  • 97
  • 155
  • 1
    It's been such a long time since I asked this. xD Thank you for the answer James. I remember going the apache way. I remember checking out htmlunit recently and found it executed javascript on pages which was awesome. Thanks – Kyle Jun 27 '12 at 08:10
  • 1
    Great to hear you made some advancement. Btw, did you go into anything like file integrity testing and handling download interrupts ? Also, this might be of interest: http://stackoverflow.com/questions/4687615/how-to-achieve-transfer-file-between-client-and-server-using-java-socket/4687706#4687706 – James P. Jun 27 '12 at 08:15
  • I have not. I remember trying to get stop/resume downloading and I was suggested to look at jdownloader source code but I never did. Thank you for that resource. – Kyle Jun 27 '12 at 08:36
1

You can leave out the BufferedOutputStream since you're already using a buffer yourself. But that's not going to make a big difference.

What may (or may not) make a big difference is using the nio channel classes instead of the streams.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
0

It is certainly not the best way. Code that throws away all exceptions is rarely the best way to do any thing. You might also consider not usi g strings as parameters. URI and File would be good alternatives.

If you want to copy streams transferTo is a good way.

time4tea
  • 2,169
  • 3
  • 16
  • 21