commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Ghislain Gadbois" <ghislain.gadb...@bell.ca>
Subject [Net]: FTPClient.abort problem
Date Mon, 13 Mar 2006 19:29:02 GMT
Hi,

I'm using Jakarta Commons Net's FTPClient to do FTP transfers.  I use version 1.4.1 on J2SDK
1.4.2_11.

I'm trying to abort an FTP transfer in a separate thread.

What happens is:
1- The thread that is downloading the file is not interrupted so the file is all downloaded
2- Since the download thread is not interrupted, the responses are not handled properly:
   * The thread that is calling abort() gets a 426 - transfer aborted response
   * The thread that is downloading the file get 226 ABOR command succesful

Following this message is a sample program to reproduce the problem and a sample of this program's
output...

Is there something that I'm doing wrong?

What would be required to solve the problem?

Thanks a lot for your help!


Ghis


---------- Sample code -------------

import java.io.FileOutputStream;
import org.apache.commons.net.ProtocolCommandEvent;
import org.apache.commons.net.ProtocolCommandListener;
import org.apache.commons.net.ftp.FTPClient;

public class TestsNetCommons
{
    public static void main(String[] pasArgs)
    {
        final Object            oMonitor    = new Object();
        FTPClient               oFtp        = null;
        Thread                  oThread;
        ProtocolCommandListener oListener   = new ProtocolCommandListener()
        {
            public void protocolCommandSent(ProtocolCommandEvent poEvent)
            {
                String sMessage;

                sMessage = poEvent.getMessage();

                if(sMessage != null)
                {
                    sMessage = sMessage.trim();
                }

                // Remove the password from the log files...
                if(sMessage.startsWith("PASS "))
                {
                    sMessage = sMessage.substring(0, 5) + "*****";
                }

                System.out.println("Client:  " + sMessage);
            }


            public void protocolReplyReceived(ProtocolCommandEvent poEvent)
            {
                String sMessage;

                sMessage = poEvent.getMessage();

                if(sMessage != null)
                {
                    sMessage = sMessage.trim();
                }

                System.out.println("Server:  " + sMessage);
            }
        };

        try
        {
            oFtp = new FTPClient();

            oFtp.addProtocolCommandListener(oListener);

            oFtp.connect("127.0.0.1");

            if(oFtp.login("ftptest", "ftptest"))
            {
                final FTPClient oFtpClient = oFtp;

                // Thread used to download a file...
                oThread = new Thread()
                {
                    public void run()
                    {
                        FileOutputStream oOut = null;

                        try
                        {
                            oOut = new FileOutputStream("C:\\bigfile.zip");

                            oFtpClient.retrieveFile("bigfile.zip", oOut);
                        }
                        catch(Exception ex)
                        {
                            ex.printStackTrace();
                        }
                        finally
                        {
                            if(oOut != null)
                            {
                                try
                                {
                                    oOut.close();
                                }
                                catch(Exception ex)
                                {
                                    // Don't care...
                                }
                            }
                        }
                    } // run                };

                oThread.start();

                // Wait a little, then abort the transfer...
                Thread.currentThread().sleep(1000);
                oFtp.abort();

                // Wait for the download thread to finish it's job
                oThread.join();
            }
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            if(oFtp != null)
            {
                try
                {
                    oFtp.logout();
                    oFtp.disconnect();
                }
                catch(Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }
}


------------- Sample output --------------------

Server:  220 localhost Microsoft FTP Service (Version 5.0).
Client:  USER ftptest
Server:  331 Password required for ftptest.
Client:  PASS *****
Server:  230 User ftptest logged in.
Client:  PORT 127,0,0,1,10,174
Server:  200 PORT command successful.
Client:  RETR bigfile.zip
Server:  150 Opening ASCII mode data connection for bigfile.zip(29716750 bytes).
Client:  ABOR
Server:  426 Connection closed; transfer aborted.
Server:  226 ABOR command successful.
Client:  QUIT
Server:  221


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message