I made a small application that should upload files to an FTP server. The thing is that I used passive mode with the method
enterLocalPassiveMode()
Recently I was told that no passive mode is allowed on the FTP server, so I should make my application work in active mode. I suppose it couldn't be done by simply changing the method to
enterLocalActiveMode()
What else should I change in the application to ensure it's working in active mode.
Here's a code snippet which makes the connection to the server:
public void connect() throws FTPException {
try {
ftpClient.connect(server, port);
replyCode = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(replyCode)) {
printText("FTP server refused connection.");
throw new FTPException("FTP server refused connection.");
}
boolean logged = ftpClient.login(user, pass);
if (!logged) {
ftpClient.disconnect();
printText("Could not login to the server.");
throw new FTPException("Could not login to the server.");
}
ftpClient.enterLocalPassiveMode();
} catch (IOException ex) {
printText("I/O errortest: " + ex.getMessage());
throw new FTPException("I/O error: " + ex.getMessage());
}
}
Some guidance to what I have to change?
This allows you to have some connections set to Active Mode and some set to Passive Mode. By default, Passive Mode is enabled when creating new connections. To enable Active Mode, select Session:Configure:Edit:Miscellaneous from the MenuBar. Un-check the Passive Mode checkbox to enable Active Mode.
This is old, but I stumbled onto it trying to solve the issue myself.
You have to call enterLocalPassiveMode()
after calling connect()
and before calling login()
.
See my example below which initialises the FTPClient
in local passive mode, lists files for a given directory, then closes the connections.
private static FTPClient client;
public static void main(String [] args) {
//initialise the client
initPassiveClient();
//do stuff
FTPFile [] files = listFiles("./");
if( files != null ) {
logger.info("Listing Files:");
for( FTPFile f : files) {
logger.info(f.getName());
}
}
//close the client
close();
}
/**
* getPassiveClient retrive a FTPClient object that's set to local passive mode
*
* @return FTPClient
*/
public static FTPClient initPassiveClient() {
if( client == null ) {
logger.info("Getting passive FTP client");
client = new FTPClient();
try {
client.connect(server);
// After connection attempt, you should check the reply code to verify
// success.
int reply = client.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)) {
client.disconnect();
logger.error("FTP server refused connection.");
System.exit(0);
}
//after connecting to the server set the local passive mode
client.enterLocalPassiveMode();
//send username and password to login to the server
if( !client.login(user, pass) ) {
logger.error("Could not login to FTP Server");
System.exit(0);
}
} catch (SocketException e) {
String message = "Could not form socket";
logger.error(message+"\n", e);
System.exit(0);
} catch (IOException e) {
String message = "Could not connect";
logger.error(message+"\n", e);
System.exit(0);
}
}
return client;
}
public static void close() {
if( client == null ) {
logger.error("Nothing to close, the FTPClient wasn't initialized");
return;
}
//be polite and logout & close the connection before the application finishes
try {
client.logout();
client.disconnect();
} catch (IOException e) {
String message = "Could not logout";
logger.error(message+"\n", e);
}
}
/**
* listFiles uses the FTPClient to retrieve files in the specified directory
*
* @return array of FTPFile objects
*/
private static FTPFile[] listFiles(String dir) {
if( client == null ) {
logger.error("First initialize the FTPClient by calling 'initFTPPassiveClient()'");
return null;
}
try {
logger.debug("Getting file listing for current director");
FTPFile[] files = client.listFiles(dir);
return files;
} catch (IOException e) {
String message = "";
logger.error(message+"\n", e);
}
return null;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With