fr.ironcraft.assets.FileDownloader.java Source code

Java tutorial

Introduction

Here is the source code for fr.ironcraft.assets.FileDownloader.java

Source

/*
 * Copyright (C) 2014 Thog92
 *
 * 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/>.
 */

package fr.ironcraft.assets;

import org.apache.commons.io.FileUtils;

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

/**
 * @author Thog
 */
public class FileDownloader {

    private final URL url;
    private final File target;
    private final long filesize;

    public FileDownloader(URL u, File f) {
        this.url = u;
        this.target = f;
        this.filesize = tryGetFileSize(u);
    }

    public FileDownloader(URL u, File f, long size) {
        this.url = u;
        this.target = f;
        this.filesize = size;
    }

    private int tryGetFileSize(URL url) {
        HttpURLConnection conn = null;
        try {
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("HEAD");
            conn.getInputStream();
            return conn.getContentLength();
        } catch (IOException e) {
            return -1;
        } finally {
            conn.disconnect();
        }
    }

    public void downloadFile() {
        boolean needupdate = false;
        String fileName = target.getName();
        if (!target.exists()) {
            needupdate = true;

            try {
                target.getParentFile().mkdirs();
                target.createNewFile();

            } catch (IOException ex) {
                target.delete();
            }
        } else {
            long lenght = FileUtils.sizeOf(target);
            if (!(lenght == filesize)) {
                needupdate = true;
            }
        }

        if (needupdate) {
            InputStream in = null;
            FileOutputStream fout = null;
            try {
                in = url.openStream();
                fout = new FileOutputStream(target);

                byte data[] = new byte[1024];
                int count;
                while ((count = in.read(data, 0, 1024)) != -1) {
                    fout.write(data, 0, count);
                }
            } catch (IOException e) {
                System.out.println("Cannot download file : " + fileName);
                target.delete();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        target.delete();
                    }
                }
                if (fout != null) {
                    try {
                        fout.close();
                    } catch (IOException e) {
                        target.delete();
                    }
                }
                System.out.println("File " + fileName + " downloaded successfully");
            }
        }
    }
}