/* * [MaxiFileTransfer.java] * * Summary: copy or download a file. * * 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.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import static java.lang.System.*; /** * copy or download a file. *

* To read or write from the client's local hard disk, you will need a signed Applet and. * security clearance. see Signed Applet in the Java glossary. To read a files from the server, the file must be given * public read access, usually the default. To write a file to the server, you server will have to support CGI-PUT with * public access. This is unusual to find. Normally you upload files with FTP. See FTP in the Java glossary. *

* military. * * @author Roedy Green, Canadian Mind Products * @version 2.5 2008-08-10 add setReadTimeout and setConnectTimeout methods. * @since 1999 */ public final class MaxiFileTransfer extends FileTransfer { /** * constructor */ @SuppressWarnings( { "WeakerAccess" } ) public MaxiFileTransfer() { super(); } /** * constructor * * @param buffSize how big the I/O chunks are to copy files. */ public MaxiFileTransfer( int buffSize ) { super( buffSize ); } /** * Test driver * * @param args not used */ public static void main( String[] args ) { if ( DEBUGGING ) { MaxiFileTransfer ft = new MaxiFileTransfer(); out.println( ft.copy( new File( "E:\\temp", "index.html" ), new File( "C:\\temp", "a.txt" ) ) ); try { out.println( ft.download( new URL( "http://mindprod.com/index.html" ), new File( "C:\\temp", "b.txt" ), false ) ); } catch ( MalformedURLException e ) { out.println( "bad download url" ); } try { out.println( ft.upload( new File( "E:\\temp", "index.html" ), new URL( "http://mindprod.com/uploads/c.html" ) ) ); } catch ( MalformedURLException e ) { out.println( "bad upload url" ); } } // end if debugging } // end main /** * Copy a file from a local hard disk to a remote URL. This simulates the HTML PUT upload command. Unfortunately, * most servers do not support it, or refuse it. WARNING: I have not tested this code is untested. I have no access * to a server that supports PUT. WARNING: This code does not work in Netscape or IE. You must run it as a * standalone application. * * @param source existing file to be copied on local hard disk. * @param target remote URL to copy to. e.g. new URL("http://www.billabong.com:80/songs/lyrics.txt") * * @return true if the copy was successful. */ @SuppressWarnings( { "WeakerAccess", "BooleanMethodNameMustStartWithQuestion" } ) public boolean upload( File source, URL target ) { if ( source == null ) { return false; } if ( target == null ) { return false; } final FileInputStream is; final OutputStream os; // Netscape and IE don't support HttpURLConnection final HttpURLConnection urlc; try { // O P E N S O U R C E is = new FileInputStream( source ); long length = source.length(); // O P E N T A R G E T // must have file write/create permission on remote host // Generate a HTTP CGI PUT command // URLConnection with subclasses // http: HttpURLConnection // https: HttpsURLConnectionImpl // file: FileURLConnection urlc = ( HttpURLConnection ) target.openConnection(); if ( urlc == null ) { throw new IOException( "Unable to make a connection." ); } urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/octet-stream" ); urlc.setRequestProperty( "Content-length", Long.toString( length ) ); urlc.setRequestMethod( "PUT" ); urlc.connect(); // ignored if already connected. os = urlc.getOutputStream(); // C O P Y S O U R C E T O T A R G E T boolean success = copy( is, os, length, true ); if ( !success ) { return false; } // getResponseCode will block until the server responds. int statusCode = urlc.getResponseCode(); // C L O S E // is and os handled by inner copy. return statusCode == 0; } catch ( IOException e ) { return false; } } // end upload }