我做了一个小应用程序,应该将文件上传到FTP服务器。问题是我用方法
使用了被动模式enterLocalPassiveMode()
最近我被告知在FTP服务器上不允许被动模式,所以我应该让我的应用程序在主动模式下工作。我想通过简单地将方法更改为
就无法完成enterLocalActiveMode()
我应该在应用程序中更改什么以确保它在活动模式下工作。
这是一个与服务器建立连接的代码片段:
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());
}
}
对我必须改变的一些指导?
答案 0 :(得分:3)
这是旧的,但我偶然发现它试图自己解决这个问题。
在调用connect()之后和登录()之前,你必须调用enterLocalPassiveMode()。
请参阅下面的示例,该示例在本地被动模式下初始化FTPClient,列出给定目录的文件,然后关闭连接。
private static FTPClient client;
public static void main(String [] args) {
//initialzie 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;
}
答案 1 :(得分:0)
不确定这是否是您正在使用的FTPClient,但似乎确实存在enterLocalActiveMode。
答案 2 :(得分:0)
FTPClient
文档说明了
public boolean enterRemoteActiveMode(InetAddress host,int port)
throws IOException
将当前数据连接模式设置为ACTIVE_REMOTE_DATA_CONNECTION。仅将此方法用于服务器到服务器的数据传输。此方法向服务器发出PORT命令,指示应将数据传输连接到的其他服务器和端口。您必须在每次服务器到服务器传输尝试之前调用此方法。 FTPClient不会自动继续发出PORT命令。如果您希望返回正常的数据连接模式,还必须记得调用enterLocalActiveMode()。
希望这有帮助!