编写javaftp服务器
我正试图编写一个代码,在我的单机上打开一个FTP服务器,这样我就可以将文件从它复制到另一台计算机上的客户机上,反之亦然,但我对服务器端编程非常陌生,不知道如何进行 我得到了,但对它的用途有点困惑,我正在寻找如何使用它的基本步骤。可能是这样的:编写javaftp服务器,java,ftp,ftp-server,Java,Ftp,Ftp Server,我正试图编写一个代码,在我的单机上打开一个FTP服务器,这样我就可以将文件从它复制到另一台计算机上的客户机上,反之亦然,但我对服务器端编程非常陌生,不知道如何进行 我得到了,但对它的用途有点困惑,我正在寻找如何使用它的基本步骤。可能是这样的: do connect命令 登录 做一些事情 让我使用非常有用的ApacheFTPServer为您编写一个基本示例: FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFacto
让我使用非常有用的ApacheFTPServer为您编写一个基本示例:
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
factory.setPort(1234);// set the port of the listener (choose your desired port, not 1234)
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File("/home/blablah/myusers.properties"));//choose any. We're telling the FTP-server where to read its user list
userManagerFactory.setPasswordEncryptor(new PasswordEncryptor()
{//We store clear-text passwords in this example
@Override
public String encrypt(String password) {
return password;
}
@Override
public boolean matches(String passwordToCheck, String storedPassword) {
return passwordToCheck.equals(storedPassword);
}
});
//Let's add a user, since our myusers.properties file is empty on our first test run
BaseUser user = new BaseUser();
user.setName("test");
user.setPassword("test");
user.setHomeDirectory("/home/blablah");
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);
UserManager um = userManagerFactory.createUserManager();
try
{
um.save(user);//Save the user to the user list on the filesystem
}
catch (FtpException e1)
{
//Deal with exception as you need
}
serverFactory.setUserManager(um);
Map<String, Ftplet> m = new HashMap<String, Ftplet>();
m.put("miaFtplet", new Ftplet()
{
@Override
public void init(FtpletContext ftpletContext) throws FtpException {
//System.out.println("init");
//System.out.println("Thread #" + Thread.currentThread().getId());
}
@Override
public void destroy() {
//System.out.println("destroy");
//System.out.println("Thread #" + Thread.currentThread().getId());
}
@Override
public FtpletResult beforeCommand(FtpSession session, FtpRequest request) throws FtpException, IOException
{
//System.out.println("beforeCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine());
//System.out.println("Thread #" + Thread.currentThread().getId());
//do something
return FtpletResult.DEFAULT;//...or return accordingly
}
@Override
public FtpletResult afterCommand(FtpSession session, FtpRequest request, FtpReply reply) throws FtpException, IOException
{
//System.out.println("afterCommand " + session.getUserArgument() + " : " + session.toString() + " | " + request.getArgument() + " : " + request.getCommand() + " : " + request.getRequestLine() + " | " + reply.getMessage() + " : " + reply.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());
//do something
return FtpletResult.DEFAULT;//...or return accordingly
}
@Override
public FtpletResult onConnect(FtpSession session) throws FtpException, IOException
{
//System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());
//do something
return FtpletResult.DEFAULT;//...or return accordingly
}
@Override
public FtpletResult onDisconnect(FtpSession session) throws FtpException, IOException
{
//System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString());
//System.out.println("Thread #" + Thread.currentThread().getId());
//do something
return FtpletResult.DEFAULT;//...or return accordingly
}
});
serverFactory.setFtplets(m);
//Map<String, Ftplet> mappa = serverFactory.getFtplets();
//System.out.println(mappa.size());
//System.out.println("Thread #" + Thread.currentThread().getId());
//System.out.println(mappa.toString());
FtpServer server = serverFactory.createServer();
try
{
server.start();//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
}
catch (FtpException ex)
{
//Deal with exception as you need
}
FtpServerFactory serverFactory=新的FtpServerFactory();
ListenerFactory=新建ListenerFactory();
工厂设置端口(1234);//设置侦听器的端口(选择所需的端口,而不是1234)
serverFactory.addListener(“默认”,factory.createListener());
PropertiesUserManagerFactory userManagerFactory=新属性userManagerFactory();
setFile(新文件(“/home/blablah/myusers.properties”)//随便选一个。我们正在告诉FTP服务器在哪里读取其用户列表
userManagerFactory.setPasswordEncryptor(新的PasswordEncryptor()
{//在本例中,我们存储明文密码
@凌驾
公共字符串加密(字符串密码){
返回密码;
}
@凌驾
公共布尔匹配(字符串密码检查、字符串存储密码){
返回passwordToCheck.equals(storedPassword);
}
});
//让我们添加一个用户,因为我们的myusers.properties文件在第一次测试运行时是空的
BaseUser=新的BaseUser();
user.setName(“测试”);
user.setPassword(“测试”);
user.setHomeDirectory(“/home/blablah”);
列表权限=新建ArrayList();
authorities.add(新的WritePermission());
用户设置权限(authorities);
UserManager um=userManagerFactory.createUserManager();
尝试
{
um.save(user);//将用户保存到文件系统上的用户列表中
}
捕获(FTPEException e1)
{
//根据需要处理异常
}
setUserManager(um);
Map m=新的HashMap();
m、 put(“miaFtplet”,新的Ftplet()
{
@凌驾
public void init(FtpletContext FtpletContext)抛出FtpException{
//System.out.println(“init”);
//System.out.println(“Thread#”+Thread.currentThread().getId());
}
@凌驾
公共空间销毁(){
//系统输出打印(“销毁”);
//System.out.println(“Thread#”+Thread.currentThread().getId());
}
@凌驾
公共FtpletResult before命令(FtpSession会话、FtpRequest请求)引发ftpeexception、IOException
{
//System.out.println(“beforeCommand”+session.getUserArgument()+”:“+session.toString()+”;“+request.getArgument()+”:“+request.getCommand()+”:“+request.getRequestLine());
//System.out.println(“Thread#”+Thread.currentThread().getId());
//做点什么
返回FtpletResult.DEFAULT;/…或相应地返回
}
@凌驾
公共FtpletResult afterCommand(FtpSession会话、FtpRequest请求、FtpReply回复)抛出ftpeexception、IOException
{
//System.out.println(“afterCommand”+session.getUserArgument()+”:“+session.toString()+”;“+request.getArgument()+”:“+request.getCommand()+”:“+request.getRequestLine()+”;“+reply.getMessage()+”:“+reply.toString());
//System.out.println(“Thread#”+Thread.currentThread().getId());
//做点什么
返回FtpletResult.DEFAULT;/…或相应地返回
}
@凌驾
公共FtpletResult onConnect(FtpSession会话)引发FTPEException、IOException
{
//System.out.println(“onConnect”+session.getUserArgument()+”:“+session.toString());
//System.out.println(“Thread#”+Thread.currentThread().getId());
//做点什么
返回FtpletResult.DEFAULT;/…或相应地返回
}
@凌驾
公共FtpletResult onDisconnect(FtpSession会话)抛出FTPEException、IOException
{
//System.out.println(“onDisconnect”+会话.getUserArgument()+”:“+会话.toString());
//System.out.println(“Thread#”+Thread.currentThread().getId());
//做点什么
返回FtpletResult.DEFAULT;/…或相应地返回
}
});
serverFactory.setFtplets(m);
//Map mappa=serverFactory.getFtplets();
//System.out.println(mappa.size());
//System.out.println(“Thread#”+Thread.currentThread().getId());
//System.out.println(mappa.toString());
FtpServer server=serverFactory.createServer();
尝试
{
server.start();//您的FTP服务器开始使用前面设置的配置选项侦听传入的FTP连接
}
捕获(FTP例外)
{
//根据需要处理异常
}
注意,在服务器端,您不必手动处理连接、登录等:Ftplet会为您完成这项工作
但是,您可以在匿名内部Ftplet类的重写方法中添加自定义的pre[或post]处理(当您使用
new Ftplet(){…}
实例化它时,FtpServerFactory
来自何处?它来自Apache FtpServer核心(OP在他的问题中说,他得到了ApacheFTPServer API,但不知道从哪里开始).无论如何,我忘了在我的回答中明确说明我使用的是Apache FtpServer;我马上就去。谢谢你的评论。我将编写服务器方法并稍加处理……如果我遇到什么问题,我能在几天内为你发布吗?根据StackOverflow的实践,你应该发布一个单独的问题,非常具体我很感谢你发现的新问题。不过我会留意你帐户上的问题,如果我能回答的话