Java URL Download download(final String sURL, final String sFilename)

Here you can find the source of download(final String sURL, final String sFilename)

Description

Download a file at a remote URL to the local disk or to the memory.

License

LGPL

Parameter

Parameter Description
sURL Content to download
sFilename Local file or directory. If it's a directory then the last part of the URL (after the last "/") will be used as a file name. Can be <code>null</code> if you want to get back the contents directly.

Exception

Parameter Description
IOException in case of problems

Return

the contents of the file if the filename is null, or the file name if the second parameter is not null

Declaration

public static String download(final String sURL, final String sFilename) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import java.net.URL;
import java.net.URLConnection;

public class Main {
    /**//from  ww w  .  j a  va  2  s.  c o m
     * Download a file at a remote URL to the local disk or to the memory.
     * 
     * @param sURL Content to download
     * @param sFilename Local file or directory. If it's a directory then the last part of the URL (after the last "/") will be used as a file name.
     *                Can be <code>null</code> if you want to get back the contents directly.
     * @return the contents of the file if the filename is <code>null</code>, or the file name if the second parameter is not null 
     * @throws IOException in case of problems
     * @since 1.0.5 (23.03.2008)
     */
    public static String download(final String sURL, final String sFilename) throws IOException {
        final URL u = new URL(sURL);

        final URLConnection conn = u.openConnection();

        final InputStream is = conn.getInputStream();

        final byte[] buff = new byte[10240];
        int read;

        final OutputStream os;

        String actualFileName = sFilename;

        if (sFilename != null) {
            File f = new File(sFilename);

            if (f.exists() && f.isDirectory()) {
                String sLastPart = sURL;

                int idx = sLastPart.indexOf('?');

                if (idx >= 0)
                    sLastPart = sLastPart.substring(0, idx);

                idx = sLastPart.lastIndexOf('/');

                if (idx >= 0)
                    sLastPart = sLastPart.substring(idx + 1);

                f = new File(f, sLastPart);

                actualFileName = f.getCanonicalPath();
            }

            os = new FileOutputStream(f);
        } else {
            os = new ByteArrayOutputStream();
        }

        try {
            while ((read = is.read(buff)) > 0) {
                os.write(buff, 0, read);
            }
        } finally {
            os.close();
        }

        is.close();

        if (os instanceof ByteArrayOutputStream) {
            String encoding = conn.getContentEncoding();

            if (encoding == null)
                encoding = "UTF-8";

            final byte[] content = ((ByteArrayOutputStream) os).toByteArray();

            try {
                return new String(content, encoding);
            } catch (final UnsupportedEncodingException uee) {
                return new String(content);
            }
        }

        return actualFileName;
    }
}

Related

  1. downImg(String filePath, String imageUrl, String fileName)
  2. download(final URL url)
  3. download(String downloadUrlStr, String filename)
  4. download(String fileUrl, String savePath, String fileName)
  5. download(String url)