commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Billy Bacon" <billy.ba...@gmail.com>
Subject Fwd: 500 Syntax error, command unrecognized
Date Wed, 04 Apr 2007 14:59:57 GMT
I realized that it may have been a bit obnoxious to paste the entire class
into my post and this might be why I didn't receive any replies. I
apologize. Can someone tell me if they see any obvious issues with this
method...

<code>
public boolean put(String localFilename) {
    try {
        FTPClient client = new FTPClient();
        client.connect("10.4.1.100");
        client.enterLocalPassiveMode();
        client.setFileType(FTPClient.BINARY_FILE_TYPE);
        client.login("un", "pw");

        client.storeUniqueFile(filename, local); // Returns a status code
500 (command unrecognized)

    } catch(SocketException sc) {
        sc.printStackTrace();
    } catch(IOException io) {
        io.printStackTrace();
    } finally {
        client.logout();
        client.disconnect();
    }
}
</code>

Thanks in advance for any feedback.

- Billy -
---------- Forwarded message ----------
From: Billy Bacon <billy.bacon@gmail.com>
Date: Apr 3, 2007 6:19 PM
Subject: 500 Syntax error, command unrecognized
To: commons-user@jakarta.apache.org

Hi,

I just started using the org.apache.commons.net.ftp.FTPClient from the
Commons Net project and I've found it very useful. I'm running into one snag
and I can't figure out why I'm getting this error. The subject line of this
e-mail is the reply string I'm getting back in return after I execute the
command since its not executing successfully (500 Syntax error,  command
unrecognized).

I'm getting this when I try to upload a file to an FTP server. My code is
below. All of the methods work in this class except for the two put()
methods I've implemented.

 The following FTPClient methods are failing for me...

- storeFileStream(String, String)
- storeUniqueFile(String, InputStream)

<code>
package com.ussouth.in.commons.net.ftp;

import org.apache.commons.net.ftp.FTPClient;

import java.io.* ;
import java.net.SocketException;

/**
 * This class is used to perform file transfer protocol commands.
 *

 * @version Date: Apr 2, 2007
 *          Time: 9:39:22 AM
 */
public class FileTransferClient {

    private FTPClient client;

    /**
     * Default Constructor. Takes a String for the hostname/server to
connect to.
     *
     * @param hostname A String representing the server to connect to.
     */
    public FileTransferClient(String hostname) {
        try {
            client = new FTPClient();
            client.connect(hostname);
            client.enterLocalPassiveMode();
            client.setFileType(FTPClient.BINARY_FILE_TYPE);
        } catch(SocketException sc) {
            sc.printStackTrace();
        } catch(IOException io) {
            io.printStackTrace();
        }
    }


    /**
     * Method which attempts to login to the FTP server with the given
username and password.
     *
     * @param un A String representing a valid username to the FTP server.
     * @param pw A String representing a valid password for the given
username to the FTP server.
     * @return true if logging in to the FTP server is successful, false
otherwise.
     */
    public boolean login(String un, String pw) {
        try {
            return client.login(un, pw);
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        }
    }


    /**
     * Retrieves the given remoteFile from the server and transfers it to
the local server with the given
     * localFile name.
     *
     * @param remoteFile A String representing the file on the remote
server.
     * @param localFile A String representing the file that the file should
be downloaded to.
     * @return true if the download/retrieve executes successfully, false
otherwise.
     */
    public boolean get(String remoteFile, String localFile) {
        OutputStream os = null;
        try {
            os = new FileOutputStream( localFile );
            return client.retrieveFile (remoteFile, os);
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        } finally {
            try {
                if(os != null) os.close();
            } catch(IOException ioe) {
                //
            }
        }
    }


    /**
     * Returns an InputStream from which a named file from the server can be
read.
     *
     * @param remoteFile The name of the remote file.
     * @return An InputStream from which the remote file can be read.
     *          If the data connection cannot be opened (e.g., the file does
not exist),
     *          null is returned (in which case you may check the reply code
to determine the exact reason for failure).
     */
    public InputStream get(String remoteFile) {
        InputStream is = null;
        try {
            return client.retrieveFileStream(remoteFile);
        } catch(IOException io) {
            io.printStackTrace();
            return null;
        } finally {
            try {
                if(is != null) is.close();
            } catch(IOException ioe) {
                //
            }
        }
    }

