home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / weasl101.zip / SendFileByftpSun.java < prev    next >
Text File  |  1996-10-21  |  5KB  |  199 lines

  1. import sun.net.*;
  2. import sun.net.ftp.*;
  3. import java.io.*;
  4.  
  5. /*
  6. Uses the undocumented sun.* classes to send a file to an ftp host
  7. */
  8. class SendFileByftpSun extends FtpClient
  9. {
  10. String HostName = "";
  11. String UserName = "";
  12. String Password = "";
  13. String HostDirectory = "";
  14. String HostFileName;
  15. File     FileToPut;
  16.  
  17. /*---------------------------------------------------------------
  18. ---------------------------------------------------------------*/
  19. SendFileByftpSun( String FToSend, String HName, String UName, String PWord, 
  20.                                     String HDirectory, String HFName )
  21.     {
  22.    /* Split filespec into directory and file */
  23.    FileToPut = new File( FToSend );
  24.  
  25.     if( !FileToPut.exists() || !FileToPut.canRead() || !FileToPut.isFile() )
  26.          {
  27.       System.err.println( "Can't open file for reading " + FileToPut );
  28.       System.exit(-1);
  29.          }
  30.    /*
  31.    int EndOfDir = FToSend.lastIndexOf( File. separatorChar );
  32.    if( EndOfDir < 0 )
  33.       {
  34.        FileToPutDirectory = "";
  35.         FileToPutName = FToSend;
  36.         }
  37.     else
  38.          {
  39.       if( EndOfDir < FToSend.length()-1 )
  40.           {
  41.           FileToPutDirectory = FToSend.substring( 0, EndOfDir );
  42.             FileToPutName = FToSend.substring( EndOfDir+1 );
  43.            }
  44.      else
  45.             {
  46.          System.err.println( "Source file is a directory" );
  47.          return;
  48.          }
  49.       System.out.println( "Source directory = " + FileToPutDirectory );
  50.       System.out.println( "Source file = " + FileToPutName );
  51.       }
  52.    */
  53.  
  54.    HostName = HName;
  55.    UserName = UName;
  56.    Password = PWord;
  57.    HostDirectory = HDirectory;
  58.    if( HFName != null && HFName.length() != 0 )
  59.        {
  60.       HostFileName = HFName;
  61.       }
  62.    else
  63.       {    /* Destination file name not specified, use source file name */
  64.        HostFileName = FileToPut.getName();    
  65.         }
  66.  
  67.    System.out.print( "Opening host..." );
  68.    try
  69.       {
  70.       openServer( HostName );
  71.       }
  72.    catch( IOException e )
  73.       {
  74.         System.out.println( "Error: Unable to open host: " + HostName );
  75.           return;
  76.       }
  77.     System.out.println( "Ok: " + HostName );
  78.  
  79.      /* login */
  80. //    System.out.println( "Logging into server..." );
  81.    try
  82.       {
  83.       login( UserName, Password );
  84.       }
  85.    catch( IOException e )
  86.       {
  87.         System.out.println( "Error: Unable to login " );
  88.           return;
  89.       }
  90.  
  91.    try
  92.       {
  93.         binary();
  94.           }
  95.    catch( IOException e )
  96.       {
  97.       System.out.println( "Unable to set binary transfer mode" );
  98.       }
  99.  
  100.     if( HostDirectory != null && HostDirectory.length() > 0 )
  101.          {
  102.          /* Change host directory */
  103.  
  104. //        System.out.println( "Changing directory on host..." );
  105.        try
  106.           {
  107.           cd( HostDirectory );
  108.           }
  109.        catch( IOException e )
  110.           {
  111.             System.out.println( "Error: Unable to change to remote directory: "  + HostDirectory );
  112.               return;
  113.              }
  114.       }
  115.  
  116. /* not needed, file overwrites anyway
  117.     System.out.println( "Deleting remote file..." );
  118.    try
  119.       {
  120.         issueCommand( "DELE " + HostFileName );
  121.       }
  122.    catch( IOException e )
  123.       {
  124.         System.out.println( "Error: Unable to delete remote file: "  + HostFileName );
  125.       }
  126. */
  127.  
  128.     /* PUT file */
  129.     System.out.println( "Sending file to host..." );
  130.    try
  131.       {
  132.         TelnetOutputStream out = put( HostFileName );
  133.           FileInputStream fin= new FileInputStream( FileToPut.getPath() );
  134.         int chr;
  135.         
  136.         /* This could probably be speeded up (currently does a char by char copy) */
  137.           for( long size = fin.available(); size>0; size-- )
  138.             {
  139.           chr = fin.read();
  140.           out.write( (char)chr );    
  141.            out.flush();
  142.          }
  143.       fin.close();
  144.       out.close();
  145.       }
  146.    catch( IOException e )
  147.       {
  148.         System.out.println( "Error: " + e );
  149.         System.out.println( "Error: Unable to 'put' file onto host " );
  150.       }
  151.     System.out.println( "Closing FTP session" );
  152.     /* Terminate session */
  153.      try
  154.           {
  155.          closeServer();    
  156.        }
  157.    catch( IOException e )
  158.       {
  159.         System.out.println( "Error: Unable to close host: " + HostName );
  160.           return;
  161.       }
  162.    }
  163.  
  164. /*---------------------------------------------------------------
  165. Test main for 'SendFileByftpSun'.
  166.  
  167. Uploads a file using FTP, probably extremely useful by itself ;)
  168. ---------------------------------------------------------------*/
  169. public static void main( String args[] )
  170.    {
  171.    String HFName = null;
  172.    String HDir = null;
  173.  
  174.     if( args.length < 4 )
  175.          {
  176.       System.out.println();
  177.       System.out.println( "Too few parameters" );
  178.       System.out.println();
  179.       System.out.println( "Usage: " );
  180.       System.out.println( "   src_filespec ftphostname username password [remotedirspec] [remotefilename] " );
  181.       System.out.println();
  182.       System.exit(-1);
  183.          }
  184.  
  185.     if( args.length > 4 )
  186.          {                       /* Optional parameters */
  187.       HDir = args[4];
  188.          if( args.length > 5 )
  189.              {
  190.               HFName = args[5];
  191.              }
  192.       }
  193.  
  194.     SendFileByftpSun trans = new SendFileByftpSun( args[0], args[1], args[2], args[3], HDir, HFName );
  195.    }
  196. }
  197.  
  198.  
  199.