日期:2014-05-16  浏览次数:20990 次

Android使用ApacheFtpServer实现Ftp服务器(增加用户验证)
为了方便在Android手机和PC电脑之间文件传输和管理,于是想在Android上建立FTP服务器来实现文件访问及管理操作。从网上找到这个例子后开始使用,结果发现,死活都登录不了,配置不了用户名和密码,匿名登录也登不上,给我急的啊,后来看了一下FTPServer的源码发现需要一个配置文件,所以就找来自己加上了,再一试,OK,可以登录了,分享给大家!

1、导入Apache FtpServer依赖jar包
commons-net-ftp-2.0.jar
ftpserver-core-1.0.6.jar
log4j-1.2.14.jar
mina-core-2.0.4.jar
slf4j-api-1.5.2.jar
slf4j-log4j12-1.5.2.jar


2、Android启动FtpServer示例代码
package com.orgcent.ftp;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;

import android.app.Activity;
import android.os.Bundle;

public class FtpServerActivity extends Activity {

    static { //由于Android系统版本原因,有些对ipv6支持存在bug,相关异常看文章底部介绍
        System.setProperty("java.net.preferIPv6Addresses", "false");
    }
    
    private FtpServer mFtpServer;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startFtpServer();
    }

    private void startFtpServer() {
        FtpServerFactory serverFactory = new FtpServerFactory();

        ListenerFactory factory = new ListenerFactory();

//这段很重要,是增加用户配置文件的,网上其它的文章里都没写这段,所以根本登录不上,加上这段就可以登录上了
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		File files=new File("/mnt/sdcard/users.properties");
		userManagerFactory.setFile(files);
        serverFactory.setUserManager(userManagerFactory.createUserManager());
        // set the port of the listener
        int port = 2221;
        factory.setPort(port);

        // replace the default listener
        serverFactory.addListener("default", factory.createListener());

        // start the server
        FtpServer server = serverFactory.createServer();
        this.mFtpServer = server;
        try {
            server.start();
        } catch (FtpException e) {
            e.printStackTrace();
        }

    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        
        if(null != mFtpServer) {
            mFtpServer.stop();
            mFtpServer = null;
        }
    }
}


3.users.properties配置文件内容,将该文件保存到/mnt/sdcard目录下
#开启admin帐户
# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
#ftp共享目录
ftpserver.user.admin.homedirectory=/mnt/sdcard
#是否启用
ftpserver.user.admin.enableflag=true
#写权限
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0

#开启anonymous帐号,不用密码,直接能登录
ftpserver.user.anonymous.userpassword=
ftpserver.user.anonymous.homedirectory=/mnt/sdcard
ftpserver.user.anonymous.enableflag=true
ftpserver.user.anonymous.writepermission=false
ftpserver.user.anonymous.maxloginnumber=20
ftpserver.user.anonymous.maxloginperip=2
ftpserver.user.anonymous.idletime=300
ftpserver.user.anonymous.uploadrate=4800
ftpserver.user.anonymous.downloadrate=4800


Demo下载地址:http://code.google.com/p/android-custom-view/downloads/list