commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Steve Cole" <sc...@camsbycbs.com>
Subject Re: Question about FTPClient.sendCommand()
Date Tue, 02 Jun 2009 14:21:13 GMT
The sendCommand() does just that. It sends a command to the FTP server, but
you have to understand what a FTP command does.

Some commands are simple. If you send the command FTPCommand.CWD along with
the directory name, the server will change directories.

Some commands require a more complex series of execution, like
FTPCommand.STOR, FTPCommand.RETR and FTPCommand.LIST.

The short answer is you can't simply send a PUT command. PUT is a command
many command line FTP clients understand, but the FTP server doesn't
understand PUT. The FTP server undersatands the integer value 14. When you
send command 14, the server waits for a properly formatted stream of data
that is the actual file. The client must send the command 14, then send the
actual data.

Here is what happens when you PUT (store) a file on the server...

  // this is the put methed in MyFTPClient.java class
  public boolean put()throws Throwable {
    boolean rv = true;
    if (binaryTransfer){
      ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
    }else{
      ftpsClient.setFileType(FTP.ASCII_FILE_TYPE);
    }
    if (passiveMode){
      ftpsClient.enterLocalPassiveMode();
    }else{
      ftpsClient.enterLocalActiveMode();
    }
    InputStream input;
    input = new FileInputStream(localFilename);
    ftpsClient.storeFile(remoteFilename, input);
    input.close();

    return rv;
  }

  // this is the storeFile methed in the FTPClient.java class
    public boolean storeFile(String remote, InputStream local)throws
IOException{
        return __storeFile(FTPCommand.STOR, remote, local);
    }

  // this is the underlying _storeFile methed in the FTPClient.java class
    private boolean __storeFile(int command, String remote, InputStream
local)throws IOException{
        OutputStream output;
        Socket socket;
        // tell server you want to STOR a file
        // the sendCommand method is called in the _openDataConnection_
method
        if ((socket = _openDataConnection_(command, remote)) == null)
            return false;
        // FTPCommand.STOR sent and server is now waiting for file to get
streamed to it
        output = new
BufferedOutputStream(socket.getOutputStream(),getBufferSize());
        if (__fileType == ASCII_FILE_TYPE)
            output = new ToNetASCIIOutputStream(output);
        // Treat everything else as binary for now
        try{
            Util.copyStream(local, output,
getBufferSize(),CopyStreamEvent.UNKNOWN_STREAM_SIZE, null,false);
        }catch (IOException e){
            try{
                socket.close();
            }catch (IOException f){}
            throw e;
        }
        output.close();
        socket.close();
        return completePendingCommand();
    }

    // the sendCommand method is actually called in this method
    protected Socket _openDataConnection_(int command, String arg) throws
IOException  {
        Socket socket;
        if (__dataConnectionMode != ACTIVE_LOCAL_DATA_CONNECTION_MODE &&
                __dataConnectionMode != PASSIVE_LOCAL_DATA_CONNECTION_MODE)
            return null;

        if (__dataConnectionMode == ACTIVE_LOCAL_DATA_CONNECTION_MODE)  {
            ServerSocket server;
            server = _serverSocketFactory_.createServerSocket(0, 1,
getLocalAddress());
            if (!FTPReply.isPositiveCompletion(port(getLocalAddress(),
server.getLocalPort())))   {
                server.close();
                return null;
            }
            if ((__restartOffset > 0) && !restart(__restartOffset)) {
                server.close();
                return null;
            }
            if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
{
                server.close();
                return null;
            }

            // For now, let's just use the data timeout value for waiting
for
            // the data connection.  It may be desirable to let this be a
            // separately configurable value.  In any case, we really want
            // to allow preventing the accept from blocking indefinitely.
            if (__dataTimeout >= 0)
                server.setSoTimeout(__dataTimeout);
            try {
                socket = server.accept();
            } finally {
                server.close();
            }
        }
        else
        { // We must be in PASSIVE_LOCAL_DATA_CONNECTION_MODE

            if (pasv() != FTPReply.ENTERING_PASSIVE_MODE)
                return null;

            __parsePassiveModeReply((String)
_replyLines.get(_replyLines.size() - 1));

            socket = _socketFactory_.createSocket(__passiveHost,
__passivePort);
            if ((__restartOffset > 0) && !restart(__restartOffset))
            {
                socket.close();
                return null;
            }

            if (!FTPReply.isPositivePreliminary(sendCommand(command, arg)))
            {
                socket.close();
                return null;
            }
        }

        if (__remoteVerificationEnabled && !verifyRemote(socket))
        {
            InetAddress host1, host2;

            host1 = socket.getInetAddress();
            host2 = getRemoteAddress();

            socket.close();

            throw new IOException(
                "Host attempting data connection " + host1.getHostAddress()
+
                " is not same as server " + host2.getHostAddress());
        }

        if (__dataTimeout >= 0)
            socket.setSoTimeout(__dataTimeout);

        return socket;
    }




