gpt4 book ai didi

java - 无法连接到 FTPS 服务器

转载 作者:太空宇宙 更新时间:2023-11-04 07:49:11 30 4
gpt4 key购买 nike

我能够通过 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();
}
}
}

}

最佳答案

你可以通过

1 隐式 SSL,首先需要协商 SSL/TLS ,然后建立连接

2 显式 SSL,如前所述 here

另外,你可以使用 apache-commons FTPSClient 吗??

关于java - 无法连接到 FTPS 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14769629/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com