美文网首页
Android apache ftpserver use

Android apache ftpserver use

作者: verge_l | 来源:发表于2018-10-24 23:30 被阅读0次

    编译提示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
                }
    
            }
        }
    

    相关文章

      网友评论

          本文标题:Android apache ftpserver use

          本文链接:https://www.haomeiwen.com/subject/lzmftqtx.html