From 25da959635abb3510c340fb477fc78ddce6afe1c Mon Sep 17 00:00:00 2001 From: Gianpaolo Coro Date: Mon, 19 Oct 2015 16:35:45 +0000 Subject: [PATCH] git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-analysis/SeaDataNetConnector@119874 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../dataanalysis/seadatanet/Downloader.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 src/org/gcube/dataanalysis/seadatanet/Downloader.java diff --git a/src/org/gcube/dataanalysis/seadatanet/Downloader.java b/src/org/gcube/dataanalysis/seadatanet/Downloader.java new file mode 100644 index 0000000..00849ca --- /dev/null +++ b/src/org/gcube/dataanalysis/seadatanet/Downloader.java @@ -0,0 +1,40 @@ +package org.gcube.dataanalysis.seadatanet; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger; + +public class Downloader { + + public static void downloadFile(String fileurl, String destinationFile) throws Exception{ + try { + URL smpFile = new URL(fileurl); + URLConnection uc = (URLConnection) smpFile.openConnection(); + InputStream is = uc.getInputStream(); + AnalysisLogger.getLogger().debug("Retrieving file from " + fileurl + " to :" + destinationFile); + inputStreamToFile(is, destinationFile); + is.close(); + } catch (Exception e) { + throw e; + } + } + + public static void inputStreamToFile(InputStream is, String path) throws FileNotFoundException, IOException { + FileOutputStream out = new FileOutputStream(new File(path)); + byte buf[] = new byte[1024]; + int len = 0; + while ((len = is.read(buf)) > 0) + out.write(buf, 0, len); + out.close(); + } + + public static void main(String args[]) throws Exception{ + downloadFile("https://dl.dropboxusercontent.com/u/12809149/2_Reviewed.jpg","test.jpg"); + } +}