apache ftpserver use

52 篇文章 0 订阅
39 篇文章 1 订阅

编译提示dependence

packagingOptions {
     exclude 'META-INF/DEPENDENCIES'
     exclude 'META-INF/LICENSE'
     exclude 'META-INF/LICENSE.txt'
     exclude 'META-INF/license.txt'
     exclude 'META-INF/NOTICE'
     exclude 'META-INF/NOTICE.txt'
     exclude 'META-INF/notice.txt'
     exclude 'META-INF/ASL2.0'
}

gradle导入

implementation 'org.apache.ftpserver:ftpserver-core:1.1.1'

使用代码

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val fileBasePath = Environment.getExternalStorageDirectory().absolutePath
        Log.i("MainActivity",fileBasePath)
        var propertisPath = "file:///android_asset/users.properties"
        start_server.setOnClickListener {
            val serverFactory = FtpServerFactory()
            val factory = ListenerFactory()
            factory.port = 1234// set the port of the listener (choose your desired port, not 1234)
            serverFactory.addListener("default", factory.createListener())
            val userManagerFactory = PropertiesUserManagerFactory()
            //userManagerFactory.file =
                   // File(propertisPath)//choose any. We're telling the FTP-server where to read it's user list
            userManagerFactory.passwordEncryptor =
                    object : PasswordEncryptor {//We store clear-text passwords in this example

                        override fun encrypt(password: String): String {
                            return password
                        }

                        override fun matches(passwordToCheck: String, storedPassword: String): Boolean {
                            return passwordToCheck == storedPassword
                        }
                    }
            //Let's add a user, since our myusers.properties files is empty on our first test run
            val user = BaseUser()
            user.name = "test"
            user.password = "test"
            user.homeDirectory = fileBasePath
            val authorities = ArrayList<Authority>()
            authorities.add(WritePermission())
            user.authorities = authorities
            val um = userManagerFactory.createUserManager()
            try {
                um.save(user)//Save the user to the user list on the filesystem
            } catch (e1: FtpException) {
                //Deal with exception as you need
            }

            serverFactory.userManager = um
            val m = HashMap<String, Ftplet>()
            m["miaFtplet"] = object : Ftplet {

                @Throws(FtpException::class)
                override fun init(ftpletContext: FtpletContext) {
                    System.out.println("init");
                    System.out.println("Thread #" + Thread.currentThread().getId());
                }

                override fun destroy() {
                    System.out.println("destroy");
                    System.out.println("Thread #" + Thread.currentThread().getId());
                }

                @Throws(FtpException::class, IOException::class)
                override fun beforeCommand(session: FtpSession, request: FtpRequest): FtpletResult {
                    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
                }

                @Throws(FtpException::class, IOException::class)
                override fun afterCommand(session: FtpSession, request: FtpRequest, reply: FtpReply): FtpletResult {
                    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
                }

                @Throws(FtpException::class, IOException::class)
                override fun onConnect(session: FtpSession): FtpletResult {
                    System.out.println("onConnect " + session.getUserArgument() + " : " + session.toString());System.out.println("Thread #" + Thread.currentThread().getId());

                    //do something
                    return FtpletResult.DEFAULT//...or return accordingly
                }

                @Throws(FtpException::class, IOException::class)
                override fun onDisconnect(session: FtpSession): FtpletResult {
                    System.out.println("onDisconnect " + session.getUserArgument() + " : " + session.toString());
                    System.out.println("Thread #" + Thread.currentThread().getId());

                    //do something
                    return FtpletResult.DEFAULT//...or return accordingly
                }
            }
            serverFactory.ftplets = m
            //Map<String, Ftplet> mappa = serverFactory.getFtplets();
            //System.out.println(mappa.size());
            //System.out.println("Thread #" + Thread.currentThread().getId());
            //System.out.println(mappa.toString());
            val server = serverFactory.createServer()
            try {
                server.start()//Your FTP server starts listening for incoming FTP-connections, using the configuration options previously set
            } catch (ex: FtpException) {
                //Deal with exception as you need
            }

        }
    }


作者:verge_l
链接:https://www.jianshu.com/p/8802cb2a0db5
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值