• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

FTP problem with commons.net.ftp.FTPClient

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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? Any suggestions? Thanks in advance.
Here is my code:
private void ftpFileTransfer()
throws DwhGenericException
{
try
{
// Make ftp connection to the ndrftp server.
FTPClient ftpClient = new FTPClient();
ftpClient.connect("99.999.9.999");
boolean loginSuccess = ftpClient.login(
"user",
"password");
String rootDir = ftpClient.printWorkingDirectory();
boolean cwdSuccess = ftpClient.changeWorkingDirectory(ftpSrcPath);
String workingDir = ftpClient.printWorkingDirectory();
FTPFile[] ftpFiles = ftpClient.listFiles();
for (int i = 0; i < ftpFiles.length; i++)
{
String fileName = ftpFiles[i].getName();
if (fileName.startsWith("x_"))
{
continue;
}
File newFile = new File(targetPath + "\\" + fileName);
FileOutputStream fos = new FileOutputStream(newFile);
ftpClient.retrieveFile(
fileName,
fos);
fos.close();
ftpClient.rename(
fileName,
"x_" + fileName);
}
ftpClient.logout();
ftpClient.disconnect();
ftpClient = null;
}
catch (Exception e)
{
JobLog.storeError(
"Error - ftpFileTransfer()",
e);
throw new DwhGenericException();
}
}
 
Jerry Kreps
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic