久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

編寫 Java FTP 服務器

Writing a Java FTP server(編寫 Java FTP 服務器)
本文介紹了編寫 Java FTP 服務器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試編寫一個代碼,在我的獨立設備上打開一個 FTP 服務器,這樣我就可以將文件從它復制到另一臺計算機上的客戶端,反之亦然,但我對服務器端編程非常陌生,不會了解如何.

I am trying to write a code that opens an FTP server on my stand-alone so I could copy file from it to a client in another computer and the opposite, but I am very new to server side programming and don't understand how.

我得到了 Apache FtpServer 但對它的使用有點困惑,我正在尋找有關如何使用它的基本步驟.可能是這樣的:

I got the Apache FtpServer but got a little confused with it's use, and am looking for the basic steps of how to use it. Maybe something like:

  1. 執行連接命令
  2. 登錄
  3. 做一些事情......

推薦答案

讓我為你寫一個基本的例子,使用非常有用的Apache FtpServer:

Let me write a basic example for you, using the very useful 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 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
    }

請注意,在服務器端,您不必手動處理連接、登錄等:Ftplet 會為您完成.

Note that, server-side, you don't have to deal manually with connects, logins, etc: the Ftplet does that for you.

但是,您可以在匿名內部 Ftplet 類的重寫方法中添加自定義的預處理[或后]處理(當您使用 new Ftplet(){ ... } 實例化它時.

You can, however, add your custom pre[or post]-processing inside the overridden methods of your anonymous inner Ftplet class (when you instantiate it with new Ftplet(){ ... }.

這篇關于編寫 Java FTP 服務器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 999精品视频在线观看 | 久久一区视频 | 天天干人人 | 免费成人高清在线视频 | 国产精品久久久爽爽爽麻豆色哟哟 | 日本免费黄色 | 国产午夜精品一区二区三区四区 | 日韩中文字幕在线播放 | 午夜免费在线电影 | 国产三级一区二区三区 | 亚洲 欧美 另类 综合 偷拍 | 欧美成人精品二区三区99精品 | 日韩av免费看| 国产精品久久久久久久久久久免费看 | 国产精品激情小视频 | 欧美成人性生活 | 国产精品永久在线观看 | 亚洲成人高清 | 国产午夜精品一区二区三区四区 | 日本a∨视频| 精品一区二区三区在线播放 | 亚洲欧美一区二区三区国产精品 | 亚洲免费观看视频网站 | 特级黄一级播放 | 夜夜夜夜草 | 国产免费一区二区 | 狠狠色香婷婷久久亚洲精品 | 亚洲精品视频一区 | 草草在线观看 | 综合国产| 免费精品| 亚州综合在线 | 日韩免 | 精品真实国产乱文在线 | 国内精品久久久久 | 日韩在线免费视频 | 老牛嫩草一区二区三区av | 一区二区免费视频 | 日本三级网址 | 精品亚洲一区二区 | 在线视频一区二区三区 |