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"); + } +}