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

編寫(xiě) Java FTP 服務(wù)器

Writing a Java FTP server(編寫(xiě) Java FTP 服務(wù)器)
本文介紹了編寫(xiě) Java FTP 服務(wù)器的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試編寫(xiě)一個(gè)代碼,在我的獨(dú)立設(shè)備上打開(kāi)一個(gè) FTP 服務(wù)器,這樣我就可以將文件從它復(fù)制到另一臺(tái)計(jì)算機(jī)上的客戶端,反之亦然,但我對(duì)服務(wù)器端編程非常陌生,不會(huì)了解如何.

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 但對(duì)它的使用有點(diǎn)困惑,我正在尋找有關(guān)如何使用它的基本步驟.可能是這樣的:

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. 執(zhí)行連接命令
  2. 登錄
  3. 做一些事情......

推薦答案

讓我為你寫(xiě)一個(gè)基本的例子,使用非常有用的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
    }

請(qǐng)注意,在服務(wù)器端,您不必手動(dòng)處理連接、登錄等:Ftplet 會(huì)為您完成.

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

但是,您可以在匿名內(nèi)部 Ftplet 類的重寫(xiě)方法中添加自定義的預(yù)處理[或后]處理(當(dāng)您使用 new Ftplet(){ ... } 實(shí)例化它時(shí).

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(){ ... }.

這篇關(guān)于編寫(xiě) Java FTP 服務(wù)器的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環(huán)繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動(dòng)生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數(shù)據(jù)庫(kù))
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對(duì)象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 毛片国产| 国产国产精品久久久久 | av中文字幕网 | 国产午夜精品久久久久免费视高清 | 日韩av在线播 | 精品久久久久一区二区国产 | 欧美伊人| 欧美精品久久一区 | 精品亚洲一区二区三区四区五区 | 91国语清晰打电话对白 | 中文字幕亚洲欧美 | 超碰美女在线 | 最新中文字幕第一页视频 | 国产一区二区精品在线观看 | 日韩精品一区二区三区四区 | 国产精品美女一区二区三区 | 国产日韩一区二区三免费 | 日韩欧美在线观看视频 | 青青草原精品99久久精品66 | 91久久国产综合久久 | 日韩天堂av | 色五月激情五月 | 国产精品毛片一区二区三区 | 91精品国产91久久久久久最新 | 精品一区二区久久久久久久网精 | 精品国产一区二区三区四区在线 | 国产乱码精品一品二品 | 日本三级网址 | 成人在线视频一区 | 又黄又色 | 在线播放一区二区三区 | av一区二区三区在线观看 | 国产高清在线观看 | 欧美四虎 | 国产精品美女www爽爽爽视频 | 99久久99 | a免费视频 | 亚洲精品国产成人 | 伊人狠狠干| 久久久精品视频免费看 | 亚洲综合精品 |