In many of the java applications, there exists a requirement to programmatically copy the files from a Internet URL and save it in a Local Disk in a destination folder specified. This Java based utility addresses this need using the robust and platform independent java language. Some machines have direct internet connection and some machines are connected to the internet through Proxy Server. This utility addresses the needs of both the users. One of the main requirements of a reusable tool is its ability to fit into other applications without major changes. This Utility allows the user to enter the configurable changes in a property file. By this way the utility is more robust and reusable in all the applications in any platform.
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Properties; import java.util.StringTokenizer;
public Class JavaProxy{ public static void main(String args[]) { Properties properties = null; String useProxy = null; String destinationPath = null; File destinationDirFile = null; String proxyHost = null; String proxyPort = null; URL url = null; URLConnection urlC = null; FileOutputStream fos = null; StringTokenizer st = null; String localFile = null; File fileName = null; InputStream is = null; int oneChar = 0; int count = 0; try { properties = new Properties(); properties .load(new FileInputStream("copyFileConstants.properties")); /* * Get the values from the Property * file(copyFileConstants.properties) */ url = new URL(properties.getProperty("file_url")); destinationPath = properties.getProperty("destination_folder"); useProxy = properties.getProperty("use_proxy"); proxyHost = properties.getProperty("proxy_host"); proxyPort = properties.getProperty("proxy_port"); System.out.println("The URL Specified is :" + url); /* * Setting the System Properties about the details of Proxy Server * if any. */ if (useProxy != null && useProxy.equalsIgnoreCase("yes")) { System.out .println("\nYou are behind the Proxy Server and the details are................"); System.out.println("\nThe Proxy Host is :" + proxyHost); System.out.println("\nThe Proxy Port is : " + proxyPort); // set the properties of system properties // proxySet,proxyHost,proxyPort System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxyHost); System.getProperties().put("proxyPort", proxyPort); } else { System.out.println("\nYou are not using any Proxy Server....."); System.out .println("\nIf you are using proxy server, then set the use_proxy property of copyFileConstants.properties to yes"); } /* * Establishing a connection to the URL specified in the properties * file. */ System.out.println("\nTrying to Connect to the URL : " + url); urlC = url.openConnection(); /* * Copy resource to local file, use remote file,if no local file * name specified */ is = url.openStream(); System.out.println("Connection established to " + url + "..."); // Print info about resource System.out .print("\nCopying resource (type: " + urlC.getContentType()); Date date = new Date(urlC.getLastModified()); System.out.println(", modified on: " + date.toString() + ")............................"); System.out.flush(); // Get only file name st = new StringTokenizer(url.getFile(), "/"); while (st.hasMoreTokens()) { localFile = st.nextToken(); } if (destinationPath == null)// if no destination path specified { fos = new FileOutputStream(localFile); } else {// destination path specified if (destinationPath != null && !(destinationPath.equals(""))) { destinationDirFile = new File(destinationPath); /* * Create the destination folder in the file system if not already exists */ if (!destinationDirFile.exists()) { System.out.println("\nThe destination directory " + destinationPath + " does not exists. Creating Directory..."); destinationDirFile.mkdirs(); System.out.println("\nDestination Directory Created...."); } else { System.out.println("\nDestination Directory already exists in the file system and we are using this."); } } fileName = new File(new File(destinationPath), localFile); fos = new FileOutputStream(fileName); } while ((oneChar = is.read()) != -1) { fos.write(oneChar); count++; } is.close(); fos.close(); System.out.println("\n\n "+count + " byte(s) copied..............................."); } catch (MalformedURLException e) { System.err.println(e.toString()); } catch (IOException e) { System.err.println(e.toString()); } } }