• Post Reply Bookmark Topic Watch Topic
  • New Topic

FTP between SUN and Linux servers  RSS feed

 
Melissa Pellegrin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
}
 
Melissa Pellegrin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My apologies - you will need to uncomment the following line:
// FTPFile [] ftpList = ftp.listFiles ("/shipconfirm/*.trg");
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the documentation, listFiles() can return null if there are no matching files. Therefore, your code is wrong because it uses ftpList.length without checking first if ftpList is null. The fact that it works on some platforms is irrelevant.
 
Melissa Pellegrin
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!