commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Luc De Kesel" <luc.de_ke...@the-ecorp.com>
Subject COMMONS NET FTP
Date Tue, 16 Nov 2004 18:31:40 GMT
Hi,

 

I wrote an FTP client using org.apache.commons.net.ftp.FTPClient;

I have one major problem when doing invoking "get":

1. If I connect to a remote FTP server (Windows2000) on the same lan,
the client can list the remote dir but the files are not downloaded,
they remain empty.

2. If I connect to an FTP server on my client PC (Windows XP),
everything works OK.

There is no exception, no error return code, just nothing.

Has anyone met similar problems ?

Please help me, the project deadline is coming near.

This is my code :

 

package client;

 

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

 

import java.util.Date;

import java.util.Vector;

import java.io.IOException;

import java.io.OutputStream;

import java.io.FileOutputStream;

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

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

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

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

 

 

/**

 * This Client object represents an FTP connection with the
backend-server.

  * @author Luc De Kesel

 */

public class FtpClient {

       private static final Log LOG =
LogFactory.getLog(FtpClient.class.getName());

       private String server;

       private String userId;

       private String password;

       private String remoteDir;

       private String localDir;

       private FTPClient client;

       /**

        * Create a Backend object and connect to it the BackendServer.

        * @throws Exception

        */

       public FtpClient(String server, String userId, String password,
String remoteDir, String localDir) throws Exception {

              this.server = server;

              this.userId = userId;

              this.password = password;

              this.remoteDir = remoteDir.endsWith("/") ? remoteDir :
remoteDir + "/";

              this.localDir = localDir.endsWith("/") ? localDir :
localDir + "/";

              this.client = new FTPClient();

              try {

                     client.connect(server);

                     int reply = client.getReplyCode();

                     if(!FTPReply.isPositiveCompletion(reply)) {

                           client.disconnect();

                           String error = "FTP server refused
connection.(server=" + server + ")";

                           LOG.error(error);

                           throw new Exception(error);

                     }

                     client.login(userId, password);

                     client.setFileType(FTP.ASCII_FILE_TYPE);

 

                     if (!client.changeWorkingDirectory(remoteDir)) {

                           String error = "Can't set remote directory to
" + remoteDir;

                           LOG.error(error);

                           throw new Exception(error);

                     }

              }

              catch (IOException e) {

                     throw new Exception(e.toString());

              }

 

       }

 

       /**

        * Set the files transfer type. <br>

        * FTP.BINARY_FILE_TYPE or FTP.ASCII_FILE_TYPE

        */    

       public void setFileType(int fileType) throws IOException {

              this.client.setFileType(fileType);

       }

       

       /**

        * Download a file (FTP GET). The file is not renamed.

        * @author ldekesel

        */

       public void get(String remoteFile) throws Exception {

              get(remoteFile, remoteFile);

       }

       

       public void get(String remoteFile, String localFile) throws
Exception {

              OutputStream output = null;

              try {

                     output = new FileOutputStream(this.localDir +
localFile);

                     if (!client.retrieveFile(remoteFile, output)) {

                           int replyCode = client.getReplyCode();

                           String error = "GET " + remoteFile + "
returns [" + replyCode + "]\nserver response : " +
client.getReplyString();

                           throw new Exception(error);

                     }

              }

              catch (IOException iox) {

                     String error = "General IOException for remote " +
remoteFile + "local : " + localFile + ";reason:" + iox.getMessage();

                     LOG.error(error);

                     throw new Exception(error);

              }

              finally {

                     if (output != null) {

                           try {

                                  output.close();

                           }

                           catch (Exception e) {}

                     }

              }

       }

       

       /**

        * Delete a remote file

        * @param remoteFile

        * @throws Exception

        */

       public void delete(String remoteFile) throws Exception {

              try {

                     client.deleteFile(remoteFile);

              }

              catch (IOException e) {

                     String error = "Unable to delete remote file " +
remoteFile + "reason:" + e.getMessage();

                     LOG.error(error);

                     throw new Exception(error);

              }

              

       }

       

       public Vector list() throws IOException {

              return list(this.remoteDir);

       }

 

 

       public Vector list(String pwd) throws IOException {

              try {

                     FTPFile[] files = client.listFiles();

                     Vector result = new Vector();

                     for (int i = 0 ; i < files.length ; i++) {

                           Date date =
files[i].getTimestamp().getTime();

                           String fileName = files[i].getName();

                           long size = files[i].getSize();

                           result.add(date + "\t" + "\"" + fileName +
"\"\t\t" + files[i].getSize());

                     }

                     return result;

              }

              finally {

              }

       }

       

       public void close() throws IOException {

              this.client.disconnect();

       }

       

}

 

Thanks a lot,

 

Luc

Luc De Kesel
Java Architect

Ariane-IT/Transiciel

Hoogkamerstraat 339A
B-9140 Temse
Tel  +32-(0)3-710.27.11
Fax +32-(0)3-710.27.10 

 

 


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