    /**
     * Method which uploads the given filename (localFile) to the remote
server. The file will be written to the server
     * with the given (remoteFile) name.
     *
     * @param localFile A String representing the local file which is to be
uploaded to the server.
     * @param remoteFile A String representing the filename that the
uploaded file should be written to on the server.
     * @return true if the file is uploaded successfully, false otherwise.
     */
    public boolean put(String localFile, String remoteFile) {
        InputStream fis = null;
        OutputStream os = null;

        try {
            fis = new FileInputStream(localFile);
            os = client.storeFileStream(remoteFile);

            byte buf[] = new byte[8192];
            int bytesRead = fis.read(buf);
            while (bytesRead != -1) {
                os.write(buf, 0, bytesRead);
                bytesRead = fis.read(buf);
            }

            return true;

        } catch(FileNotFoundException fnfe) {
            fnfe.printStackTrace();
            return false;
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        } finally {
            try {
                if(fis != null) fis.close();
                if(os != null) os.close();
            } catch(IOException io) {
                //
            }
        }
    }


    /**
     * Stores a file on the server using a unique name derived from the
given name and taking input
     * from the given InputStream. This method DOES close the given
InputStream when the upload is complete.
     *
     * @param filename
     * @param local
     * @return
     */
    public boolean put(String filename, InputStream local) {
        try {
            return client.storeUniqueFile(filename, local);
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        } finally {
            try {
                if(local != null) local.close();
            } catch(IOException io) {
                // fail silently for now...
            }
        }
    }


    /**
     *
     * @param local
     * @return
     */
    public boolean put(InputStream local) {
        try {
            return client.storeUniqueFile(local);
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        } finally {
            try {
                if(local != null) local.close();
            } catch(IOException io) {
                // fail silently for now...
            }
        }
    }

    /**
     * Method which renames the given (from) name to the given (to) name.
     *
     * @param from A String representing the existing filename.
     * @param to A String representing the filename that the existing file
should be renamed to.
     * @return true if the rename executes successfully, false otherwise.
     */
    public boolean rename(String from, String to) {
        try {
            return client.rename(from, to);
        } catch(IOException io) {
            io.printStackTrace();
            return false;
        }
    }


    /**
     * Deletes a file from the server with the given name.
     *
     * @param filename A String representing the name of the file that is to
be deleted.
     * @return true if the file is deleted successfully, false otherwise.
     */
    public boolean delete(String filename) {
        File f = new File(filename);
        if(f.exists()) {
            f.delete();
            return true;
        }

        return false;
    }


    /**
     * Method which should be called when you are finished with this client.
Logouts out of the FTP
     * server and disconnects from it.
     */
    public void disconnect() {
        try {
            client.logout();
            client.disconnect();
        } catch(IOException io) {
            io.printStackTrace();
        }
    }


    /**
     * Method which allows you to navigate the filesystem and change to/from
your current directory.
     *
     * @param dir A String representing the directory name to change to.
     * @return true if the change command was success, false otherwise.
     */
    public boolean changeDirectory(String dir) {
        try {
            client.changeWorkingDirectory(dir);
        } catch(IOException io) {
            io.printStackTrace();
        }
        return true;
    }


    /**
     * Method which returns the current working directory of the server.
     *
     * @return the current working directory of the server
     */
    public String getCurrentWorkingDirectory() {
        try {
            return client.printWorkingDirectory();
        } catch(IOException io) {
            io.printStackTrace();
            return null;
        }
    }

    /**
     * Method which will return all filenames in the current working
directory.
     *
     * @return A String array consisting of all filenames in the current
directory.
     */
    public String[] listFileNames() {
        try {
            return client.listNames();
        } catch(IOException io) {
            io.printStackTrace();
            return null;
        }
    }

    /**
     * Method to retrieve the reply code from the ftp client.
     *
     * @return An int representing the reply code received from the server
after a command
     *          is sent to the server.
     */
    public int getReplyCode() {
        return client.getReplyCode();
    }

    /**
     * Method to retrieve the reply string from the ftp client.
     *
     * @return An String representing the reply string from the server after
a command
     *          is sent to the server.
     */
    public String getReplyString() {
        return client.getReplyString();
    }
}
</code>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message