Description
Downloads an URL into a temporary file that is removed the next time the tempFileManager class is called, which means the next time gvSIG is launched.
License
Open Source License
Parameter
Parameter | Description |
---|
url | a parameter |
name | a parameter |
Exception
Parameter | Description |
---|
IOException | an exception |
ServerErrorResponseException | an exception |
ConnectException | an exception |
UnknownHostException | an exception |
Declaration
public static File downloadFile(URL url, String name)
throws IOException, ConnectException, UnknownHostException
Method Source Code
//package com.java2s;
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
*
* Copyright (C) 2004-2007 IVER T.I. and Generalitat Valenciana.
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
*
* For more information, contact://from www.ja v a 2 s .c om
*
* Generalitat Valenciana
* Conselleria d'Infraestructures i Transport
* Av. Blasco Ib??ez, 50
* 46010 VALENCIA
* SPAIN
*
* +34 963862235
* gvsig@gva.es
* www.gvsig.gva.es
*
* or
*
* IVER T.I. S.A
* Salamanca 50
* 46005 Valencia
* Spain
*
* +34 963163400
* dac@iver.es
*/
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Hashtable;
public class Main {
/**
* <b>key</b>: URL, <b>value</b>: path to the downloaded file.
*/
private static Hashtable downloadedFiles;
public static final String TEMPDIRECTORYPATH = System.getProperty("java.io.tmpdir") + "/tmp-andami";
/**
* Downloads an URL into a temporary file that is removed the next time the
* tempFileManager class is called, which means the next time gvSIG is launched.
*
* @param url
* @param name
* @return
* @throws IOException
* @throws ServerErrorResponseException
* @throws ConnectException
* @throws UnknownHostException
*/
public static File downloadFile(URL url, String name)
throws IOException, ConnectException, UnknownHostException {
File f = null;
try {
if ((f = getPreviousDownloadedURL(url)) == null) {
File tempDirectory = new File(TEMPDIRECTORYPATH);
if (!tempDirectory.exists())
tempDirectory.mkdir();
f = new File(TEMPDIRECTORYPATH + "/" + name + System.currentTimeMillis());
System.out.println("downloading '" + url.toString() + "' to: " + f.getAbsolutePath());
f.deleteOnExit();
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
byte[] buffer = new byte[1024 * 256];
InputStream is = url.openStream();
long readed = 0;
for (int i = is.read(buffer); i > 0; i = is.read(buffer)) {
dos.write(buffer, 0, i);
readed += i;
}
dos.close();
addDownloadedURL(url, f.getAbsolutePath());
}
} catch (IOException io) {
io.printStackTrace();
}
return f;
}
/**
* Returns the content of this URL as a file from the file system.<br>
* <p>
* If the URL has been already downloaded in this session and notified
* to the system using the static <b>Utilities.addDownloadedURL(URL)</b>
* method, it can be restored faster from the file system avoiding to
* download it again.
* </p>
* @param url
* @return File containing this URL's content or null if no file was found.
*/
private static File getPreviousDownloadedURL(URL url) {
File f = null;
if (downloadedFiles != null && downloadedFiles.containsKey(url)) {
String filePath = (String) downloadedFiles.get(url);
f = new File(filePath);
}
return f;
}
/**
* Adds an URL to the table of downloaded files for further uses. If the URL
* already exists in the table its filePath value is updated to the new one and
* the old file itself is removed from the file system.
*
* @param url
* @param filePath
*/
private static void addDownloadedURL(URL url, String filePath) {
if (downloadedFiles == null)
downloadedFiles = new Hashtable();
String fileName = (String) downloadedFiles.put(url, filePath);
//JMV: No se puede eliminar el anterior porque puede que alguien lo
// este usando
/*
if (fileName!=null){
File f = new File(fileName);
if (f.exists())
f.delete();
}
*/
}
}
Related
- downloadFile(String urlString, String filename)
- downloadFile(String urlToDownload, File locationToStore)
- downloadFile(URL fileUrl, File out)
- downloadFile(URL theURL, String filePath)
- downloadFile(URL url, File toFile)
- downloadFile(URL url, String path, String fileName)
- downloadFileAndRetry(final File file, final URL url, final int retry)
- downloadFileFromInternet(String remoteUrl)
- downloadFileFromUrl(String urlPath, File file)