Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

FTP Access via Socks5 Proxy Server

843790Dec 13 2007 — edited Dec 13 2007
I am trying to access a remote FTP site via Socks5 proxy server using apache.commons.net package.
The source is as follows which simply access the FTP Server and prints the list of folder names in the root directory.
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class SimpleFTPClient {
	public static void main(String arg[]) throws Exception {
		int reply;
		FTPClient ftpClient = new FTPClient();
		try {
			ftpClient.setReaderThread(false);
			ftpClient.setDefaultTimeout(300000);
			ftpClient.setDataTimeout(10800000);
			
			System.setProperty("socksProxyHost", "proxyServerName");
			System.setProperty("socksProxyPort", Integer.toString(1080));
			java.net.Authenticator.setDefault(new Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication("proxyServerUsrId", "password".toCharArray());
				}
			});
			ftpClient.connect("remoteFtpServer");
			reply = ftpClient.getReplyCode();
			if (!FTPReply.isPositiveCompletion(reply)) {
				ftpClient.disconnect();
				System.err.println("FTP server refused connection.");
				System.exit(1);
			}
			 
			ftpClient.enterLocalPassiveMode() ;   
			ftpClient.login("FTPUserId", "password");
			if (ftpClient.isConnected()) {
				
				 String[] folderNames = ftpClient.listNames();
				 for(int i=0;i<folderNames.length ;i++)
				 {
				 System.out.println(folderNames);
}
}
} catch (RuntimeException e) {
e.printStackTrace();
}
finally {
ftpClient.logout();
ftpClient.disconnect();
}
}
}
I am able to access and print the folder names in root directory from a Local FTP Filezilla  server with the above code.
But when I try to access the remote site via the proxy server I am getting the following error. 
java.lang.NullPointerException
at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:536)
at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:1959)
at org.apache.commons.net.ftp.FTPClient.listNames(FTPClient.java:2006)
at com.foursoft.visilog.net.SimpleFTPClient.main(SimpleFTPClient.java:34)
I think I am able to login to remote site. But while trying to do some data transfer it is failing to establish a connection.
Same remote FTP I am able to connect and transfer files using any FTP client tools like FileZilla.

Any suggestion in this regard will be greatly appreciated.

Thanks in Advance.

Edited by: VAS on Dec 12, 2007 10:54 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 10 2008
Added on Dec 13 2007
0 comments
1,983 views