栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

编写Java FTP服务器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

编写Java FTP服务器

让我使用非常有用的 Apache FtpServer 为您编写一个基本示例:

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 listuserManagerFactory.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    }

请注意,在服务器端,您不必手动处理连接,登录等:Ftplet会为您完成此操作。

不过,您可以在匿名内部Ftplet类的覆盖方法中添加自定义的预处理过程(使用实例化时)

new Ftplet(){ ... }



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454793.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号