Java URL Download downlod(String url, File dest)

Here you can find the source of downlod(String url, File dest)

Description

downlod

License

Open Source License

Declaration

public static void downlod(String url, File dest) throws MalformedURLException, IOException 

Method Source Code

//package com.java2s;
/**//ww w  .ja  v  a2 s.c o  m
   Licensed under the GNU General Public License version 3
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.gnu.org/licenses/gpl-3.0.html
    
   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.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;

public class Main {
    private static final int TIMEOUT = 30 * 60000;

    public static void downlod(String url, File dest) throws MalformedURLException, IOException {
        int retry = 0;
        while (++retry <= 3) {
            System.out.println("Downloading " + url + " Retry count: " + retry);
            try {
                URLConnection conn = new URL(url).openConnection();
                conn.setConnectTimeout(TIMEOUT);
                conn.setReadTimeout(TIMEOUT);
                InputStream in = conn.getInputStream();
                FileOutputStream fos = new FileOutputStream(dest);
                BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
                byte[] data = new byte[1024];
                int x = 0;
                while ((x = in.read(data, 0, 1024)) >= 0) {
                    bout.write(data, 0, x);
                }
                bout.close();
                in.close();
                return;
            } catch (SocketTimeoutException e) {
            } catch (ConnectException e) {
            }
        }
        throw new SocketTimeoutException();
    }
}

Related

  1. downloadUrlToFile(String surl, File file, String method)
  2. downloadUrlToFile(URL url, File file)
  3. downloadUrlToFile(URL url, File result)
  4. downloadWebpage(String url)
  5. downloadZip(URL file, File dump)
  6. downNetImg(String filePath, String remotePath, String htmlUrl, String fileName)
  7. downPicture(String filePath, String imageUrl, String fileName)
  8. fetchUrl(String _url, String charset)
  9. fetchURL(String url)