pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

編寫 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(使用線程逐塊處理文件)
主站蜘蛛池模板: 超声波流量计_流量标准装置生产厂家 _河南盛天精密测控 | 电池挤压试验机-自行车喷淋-车辆碾压试验装置-深圳德迈盛测控设备有限公司 | 钢格栅板_钢格板网_格栅板-做专业的热镀锌钢格栅板厂家-安平县迎瑞丝网制造有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 超声骨密度仪,双能X射线骨密度仪【起草单位】,骨密度检测仪厂家 - 品源医疗(江苏)有限公司 | 多米诺-多米诺世界纪录团队-多米诺世界-多米诺团队培训-多米诺公关活动-多米诺创意广告-多米诺大型表演-多米诺专业赛事 | 网站制作优化_网站SEO推广解决方案-无锡首宸信息科技公司 | 美名宝起名网-在线宝宝、公司、起名平台 | 智能楼宇-楼宇自控系统-楼宇智能化-楼宇自动化-三水智能化 | 专注提供国外机电设备及配件-工业控制领域一站式服务商-深圳市华联欧国际贸易有限公司 | 广州物流公司_广州货运公司_广州回程车运输 - 万信物流 | 山东臭氧发生器,臭氧发生器厂家-山东瑞华环保设备 | 锂电叉车,电动叉车_厂家-山东博峻智能科技有限公司 | 动物解剖台-成蚊接触筒-标本工具箱-负压实验台-北京哲成科技有限公司 | 宁波普瑞思邻苯二甲酸盐检测仪,ROHS2.0检测设备,ROHS2.0测试仪厂家 | 商秀—企业短视频代运营_抖音企业号托管| 飞象网 - 通信人每天必上的网站| 温州富欧金属封头-不锈钢封头厂家| 农业四情_农业气象站_田间小型气象站_智慧农业气象站-山东风途物联网 | 120kv/2mA直流高压发生器-60kv/2mA-30kva/50kv工频耐压试验装置-旭明电工 | 小型铜米机-干式铜米机-杂线全自动铜米机-河南鑫世昌机械制造有限公司 | 考试试题_试卷及答案_诗词单词成语 - 优易学 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 艾默生变频器,艾默生ct,变频器,ct驱动器,广州艾默生变频器,供水专用变频器,风机变频器,电梯变频器,艾默生变频器代理-广州市盟雄贸易有限公司官方网站-艾默生变频器应用解决方案服务商 | 雷达液位计_超声波风速风向仪_雨量传感器_辐射传感器-山东风途物联网 | 两头忙,井下装载机,伸缩臂装载机,30装载机/铲车,50装载机/铲车厂家_价格-莱州巨浪机械有限公司 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 法钢特种钢材(上海)有限公司 - 耐磨钢板、高强度钢板销售加工 阀门智能定位器_电液动执行器_气动执行机构-赫尔法流体技术(北京)有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | 管理会计网-PCMA初级管理会计,中级管理会计考试网站 | 高铝轻质保温砖_刚玉莫来石砖厂家_轻质耐火砖价格 | 连栋温室大棚建造厂家-智能玻璃温室-薄膜温室_青州市亿诚农业科技 | 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | 江苏全风,高压风机,全风环保风机,全风环形高压风机,防爆高压风机厂家-江苏全风环保科技有限公司(官网) | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 河南中整光饰机械有限公司-抛光机,去毛刺抛光机,精密镜面抛光机,全自动抛光机械设备 | 高温链条油|高温润滑脂|轴承润滑脂|机器人保养用油|干膜润滑剂-东莞卓越化学 | 苗木价格-苗木批发-沭阳苗木基地-沭阳花木-长之鸿园林苗木场 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 |