wood burning stoves*
The moose likes Sockets and Internet Protocols and the fly likes FTP problem in commons.net.ftp.FTPClient Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "FTP problem in commons.net.ftp.FTPClient" Watch "FTP problem in commons.net.ftp.FTPClient" New topic

FTP problem in commons.net.ftp.FTPClient

Jerry Kreps

Joined: Jun 16, 2003
Posts: 12
Hi, Everybody!
Listed below is a method I have written that is designed to connect to my remote FTP server, change the working directory on the FTP server to to a certain subdirectory (ftpSrcPath) that contains files that I want to FTP to my local directory (targetPath). I want to get an array of FTPFiles and FTP only those whose names do not start with "x_" (previously sent files). The variables loginSuccess, rootDir, cwdSuccess, and workingDir are there only so that I can see their values when I am debugging in my IDE.
When I run it through my debugger, I make the connection successfully (loginSuccess = true), my initial working directory on the remote FTP server is the root directory (rootDir = "/"), then I change the working directory successfully (cwdSuccess = true) and I can see the new working directory on the remote FTP server (workingDir = "/srcdir"). But when I try to get the array of FTPFile objects, the FTPClient.listFiles() method returns null.
What am I doing wrong? My initial suspicion is that the DefaultFTPFileListParser is not handling the names of the files in my source directory and that I need to make my own FTPFileListParser. But how? I've been able to find very little documentation on the web other than the API JavaDocs, and they don't say much about it.
Thanks in advance for your help!
Here's the code:
private void ftpFileTransfer()
throws DwhGenericException
FTPClient ftpClient = new FTPClient();
boolean loginSuccess = ftpClient.login(
String rootDir = ftpClient.printWorkingDirectory();
boolean cwdSuccess = ftpClient.changeWorkingDirectory(ftpSrcPath);
String workingDir = ftpClient.printWorkingDirectory();
// **** The following operation returns null. ****
FTPFile[] ftpFiles = ftpClient.listFiles();
for (int i = 0; i < ftpFiles.length; i++)
String fileName = ftpFiles[i].getName();
if (fileName.startsWith("x_"))
File newFile = new File(targetPath + "\\" + fileName);
FileOutputStream fos = new FileOutputStream (newFile);
ftpClient.rename(fileName, "x_" + fileName);
ftpClient = null;
catch (Exception e)
"Error - ftpFileTransfer()",
throw new DwhGenericException();
Jerry Kreps

Joined: Jun 16, 2003
Posts: 12
As a PS to the above message:
I've put this message on the "Other Open Source APIs" forum, since I thought it might apply to that too. If this violates forum rules, I apologize and I will be more careful where to put my messages in the future.
Jerry Kreps

Joined: Jun 16, 2003
Posts: 12
Well, I found my own work-around that gets the job done.
My code in the original message used the FTPClient.listFiles() method to retrieve the array of FTPFile objects and then looped through the array using the getName() method of each object to pass the file name to the FTPClient.retrieveFile() method. But the trouble was that the FTPClient.listFiles() method always returned null even though the working directory clearly contained files that I wanted to download.
So I decided to go through the back door.
I noticed that the FTPClient.listNames() method returned an array of string objects that were the names of all the files in the source directory that I wanted to download. So then just I looped through the string array and I passed each file name string to the FTPClient.retrieveFile() method and it worked like a charm.
The only question is: Why did the FTPClient.listFiles() method always return null when the FTPClient.listNames() method was able to identify the files I wanted?
Well, I hope this helps someone else who comes across the same problem. If any of you know that answer to my puzzle, please fill me in. Thanks!
I LOVE JavaRanch!!!
Melissa Pellegrin

Joined: Oct 28, 2003
Posts: 4
I have been doing quite a bit of work with the FTPClient software. I am finding that different versions of the FTP software can result in different outcomes.
A majority of the time, when I use listFiles (), I would put the full path in the "()". For example, ftp.listFiles (/root/dir1/*.txt), and I would get back a list of all *.txt files.
Rick Herrick

Joined: Aug 25, 2004
Posts: 11
I just ran across this while looking for info on using the Commons FTP package. You may want to try setting your connection to passive. A lot of the issues that people have had getting info properly through the FTP connection have related to firewall issues, which can be (somewhat) circumvented by using a passive instead of active FTP connection.

Rick Herrick<br />C#/.NET, Java, Ruby, Agile as hell
subject: FTP problem in commons.net.ftp.FTPClient