FTP 経由で Apache FTP サーバー ( http://mina.apache.org/ftpserver-project/index.html ) に接続できます。私はFTPS接続を行うために同じ方法に従いました。しかし、クライアント側プログラムを実行すると、次のエラーが発生します。クライアント クラスの作成を手伝ってくれる人はいますか?
サーバーを起動するコードは次のとおりです。
public class FTPSServer {
public static void main(String[] args) {
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
// set the port of the listener
factory.setPort(2221);
// define SSL configuration
SslConfigurationFactory ssl = new SslConfigurationFactory();
ssl.setKeystoreFile(new File(
"src/ftp/resources/cert.jks"));
ssl.setKeystorePassword("password");
// set the SSL configuration for the listener
factory.setSslConfiguration(ssl.createSslConfiguration());
factory.setImplicitSsl(true);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
UserManager userManager = userManagerFactory.createUserManager();
BaseUser user = new BaseUser();
user.setName("test");
user.setPassword("test");
user.setHomeDirectory("D:\\FTP-TEST-UPLOADS");
List<Authority> auths = new ArrayList<Authority>();
Authority auth = new WritePermission();
auths.add(auth);
user.setAuthorities(auths);
try {
userManager.save(user);
} catch (FtpException e1) {
e1.printStackTrace();
}
serverFactory.setUserManager(userManagerFactory.createUserManager());
// start the server
FtpServer server = serverFactory.createServer();
try {
server.start();
System.out.println("FTPs Server Started");
} catch (FtpException e) {
e.printStackTrace();
}
}
}
および私のクライアントクラス:
class FtpClient {
private static String ftpServerAddress = "localhost";
private static int port = 2121;
private static String userName = "test";
private static String password = "test";
public static void main(String[] args) {
try {
uploadFile();
downloadFile();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void downloadFile() throws IOException {
FTPClient client = new FTPClient();
FileOutputStream fos = null;
boolean result;
try {
client.connect(ftpServerAddress, port);
result = client.login(userName, password);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
if (result == true) {
System.out.println("Successfully logged in!");
} else {
System.out.println("Login Fail!");
return;
}
// The remote filename to be downloaded.
String filename = "Technolabs Logo.PNG";
fos = new FileOutputStream(filename);
// Download file from FTP server
result = client.retrieveFile("receivedFiles/" + filename, fos);
if (result == true)
System.out.println("File was downloaded");
client.logout();
} catch (FTPConnectionClosedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
fos.close();
} catch (FTPConnectionClosedException e) {
System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private static void uploadFile() throws IOException {
FTPClient client = new FTPClient();
FileInputStream fis = null;
boolean result;
try {
client.connect(ftpServerAddress, port);
result = client.login(userName, password);
if (!FTPReply.isPositiveCompletion(client.getReplyCode())) {
client.disconnect();
System.err.println("FTP server refused connection.");
System.exit(1);
}
if (result == true)
System.out.println("Successfully logged in!");
else {
System.out.println("Login Fail!");
return;
}
client.setFileType(FTP.BINARY_FILE_TYPE);
client.enterLocalPassiveMode();
client.changeWorkingDirectory("/");
File file = new File("D:/myFile.PNG");
String testName = file.getName();
fis = new FileInputStream(file);
// Upload file to the ftp server
result = client.storeFile(testName, fis);
if (result == true) {
System.out.println("File is uploaded successfully");
} else {
System.out.println("File uploading failed");
}
client.logout();
} catch (FTPConnectionClosedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
client.disconnect();
fis.close();
} catch (FTPConnectionClosedException e) {
System.out.println(e);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}