Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;

import java.net.URL;

public class Main {

    public static long downloadFileFromUrl(String urlPath, File file) {
        long size = 0;
        try {
            URL url = new URL(urlPath);
            HttpURLConnection httpurlconnection = (HttpURLConnection) url.openConnection();
            BufferedInputStream bufferedinputstream = new BufferedInputStream(httpurlconnection.getInputStream());
            BufferedOutputStream bufferedoutputstream = new BufferedOutputStream(new FileOutputStream(file));
            int i;
            while ((i = bufferedinputstream.read()) != -1) {
                bufferedoutputstream.write(i);
            }
            bufferedinputstream.close();
            bufferedoutputstream.close();
            httpurlconnection.disconnect();
            size = file.length();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return size;
    }
}