/* * [Download.java] * * Summary: command line automated download a file from the Internet. * * Copyright: (c) 1999-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 2.5 2008-08-10 add setReadTimeout and setConnectTimeout methods. */ package com.mindprod.filetransfer; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import static java.lang.System.*; /** * command line automated download a file from the Internet. *

* use: * java com.mindprod.filetransfer.Download http://x.com/y.html C:\y.txt * * @author Roedy Green, Canadian Mind Products * @version 2.5 2008-08-10 add setReadTimeout and setConnectTimeout methods. * @since 1999 */ public final class Download { public static final String USAGE = "Syntax is: Download [-sni] {source URL of file to download} {target file name}"; /** * 64K is size buffer for file transfer */ private static final int BUFFSIZE = 64 * 1024; static { System.setProperty( "java.net.preferIPv4Stack", "true" ); System.setProperty( "jsse.enableSNIExtension", "false" ); System.setProperty( "jdk.tls.ephemeralDHKeySize", "2048" ); } /** * private constructor not used. Only use this class via static main. */ private Download() { } /** * Download one file * * @param urlString source URL of file to download * e.g."http://www.billabong.com:80/songs/lyrics.txt" * works with http:, https:, file: and possibly others. * @param filename target name of file to save on hard disk. * @param sni true if should use server name identification * * @return true if download succeeded * @see com.mindprod.http.Fetch */ @SuppressWarnings( { "BooleanMethodNameMustStartWithQuestion" } ) public static boolean downloadOneFile( String urlString, String filename, boolean sni ) { URL url; try { url = new URL( urlString ); } catch ( MalformedURLException e ) { throw new IllegalArgumentException( "Malformed URL: " + urlString ); } final File file; try { file = new File( filename ); if ( file.exists() ) { if ( !file.canWrite() ) { // canWrite true implies file exists. throw new IllegalArgumentException( "Cannot write file: " + filename ); } } else { // create file safely before we fool with URL file.createNewFile(); } } catch ( IOException e ) { throw new IllegalArgumentException( "Cannot write file: " + filename ); } FileTransfer downloader = new FileTransfer( BUFFSIZE/* buffsize */ ); // FileTransfer.download does the actual work. return downloader.download( url, file, sni ); } /** * Command line Download * * @param args source url, target file * source URL of file to download * e.g."http://www.billabong.com:80/songs/lyrics.txt" * works with http:, https:, file: and possibly others. */ public static void main( String[] args ) { if ( args.length < 1 ) { throw new IllegalArgumentException( USAGE ); } final boolean sni; final String source; final String target; if ( args[ 0 ].equals( "-sni" ) ) { sni = true; if ( args.length < 3 ) { throw new IllegalArgumentException( USAGE ); } source = args[ 1 ]; target = args[ 2 ]; } else { sni = false; if ( args.length < 2 ) { throw new IllegalArgumentException( USAGE ); } source = args[ 0 ]; target = args[ 1 ]; } boolean success = downloadOneFile( source, target, sni ); if ( success ) { out.println( "Download of " + source + " to " + target + " succeeded." ); } else { err.println( "Download of " + source + " to " + target + " failed. " + FileTransfer.why ); System.exit( 1 ); } } }