我想知道我与 FTP 服务器连接和断开连接的方式是否正确,或者是否可以更好。
我正在使用sun.net.ftp.FtpClient
.
import sun.net.ftp.FtpClient;
public class FTPUtility
{
public static FtpClient connect(FTPConfig ftpConfig,WebTextArea statusTextArea)
{
String hostname = ftpConfig.getFtpServer();
String username = ftpConfig.getUsername();
String password = ftpConfig.getPassword();
String portnumb = ftpConfig.getPort();
try
{
FtpClient client = new FtpClient(hostname);
statusTextArea.append("Connecting to " + hostname + " as " + username + " on port:" + portnumb );
client.login(username, password);
client.binary();
statusTextArea.append("Connected to " + hostname + " as " + username + " on port:" + portnumb );
return client;
}
catch (Exception e)
{
statusTextArea.append("Failed to connect to " + hostname + " as " + username + "\n".concat(e.getMessage()) );
return null;
}
}
public static boolean disConnect(FtpClient client, WebTextArea statusTextArea)
{
boolean success = false;
if (client != null)
{
try
{
statusTextArea.append("Disconnecting from server...");
client.closeServer();
statusTextArea.append("Disconnected from server." );
success = true;
}
catch (Exception e)
{
statusTextArea.append("Failed to disconnect from server. " + "\n".concat(e.getMessage()));
}
}
return success;
}
}