Win a copy of Terraform in Action this week in the Cloud forum!

Melissa Pellegrin

Greenhorn
+ Follow
since Oct 28, 2003
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Melissa Pellegrin

Thanks for the info! After posting this, I started checking if the list returned was null. I plan to implement this in my other methods.
My apologies - you will need to uncomment the following line:
// FTPFile [] ftpList = ftp.listFiles ("/shipconfirm/*.trg");
I have been working on getting a successful ftp download of files from a Linux system to my SUN system and I am having all sorts of headaches. This should be a pretty straight-forward process, so maybe someone out here can give me some pointers.
I am using Jakarta's commons-net-1.0.0 API jar. I am able to connect to the Linux server with no problems, then change the working directory with no problems. Now, here comes the problem - when I attempt to get a list of files (I have to grab a list of files that end with .trg), I use the following line of code:
FTPFile [] ftpList = ftp.listFiles ("/shipconfirm/*.trg");
If the files exist on the Linux system, I do not have a problem. However, if no files exist, I get a NullPointer exception. I do not incur this problem on other systems. I really feel that this is related to the FTP software than on the API. However, I would love to get input from anyone here.
Here is my code that I am using to test:
import org.apache.commons.net.ftp.*;
import java.io.*;
import java.lang.*;
public class ftpListFiles
{
public static void main (String [] args)
{
FTPClient ftp = new FTPClient ();
String remote = "/shipconfirm";
try
{
int reply;
ftp.connect ("mysystem.com");
System.out.print (ftp.getReplyString ());
reply = ftp.getReplyCode ();
if (!FTPReply.isPositiveCompletion (reply))
{
ftp.disconnect ();
System.err.println ("FTP Server refused connection.");
System.exit (1);
}
if (!ftp.login ("mp2114", "passwd"))
{
ftp.disconnect ();
System.err.println ("Unable to log into account!");
System.exit (1);
}
if (!ftp.setFileType (FTP.ASCII_FILE_TYPE))
{
ftp.disconnect ();
System.err.println ("Unable to log into account!");
System.exit (1);
}
ftp.enterLocalActiveMode ();
if (!ftp.changeWorkingDirectory (remote))
{
System.err.println ("Unable to change to " + remote + " on remote system
.");
System.exit (1);
}
String dirType = remote + "/" + "*.xml";
// FTPFile [] ftpList = ftp.listFiles ("/shipconfirm/*.trg");
System.out.println ("Getting list of files.");
System.out.println ("number of files: " + ftpList.length);
for (int count = 0; count < ftpList.length; count++)
{
System.out.println (ftpList [count].getName ());
}
ftp.logout ();
}
catch (IOException e)
{
if (ftp.isConnected ())
{
try
{
ftp.disconnect ();
}
catch (IOException f)
{
//do nothing
}
}
System.err.println ("Could not connect to server.");
e.printStackTrace ();
System.exit (1);
}
}
}
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.