Java URL Download download(String urlString)

Here you can find the source of download(String urlString)

Description

download

License

Apache License

Declaration

public static File download(String urlString) 

Method Source Code


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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Main {
    public static File download(String urlString) {
        StringBuffer sb = new StringBuffer(System.getProperty("java.io.tmpdir")).append(File.separator)
                .append(System.currentTimeMillis()).append(".jpg");
        File outFile = new File(sb.toString());
        URL url = null;/*w  w  w  .  j a  va2s .  c o  m*/
        OutputStream os = null;
        InputStream is = null;
        try {
            url = new URL(urlString);
            os = new FileOutputStream(outFile);
            is = url.openStream();
            byte[] buff = new byte[1024];
            while (true) {
                int count = is.read(buff);
                if (count == -1) {
                    break;
                }
                os.write(buff, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return outFile;
    }
}

Related

  1. download(String url)
  2. download(String url, String path)
  3. download(String url, String saveFile)
  4. download(String url, String toFile)
  5. download(String urlAddress, File file)
  6. download(String urlString, File output)
  7. download(URL source, File destination)
  8. download(URL sourceUrl, File destinationFile)
  9. download(URL url)