Java URL Download downloadFile(URL url, String path, String fileName)

Here you can find the source of downloadFile(URL url, String path, String fileName)

Description

download File

License

Apache License

Declaration

public static boolean downloadFile(URL url, String path, String fileName) 

Method Source Code

//package com.java2s;
/*//  w w w  .  j  a  v  a  2  s .  com
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*  
*      http://www.apache.org/licenses/LICENSE-2.0
*  
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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

public class Main {

    public static boolean downloadFile(URL url, String path, String fileName) {

        // int bytesum = 0;
        int byteread = 0;

        boolean result = false;
        try {
            URLConnection conn = url.openConnection();
            InputStream inStream = conn.getInputStream();
            FileOutputStream fs = new FileOutputStream(
                    new StringBuilder(path).append("/").append(fileName).toString());
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                // bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            result = true;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        }

        return result;

    }

    public static boolean downloadFile(String url, String path, String fileName) {

        int bytesum = 0;
        int byteread = 0;

        boolean result = false;
        InputStream inStream = null;
        URLConnection conn = null;
        FileOutputStream fs = null;
        try {

            conn = new URL(url).openConnection();
            inStream = conn.getInputStream();
            fs = new FileOutputStream(new StringBuilder(path).append("/").append(fileName).toString());
            byte[] buffer = new byte[1204];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            result = true;
        } catch (FileNotFoundException e) {
        } catch (IOException e) {
        } finally {
            if (inStream != null)
                try {
                    inStream.close();
                } catch (IOException e) {
                }
            if (fs != null)
                try {
                    fs.close();
                } catch (IOException e) {
                }
        }
        return result;
    }
}

Related

  1. downloadFile(String urlToDownload, File locationToStore)
  2. downloadFile(URL fileUrl, File out)
  3. downloadFile(URL theURL, String filePath)
  4. downloadFile(URL url, File toFile)
  5. downloadFile(URL url, String name)
  6. downloadFileAndRetry(final File file, final URL url, final int retry)
  7. downloadFileFromInternet(String remoteUrl)
  8. downloadFileFromUrl(String urlPath, File file)
  9. downloadFileFromWeb(URL resourceURL, String fullPath)