----- Original Message ----- 
From: "Kevin Dougan" <kevindougan@hotmail.com>
To: <user@commons.apache.org>
Sent: Tuesday, June 02, 2009 9:53 AM
Subject: RE: Question about FTPClient.sendCommand()





Yes, I saw that specific API method call, but I don't really want to
implement a big set of IF THEN clauses in my code...then I just become an
interpreter for the FTP specification.



Maybe I should be asking a different question, like what is sendCommand()
used for in the first place?


Maybe it's not possible to do what I want to do, and I WILL have to
String-compare everything I read in, and use an IF THEN block to call the
various API methods, as I encounter different keywords (i.e. re-implement
the wheel)...


Thanks!
Kevin
KevinDougan@Hotmail.com


> From: scole@camsbycbs.com
> To: user@commons.apache.org
> Subject: Re: Question about FTPClient.sendCommand()
> Date: Tue, 2 Jun 2009 08:54:14 -0400
>
> The sendCommand method requires the command and args to be passed as two
> separate variables.
>
> I think the bigger issue is that simpling sending a PUT command along with
> the filenames is not going to send the file to the server. Check out the
> FTPClient.java source and look at the _storeFile method. You'll see
> everything else that needs to be done to actually store a file.
>
> ----- Original Message ----- 
> From: "Kevin Dougan" <kevindougan@hotmail.com>
> To: <user@commons.apache.org>
> Sent: Tuesday, June 02, 2009 8:38 AM
> Subject: RE: Question about FTPClient.sendCommand()
>
>
>
> Hi and thanks for your reply. I'll try and give an example to
illustrate...
>
>
>
> Let's say there's a file that contains the following lines:
>
> CWD /home/sampleuser/
>
> PUT localfile.txt remotefile.txt
>
>
> I am building a Java Class that will be "dumb", in the sense that it will
> just open the file (after connecting to the FTP Server using some other
> properties), and issue the commands it finds in that file. The idea is
that
> the caller can dynamically build a file of commands, and the program does
> not have to parse them individually and decide what they are, the program
> can simply pass them along to the FTP Server.
>
>
>
> Therefore, I was thinking that I could use the FTPClient.sendCommand()
> method to simple forward those commands, but it is not working.
>
>
>
> I do NOT want to do the following:
>
> ...read line from file: PUT localfile.txt remotefile.txt
>
> ...decide which API method to call: if (line.startsWith("PUT"))
> client.storeFile(remoteFile, localFile); else if
> (line.statrsWith("GET"))...etc, etc
>
>
>
> However, I am trying to do something more simple, like the following:
>
> ...read line from file: PUT localfile.txt remotefile.txt
>
> ...pass the command along
>
> ...read next line from file: : : :
>
> ...pass the command along: : : :
>
> ...etc, etc
>
>
>
> Thanks!
> Kevin
> KevinDougan@Hotmail.com
>
>
> > From: scole@camsbycbs.com
> > To: user@commons.apache.org
> > Subject: Re: Question about FTPClient.sendCommand()
> > Date: Tue, 2 Jun 2009 08:29:55 -0400
> >
> > I'm not sure if I understand what you're trying to accomplish. Are you
> > sending FTP commands to the FTP server? If so, what command? Or are you
> > trying to send operating system shell script commands? The FTP server is
> > only going to accept commands it understands and supports, which is not
> the
> > same as OS shell scripts.
> >
> > ----- Original Message ----- 
> > From: "Kevin Dougan" <kevindougan@hotmail.com>
> > To: <user@commons.apache.org>
> > Sent: Tuesday, June 02, 2009 7:14 AM
> > Subject: Question about FTPClient.sendCommand()
> >
> >
> >
> > Greetings!
> >
> > I have been searching around a lot, trying to figure out how to properly
> use
> > the FTPClient.sendCommand() API method, but to no avail! Perhaps someone
> > here can answer my question very quickly...?
> >
> > Basically, I want to implement a "dumb" FTP class who handles "batch
> > processing". I thought I could just read lines from a file, and then
pass
> > them through to an FTP Server using the sendCommand() method, but it
> always
> > replies with "command not understood".
> >
> > Am I totally off track with what I want to do here? Am I using the API
> > method incorrectly or for the wrong purpose?
> >
> > I am trying to avoid parsing every command, doing a String compare on
each
> > word, and then trying to match it against a specific API method
> > (storeFile(), retrieveFile(), etc.), depending on each String I
> parse...big,
> > long IF blocks are ugly!
> >
> > Thanks!
> > Kevin
> > KevinDougan@Hotmail.com
> >
> >
> > _________________________________________________________________
> > Attention all humans. We are your photos. Free us.
> > http://go.microsoft.com/?linkid=9666046
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> > For additional commands, e-mail: user-help@commons.apache.org
> >
>
> _________________________________________________________________
> We are your photos. Share us now with Windows Live Photos.
> http://go.microsoft.com/?linkid=9666047
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>

_________________________________________________________________
We are your photos. Share us now with Windows Live Photos.
http://go.microsoft.com/?linkid=9666047


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


Mime
View raw message