首先到apache官方下载 ftp服务器 下载地址是
http://mina.apache.org/ftpserver/
解压后 apache-ftpserver-1.0.5/common/lib 下边的jar既是启动ftp的主要jar
一半只需要 如下几个
ftplet-api-1.0.5.jar
ftpserver-core-1.0.5.jar
log4j-1.2.14.jar
mina-core-2.0.0-RC1.jar
slf4j-api-1.5.2.jar
slf4j-log4j12-1.5.2.jar
新建工程 ftpserver 添加类 startFtpServer 代码如下
在ftpserver工程添加文件夹 conf
拷贝 apache-ftpserver-1.0.5/res/conf 下面文件:
users.properties 到 conf目录下
在users.propertis中添加
ftpserver.user.a.userpassword=a
ftpserver.user.a.homedirectory=c://myftp
ftpserver.user.a.enableflag=true
ftpserver.user.a.writepermission=true
ftpserver.user.a.maxloginnumber=0
ftpserver.user.a.maxloginperip=0
ftpserver.user.a.idletime=0
ftpserver.user.a.uploadrate=0
ftpserver.user.a.downloadrate=0
就相当于添加了一个用户a 密码是a
ftpserver中默认 密码被md5加密了
代码中 setPasswordEncryptor 重写了加密方法
import java.io.File;
import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PasswordEncryptor;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
public class startFtpServer {
/**
* @param args
* @throws FtpException
*/
public static void main(String[] args) throws FtpException {
// TODO Auto-generated method stub
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(21);
// define SSL configuration
/**
* 使用ssl会导致客户端无法连接 SslConfigurationFactory ssl = new
* SslConfigurationFactory(); ssl.setKeystoreFile(new
* File(System.getProperty("user.dir")+"/conf/ftpserver.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.setFile(new File(System.getProperty("user.dir")
+ "/conf/users.properties"));
userManagerFactory.setPasswordEncryptor(new PasswordEncryptor() {
public String encrypt(String pwd) {
// TODO Auto-generated method stub
return null;
}
//storedPassword 配置文件中配置的密码 passwordToCheck 是用户输入的密码
public boolean matches(java.lang.String passwordToCheck,
java.lang.String storedPassword) {
if (passwordToCheck.equals(storedPassword))
return true;
return false;
}
});
serverFactory.setUserManager(userManagerFactory.createUserManager());
// start the server
FtpServer server = serverFactory.createServer();
server.start();
}
}
下载个客户端 登陆试一下