home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-05-23 | 3.2 KB | 128 lines | [TEXT/CWIE] |
- package au.com.peter.libraries;
-
- import au.com.peter.libraries.URL;
- import java.io.*;
- import java.net.*;
-
- public class FTPClient extends NetClient {
-
- public void OpenFTPConnection( URL url ) throws IOException {
- OpenConnection( url.host, url.port );
- Login( url.username, url.password );
- if ( url.path != null && url.path.length() > 0 ) {
- CD( url.path );
- }
- }
-
- public void DoOpenConnection( Socket conn ) throws IOException {
- super.DoOpenConnection( conn );
- if ( ReadResponseHighCode() != 2 ) {
- throw new FTPClientException( "Connection refused by server" );
- }
- }
-
- public void Login( String username, String password ) throws IOException {
- Login( username, password, null );
- }
-
- public void Login( String username, String password, String account ) throws IOException {
- CheckOpen();
- int response;
-
- response = SendCommandHighCode( "USER " + username );
- if ( response == 3 ) {
- response = SendCommandHighCode( "PASS " + password );
- }
- if ( response == 3 ) {
- response = SendCommandHighCode( "ACCT " + account );
- }
- if ( response != 2 ) {
- throw new FTPClientException( "login failed" );
- }
- }
-
- public void CD( String path ) throws IOException {
- SendCommandCheck( "CWD " + path );
- }
-
- public void SetBinaryMode( boolean binary ) throws IOException {
- if ( binary ) {
- SendCommandCheck( "TYPE I" );
- } else {
- SendCommandCheck( "TYPE A" );
- }
- }
-
- public void SetMacBinaryMode( boolean macbinary ) throws IOException {
- if ( macbinary ) {
- SendCommandCheck( "MACB E" );
- } else {
- SendCommandCheck( "MACB D" );
- }
- }
-
- public Socket OpenDataConnection( String command ) throws IOException {
- Socket data_connection = null;
- ServerSocket data_listener = new ServerSocket( 0, 1 );
- try {
- byte[] address;
- if ( true ) {
- address = new byte[4];
- address[0] = (byte) 206;
- address[1] = (byte) 126;
- address[2] = (byte) 100;
- address[3] = (byte) 110;
- } else {
- address = data_listener.getInetAddress().getAddress();
- }
- if ( address.length != 4 ) {
- throw new FTPClientException( "IP address must be four bytes" );
- }
- int port = data_listener.getLocalPort();
-
- String port_command = "PORT " +
- (address[0] & 0xFF) + "," +
- (address[1] & 0xFF) + "," +
- (address[2] & 0xFF) + "," +
- (address[3] & 0xFF) + "," +
- ((port >> 8) & 0xFF) + "," +
- (port & 0xFF);
- SendCommandCheck( port_command );
-
- if ( SendCommandHighCode( command ) != 1 ) {
- throw new NetClientException( "command '"+ ObscureCommand( command ) + "' failed to return 1xx" );
- }
-
- data_connection = data_listener.accept();
- } finally {
- data_listener.close();
- }
-
- return data_connection;
- }
-
- public InputStream OpenInputDataConnection( String command ) throws IOException {
- return OpenDataConnection( command ).getInputStream();
- }
-
- public OutputStream OpenOutputDataConnection( String command ) throws IOException {
- return OpenDataConnection( command ).getOutputStream();
- }
-
- public void CloseConnection() {
- if ( ConnectionIsOpen() ) {
- try {
- SendCommandCheck( "QUIT" );
- } catch ( Exception e );
- super.CloseConnection();
- }
- }
-
- }
-
- class FTPClientException extends NetClientException {
- public FTPClientException( String s ) {
- super( s );
- }
- }
-