Download from URL : Utilities « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JDK 6
25.JNDI LDAP
26.JPA
27.JSP
28.JSTL
29.Language Basics
30.Network Protocol
31.PDF RTF
32.Reflection
33.Regular Expressions
34.Scripting
35.Security
36.Servlets
37.Spring
38.Swing Components
39.Swing JFC
40.SWT JFace Eclipse
41.Threads
42.Tiny Application
43.Velocity
44.Web Services SOA
45.XML
Java » Network Protocol » UtilitiesScreenshots 
Download from URL
     

/**
 *
    DEScribe - A Discrete Experience Sampling cross platform application
    Copyright (C) 2011
    Sébastien Faure <sebastien.faure3@gmail.com>,
    Bertrand Gros   <gros.bertrand@gmail.com>,
    Yannick Prie    <yannick.prie@univ-lyon1.fr>.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 *
 * Source : http://www.javafr.com/codes/TELECHARGER-FICHIER-PARTIR-URL_42988.aspx
 * Author : Jaoued Zahraoui for Javafr.
 *
 *
 */

//package api.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;


/**
 * Class UrlHelper.java
 * @description Tool to download file from url
 @author Sébastien Faure  <sebastien.faure3@gmail.com>
 @author Bertrand Gros    <gros.bertrand@gmail.com>
 @version 2011-02-01
 */
public class UrlHelper {

    public static void downloadFile(String adresse) {

        downloadFile(adresse, null);
    }

    public static void downloadFile(String adresse, File dest) {
        BufferedReader reader = null;
        FileOutputStream fos = null;
        InputStream in = null;
        try {

            // création de la connection
            URL url = new URL(adresse);
            URLConnection conn = url.openConnection();
            String FileType = conn.getContentType();

            int FileLenght = conn.getContentLength();
            if (FileLenght == -1) {
                throw new IOException("Fichier non valide.");
            }

            // lecture de la réponse
            in = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(in));
            if (dest == null) {
                String FileName = url.getFile();
                FileName = FileName.substring(FileName.lastIndexOf('/'1);
                dest = new File(FileName);
            }
            fos = new FileOutputStream(dest);
            byte[] buff = new byte[1024];
            int l = in.read(buff);
            while (l > 0) {
                fos.write(buff, 0, l);
                l = in.read(buff);
            }
        catch (Exception e) {
            e.printStackTrace();
        finally {
            try {
                fos.flush();
                fos.close();
            catch (IOException e) {
                e.printStackTrace();
            }
            try {
                reader.close();
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

   
    
    
    
    
  
Related examples in the same category
1.A class that encodes URL parameter values for MIDP devices.
2.Get the listing of everyone logged on
3.Scan your computer for ports in useScan your computer for ports in use
4.Using the URL Class (GetURL.java)
5.TCP socket monitor
6.Create Socket helper
7.Implements a TCP/IP bounce utility (proxy)
8.URL utilities class that makes it easy to create new URLs based off of old URLs without having to assemble or parse them yourself
9.URL ParserURL Parser
10.A simple class to load an URL in a browser
11.Using the java API URL class, extract the http/https hostname.
12.Utility class for URL decoding.
13.Utility class for URL encoding.
14.Allows easy downloading of files from HTTP sites
15.enum Http Status
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.