我需要模拟“ FTPS”服务器作为单元测试的一部分。我通过使用模拟FTPS服务器收到“ javax.net.ssl.SSLException:502命令未实现:AUTH”。
我经历了http://mockftpserver.sourceforge.net/和How to create a "FTPS" Mock Server to unit test File Transfer in Java,但是没有找到作为FTPS服务器进行连接的方法。
import java.io.File;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPSClient;
import org.mockftpserver.fake.FakeFtpServer;
import org.mockftpserver.fake.UserAccount;
public class FtpsMockTest {
private FakeFtpServer fakeFtpServer;
public FtpsMockTest(int port, String userName, String password, File homeDir){
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(port);
fakeFtpServer.addUserAccount(new UserAccount(userName, password, homeDir.getAbsolutePath()));
}
public static FTPSClient createFtpsClient(String hostname, Integer port, String username, String password)
throws Exception {
FTPSClient ftpsClient = new FTPSClient("TLS", false);
ftpsClient.connect(hostname, port);
ftpsClient.enterLocalPassiveMode();
boolean loginStatus = ftpsClient.login(username, password);
if (!loginStatus) {
throw new Exception("FTPS client login failed.");
}
ftpsClient.execPBSZ(0);
ftpsClient.execPROT("P");
ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
return ftpsClient;
}
public static void main(String[] args) throws Exception {
FtpsMockTest test = null;
try {
test = new FtpsMockTest(990, "test", "test", new File("C:\\FuncTest"));
test.start();
FTPSClient ftpsClient = createFtpsClient("127.0.0.1", 990, "test", "test");
FTPFile[] listFiles = ftpsClient.listFiles("C:/Test");
System.out.println("*** total available files ***" + listFiles.length);
}finally {
test.stop();
}
}
public void start(){
fakeFtpServer.start();
System.out.println("**** FAKE FTPS Server Started ***");
}
public void stop(){
fakeFtpServer.stop();
System.out.println("**** FAKE FTPS Server Stopped ***");
}
}
我使用上面的代码连接FTPS服务器,但出现以下错误。您能否提供任何输入以解决以下错误。
Exception in thread "main" javax.net.ssl.SSLException: 502 Command not implemented: AUTH.
at org.apache.commons.net.ftp.FTPSClient.execAUTH(FTPSClient.java:214)
at org.apache.commons.net.ftp.FTPSClient._connectAction_(FTPSClient.java:196)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:164)
at org.apache.commons.net.SocketClient.connect(SocketClient.java:184)
at com.asos.mule.core.connector.ftps.functional.FtpsMockTest.createFtpsClient(FtpsMockTest.java:25)
at com.asos.mule.core.connector.ftps.functional.FtpsMockTest.main(FtpsMockTest.java:47)
最佳答案
克里斯(Chris)确认,MockFTPServer不支持FTPS。
参考链接-https://sourceforge.net/p/mockftpserver/discussion/748297/thread/d69457a91a/
关于java - 如何在Java中创建“FTPS”模拟服务器进行单元测试,出现错误-javax.net.ssl.SSLException:502未实现的命令:AUTH,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57359102/