android ftp服务器实现

通过ApacheFtpServer实现,依赖以下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
资源下载地址 http://download.csdn.net/detail/smile3670/8508525

代码片段
	private static final int PORT = 2222;
	// sd卡目录
	@SuppressLint("SdCardPath")
	private static final String dirname = "/mnt/sdcard/ftp";
	// ftp服务器配置文件路径
	private static final String filename = dirname + "/users.properties";
	private FtpServer mFtpServer = null;
	/**
	 * 开启FTP服务器
	 * @param hostip 本机ip
	 */
	private void startFtpServer(String hostip) {
		FtpServerFactory serverFactory = new FtpServerFactory();


		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
		File files = new File(filename);
		//设置配置文件
		userManagerFactory.setFile(files);
		serverFactory.setUserManager(userManagerFactory.createUserManager());
		// 设置监听IP和端口号
		ListenerFactory factory = new ListenerFactory();
		factory.setPort(PORT);
		factory.setServerAddress(hostip);


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


		// start the server
		mFtpServer = serverFactory.createServer();
		try {
			mFtpServer.start();
			Log.d(TAG, "开启了FTP服务器  ip = " + hostip);
		} catch (FtpException e) {
			System.out.println(e);
		}
	}



开启服务前需在文件路径里添加配置文件users.properties 
/mnt/sdcard/ftp # ls
files
users.properties
/mnt/sdcard/ftp # cat users.properties 
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
 ftpserver.user.admin.homedirectory=/mnt/sdcard/ftp/
 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
 ftpserver.user.anonymous.userpassword=anonymous
 ftpserver.user.anonymous.homedirectory=/mnt/sdcard/ftp/
 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=480000
 ftpserver.user.anonymous.downloadrate=480000


配置修改可参考 http://blog.csdn.net/zzh87615/article/details/6274461


demon下载地址http://download.csdn.net/detail/smile3670/8508939

你可能感兴趣的:(android,android,对讲开发中问题)