当前位置: 代码迷 >> Android >> Android中FTP服务器筹建入门
  详细解决方案

Android中FTP服务器筹建入门

热度:104   发布时间:2016-04-28 02:31:32.0
Android中FTP服务器搭建入门

 ftp服务器简单介绍:FTP(File Transfer Protocol)是文件传输协议的简称。

作用:让用户连接上一个远程计算机(该计算机上运行着FTP服务器程序)察看远程计算机有哪些文件,然后把文件从远程计算机上拷到本地计算机,或把本地计算机的文件送到远程计算机去。

Apache 官网ftpserver有详细介绍和使用说明以及必要文件下载:http://mina.apache.org/ftpserver-project/embedding_ftpserver.html

首先要阅读官网上的介绍,并下载相应资源包。

ftpserver下载地址:http://mina.apache.org/downloads-ftpserver.html

注意不要下载apache-mina的资源包,而要下载ftpserver-1.0.X资源包。

根据官网“Embedding FtpServer in 5 minutes”中提示,导入相应的资源包。

 导入包的步骤参考

方法是:
1,右键工程,Build path,java build path,
2,选择libraries在右边的按钮中点击“Add Library”    
3,选择“User library”,点击“下一步”     
4,点击“User librarys”按钮在出现的界面中点击“New..”按钮在弹出的界面中随便起一个名字,点击“确定”这里注意的问题如下图所示,否则有时会有无穷无尽的麻烦


5,点击“Add jars”按钮选择第三方jar包
创建完的项目结构如下


当然每个人开发环境不尽相同,有时别人有用的方法自己不一定有用,这需要自己去探索解决。
关于项目中引用jar包,相依赖的包名如下(官方介绍参考:http://mina.apache.org/ftpserver-project/embedding_ftpserver.html):
mina-core, 2.0-M3 or later
slf4j-api
ftplet-api
ftpserver-core
实际使用的jar包包括:
slf4j-log4j12-1.5.2.jar 
slf4j-api-1.5.2.jar

mina-core-2.0.4.jar
log4j-1.2.14.jar
ftpserver-core-1.0.6.jar
将上述包成功导入是成功的开始,也是可以继续进行的必要条件。

主要代码如下:

 MainActivity.java

public class MainActivity extends Activity {	static { // 由于Android系统版本原因,有些对ipv6支持存在bug		System.setProperty("java.net.preferIPv6Addresses", "false");	}	private ListView scrList = null;	public static String hostip; // 本机IP	public static String hostmac; // 本机MAC	@SuppressLint("SdCardPath")	//ftp用户配置文件路径	private String filename = "/mnt/sdcard/users.properties";	//sd卡目录	private String dirname="/mnt/sdcard/";	@Override	protected void onCreate(Bundle savedInstanceState) {		System.setProperty("java.net.preferIPv6Addresses", "false");		super.onCreate(savedInstanceState);		try {			creatDirsFiles();		} catch (IOException e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		setContentView(R.layout.activity_main);		scrList = (ListView) findViewById(R.id.scrList);		SimpleAdapter adapter = new SimpleAdapter(this, getData(),				R.layout.list_item, new String[] { "name", "img" }, new int[] {						R.id.nametxtView, R.id.img });		scrList.setAdapter(adapter);		scrList.setOnItemClickListener(itemlistener);		hostip = getLocalIpAddress();		hostmac = getLocalMacAddress();		this.setTitle("ftp://" + hostip + ":2221/");		startFtpServer();	}	private void creatDirsFiles() throws IOException {		// TODO Auto-generated method stub		String tmp=getString(R.string.archlinux);		isFolderExists(dirname);		isFolderExists(dirname+"archlinux/");		File sourceFile = new File(dirname+"archlinux"+"/sources.list");        FileOutputStream fos=null;        try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"centos/");		tmp=getString(R.string.centos);		sourceFile=new File(dirname+"centos"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"deepin/");		tmp=getString(R.string.deepin);		sourceFile=new File(dirname+"deepin"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"fedora/");		tmp=getString(R.string.fedora);		sourceFile=new File(dirname+"fedora"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"gentoo/");		tmp=getString(R.string.gentoo);		sourceFile=new File(dirname+"gentoo"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"opensuse/");		tmp=getString(R.string.ubuntuscr);		sourceFile=new File(dirname+"opensuse"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		isFolderExists(dirname+"ubuntu/");		tmp=getString(R.string.ubuntuscr);		sourceFile=new File(dirname+"ubuntu"+"/sources.list");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }  		tmp=getString(R.string.users);		sourceFile=new File(dirname+"/users.properties");		try {            fos = new FileOutputStream(sourceFile);            fos.write(tmp.getBytes());        } catch (FileNotFoundException e) {                    }		fos.close();	}	OnItemClickListener itemlistener = new OnItemClickListener() {		@Override		public void onItemClick(AdapterView<?> parent, View view, int position,				long id) {			// TODO Auto-generated method stub			TextView nametxtView = (TextView) view					.findViewById(R.id.nametxtView);			String name = "ftp://" + hostip + ":2221/"					+ (String) nametxtView.getText() + "/";			Bundle bundle = new Bundle();			bundle.putString("name", name);			Intent intent = new Intent(MainActivity.this, Downloadaddress.class);			intent.putExtras(bundle);			startActivity(intent);		}	};	private void startFtpServer() {		FtpServerFactory serverFactory = new FtpServerFactory();		System.out.println("Factory创建");		ListenerFactory factory = new ListenerFactory();		PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();		File files = new File(filename);		userManagerFactory.setFile(files);		serverFactory.setUserManager(userManagerFactory.createUserManager());		System.out.println(serverFactory.getFileSystem());		// 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();		try {			server.start();		} catch (FtpException e) {			System.out.println(e);		}	}	@Override	public boolean onCreateOptionsMenu(Menu menu) {		// Inflate the menu; this adds items to the action bar if it is present.		getMenuInflater().inflate(R.menu.main, menu);		return true;	}	public String getLocalIpAddress() {//		try {//			for (Enumeration<NetworkInterface> en = NetworkInterface//					.getNetworkInterfaces(); en.hasMoreElements();) {//				NetworkInterface intf = en.nextElement();//				for (Enumeration<InetAddress> enumIpAddr = intf//						.getInetAddresses(); enumIpAddr.hasMoreElements();) {//					InetAddress inetAddress = enumIpAddr.nextElement();//					if (!inetAddress.isLoopbackAddress()) {//						return inetAddress.getHostAddress().toString();//					}//				}//			}//		} catch (SocketException ex) {//			Log.e("WifiPreference IpAddress", ex.toString());//		}//		return null;		WifiManager wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);  		   int ip = wifiManager.getConnectionInfo().getIpAddress();  		   String realIp = intToIp(ip);		   return realIp;	}	 public String intToIp(int i) {      	        return (i & 0xFF ) + "." + ((i >> 8 ) & 0xFF) + "." + ((i >> 16 ) & 0xFF) + "." + ( i >> 24 & 0xFF) ; 	 }	public String getLocalMacAddress() {		WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);		WifiInfo info = wifi.getConnectionInfo();		return info.getMacAddress();	}	private List<Map<String, Object>> getData() {		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();		Map<String, Object> map = new HashMap<String, Object>();		map.put("name", "archlinux");		map.put("img", R.drawable.arch);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "dentos");		map.put("img", R.drawable.centos);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "deepin");		map.put("img", R.drawable.deepin);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "fedora");		map.put("img", R.drawable.fedora);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "gentoo");		map.put("img", R.drawable.gentoo);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "opensuse");		map.put("img", R.drawable.opensuse);		list.add(map);		map = new HashMap<String, Object>();		map.put("name", "ubuntu");		map.put("img", R.drawable.ubuntu);		list.add(map);		return list;	}	boolean isFolderExists(String strFolder) {		File file = new File(strFolder);		if (!file.exists()) {			if (file.mkdirs()) {				return true;			} else				return false;		}		return true;	}}
String.xml

 

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">AndroidFtp</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string>    <string name="ubuntuscr">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#Rename this file to "sources.list"(without quotation marks)\r\n#Put this file in /etc/apt/sources.list\r\n#ubuntu12.04\r\ndeb http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu/ precise-updates main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu/ precise-backports main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu/ precise-updates main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu/ precise-backports main restricted universe multiverse\r\n#ubuntu12.10\r\n#网易的源(163源,无论是不是教育网,速度都很快)\r\n#deb http://mirrors.163.com/ubuntu/ quantal main universe restricted multiverse\r\n#deb-src http://mirrors.163.com/ubuntu/ quantal main universe restricted multiverse\r\n#deb http://mirrors.163.com/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb-src http://mirrors.163.com/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb http://mirrors.163.com/ubuntu/ quantal-updates universe main multiverse restricted\r\n#deb http://mirrors.163.com/ubuntu/ quantal-proposed universe main multiverse restricted\r\n#deb-src http://mirrors.163.com/ubuntu/ quantal-proposed universe main multiverse restricted\r\n#deb http://mirrors.163.com/ubuntu/ quantal-backports universe main multiverse restricted\r\n#deb-src http://mirrors.163.com/ubuntu/ quantal-backports universe main multiverse restricted\r\n#deb-src http://mirrors.163.com/ubuntu/ quantal-updates universe main multiverse restricted\r\n\r\n#搜狐的源(sohu 源今天还没有更新,不过应该快了)\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal main restricted\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal main restricted\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal universe\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal universe\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-updates universe\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates universe\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal multiverse\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal multiverse\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-updates multiverse\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates multiverse\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-security main restricted\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security main restricted\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-security universe\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security universe\r\n#deb http://mirrors.sohu.com/ubuntu/ quantal-security multiverse\r\n#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security multiverse\r\n#deb http://extras.ubuntu.com/ubuntu quantal main\r\n#deb-src http://extras.ubuntu.com/ubuntu quantal main\r\n#台湾源(台湾的ubuntu 更新源还是很给力的)\r\n#deb http://tw.archive.ubuntu.com/ubuntu/ quantal main universe restricted multiverse\r\n#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal main universe restricted multiverse\r\n#deb http://tw.archive.ubuntu.com/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb http://tw.archive.ubuntu.com/ubuntu/ quantal-updates universe main multiverse restricted\r\n#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal-updates universe main multiverse restricted\r\n\r\n#骨头源,骨头源是bones7456架设的一个Ubuntu源 ,提供ubuntu,deepin\r\n#deb http://ubuntu.srt.cn/ubuntu/ quantal main universe restricted multiverse\r\n#deb-src http://ubuntu.srt.cn/ubuntu/ quantal main universe restricted multiverse\r\n#deb http://ubuntu.srt.cn/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-security universe main multiverse restricted\r\n#deb http://ubuntu.srt.cn/ubuntu/ quantal-updates universe main multiverse restricted\r\n#deb http://ubuntu.srt.cn/ubuntu/ quantal-proposed universe main multiverse restricted\r\n#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-proposed universe main multiverse restricted\r\n#deb http://ubuntu.srt.cn/ubuntu/ quantal-backports universe main multiverse restricted\r\n#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-backports universe main multiverse restricted\r\n#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-updates universe main multiverse restricted\r\n\r\n#ubuntu.cn99.com源(推荐):\r\n#deb http://ubuntu.cn99.com/ubuntu/ quantal main restricted universe multiverse\r\n#deb http://ubuntu.cn99.com/ubuntu/ quantal-updates main restricted universe multiverse\r\n#deb http://ubuntu.cn99.com/ubuntu/ quantal-security main restricted universe multiverse\r\n#deb http://ubuntu.cn99.com/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb http://ubuntu.cn99.com/ubuntu-cn/ quantal main restricted universe multiverse\r\n\r\n#教育网源\r\n#电子科技大学\r\n#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal main restricted universe multiverse\r\n#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse\r\n#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse\r\n#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse\r\n#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal main restricted universe multiverse\r\n#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse\r\n#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse\r\n#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse\r\n\r\n#中国科技大学\r\n#deb http://debian.ustc.edu.cn/ubuntu/ quantal main restricted universe multiverse\r\n#deb http://debian.ustc.edu.cn/ubuntu/ quantal-backports restricted universe multiverse\r\n#deb http://debian.ustc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse\r\n#deb http://debian.ustc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse\r\n#deb http://debian.ustc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse\r\n#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal main restricted universe multiverse\r\n#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse\r\n#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse\r\n#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse\r\n#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse\r\n#北京理工大学\r\n#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe\r\n#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe\r\n#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe\r\n#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe\r\n#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe\r\n#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe\r\n#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe\r\n#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe\r\n#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe\r\n#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe\r\n\r\n#兰州大学\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal main multiverse restricted universe\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe\r\n#deb ftp://mirror.lzu.edu.cn/ubuntu-cn/ quantal main multiverse restricted universe\r\n\r\n#上海交通大学(上海交大源,教育网的速度不用说了)\r\n#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe\r\n#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe\r\n#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe\r\n#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe\r\n#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe\r\n#deb http://ftp.sjtu.edu.cn/ubuntu-cn/ quantal main multiverse restricted universe\r\n#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe\r\n#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe\r\n#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe\r\n#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe\r\n#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe\r\n</string><string name="archlinux">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#Rename this file to "mirrorlist"(without quotation marks)\r\n#Put this file in /etc/pacman.d/\r\n#\r\n#\r\n#\r\n#Example\r\n#For i686\r\nServer = http://mirror.lzu.edu.cn/archlinux/$repo/os/i686\r\n#For x86_64\r\nServer = http://mirror.lzu.edu.cn/archlinux/$repo/os/x86_64\r\n</string><string name="centos">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#\r\n#\r\n#For CentOS5 Users\r\n#Create a new file "CentOS5-Base-LZUOSS.repo"(without quotation marks) with content below:\r\n############START_CENTOS_5\r\n[base]\r\nname=CentOS-$releasever - Base - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n\r\n#released updates \r\n[updates]\r\nname=CentOS-$releasever - Updates - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n\r\n#packages used/produced in the build but not released\r\n[addons]\r\nname=CentOS-$releasever - Addons - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/addons/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n\r\n#additional packages that may be useful\r\n[extras]\r\nname=CentOS-$releasever - Extras - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n\r\n#additional packages that extend functionality of existing packages\r\n[centosplus]\r\nname=CentOS-$releasever - Plus - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus\r\ngpgcheck=1\r\nenabled=0\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n\r\n#contrib - packages by Centos Users\r\n[contrib]\r\nname=CentOS-$releasever - Contrib - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib\r\ngpgcheck=1\r\nenabled=0\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n############END_CENTOS_5\r\n#And put this file in /etc/yum.repos.d/\r\n#\r\n#\r\n#For CentOS6 Users\r\n#Create a new file "CentOS6-Base-LZUOSS.repo"(without quotation marks) with content below:\r\n############START_CENTOS_6\r\n[base]\r\nname=CentOS-$releasever - Base  - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n\r\n#released updates \r\n[updates]\r\nname=CentOS-$releasever - Updates  - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n\r\n#additional packages that may be useful\r\n[extras]\r\nname=CentOS-$releasever - Extras  - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras\r\ngpgcheck=1\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n\r\n#additional packages that extend functionality of existing packages\r\n[centosplus]\r\nname=CentOS-$releasever - Plus  - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus\r\ngpgcheck=1\r\nenabled=0\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n\r\n#contrib - packages by Centos Users\r\n[contrib]\r\nname=CentOS-$releasever - Contrib  - LZUOSS\r\nbaseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/\r\n#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib\r\ngpgcheck=1\r\nenabled=0\r\ngpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n############END_CENTOS_6\r\n#And Put this file in /etc/yum.repos.d/\r\n#\r\n#Make cahce use command like this: yum makecache\r\n</string><string name="deepin">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#Rename this file to "source.list"(without quotation marks)\r\n#Put this file in /etc/apt/\r\n#\r\n#\r\n#\r\ndeb http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse\r\ndeb http://mirror.lzu.edu.cn/ubuntu precise-updates main restricted universe multiverse\r\n#deb http://mirror.lzu.edu.cn/ubuntu precise-proposed main restricted universe multiverse\r\n#deb http://mirror.lzu.edu.cn/ubuntu precise-backports main restricted universe multiverse\r\n\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse\r\ndeb-src http://mirror.lzu.edu.cn/ubuntu precise-updates main restricted universe multiverse\r\n#deb-src http://mirror.lzu.edu.cn/ubuntu precise-proposed main restricted universe multiverse\r\n#deb-src http://mirror.lzu.edu.cn/ubuntu precise-backports main restricted universe multiverse\r\n\r\ndeb http://mirror.lzu.edu.cn/deepin quantal main non-free\r\ndeb-src http://mirror.lzu.edu.cn/deepin quantal main non-free\r\n\r\ndeb http://mirror.lzu.edu.cn/deepin quantal-updates main non-free\r\ndeb-src http://mirror.lzu.edu.cn/deepin quantal-updates main non-free\r\n</string><string name="fedora">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#\r\n#Create a new file "fedora-LZUOSS.repo"(without quotation marks) with content below:\r\n############START_FEDORA_LZUOSS.REPO\r\n[fedora]\r\nname=Fedora $releasever - $basearch - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch\r\nenabled=1\r\nmetadata_expire=7d\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n\r\n[fedora-debuginfo]\r\nname=Fedora $releasever - $basearch - Debug - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/debug/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-debug-$releasever&arch=$basearch\r\nenabled=0\r\nmetadata_expire=7d\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n\r\n[fedora-source]\r\nname=Fedora $releasever - Source - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/source/SRPMS/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-source-$releasever&arch=$basearch\r\nenabled=0\r\nmetadata_expire=7d\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n############END_FEDORA_LZUOSS.REPO\r\n#And put this file in /etc/yum.repos.d/\r\n#\r\n#Create a new file "fedora-updates-LZUOSS.repo"(without quotation marks) with content below:\r\n############START_FEDORA_UPDATES_LZUOSS.REPO\r\n[updates]\r\nname=Fedora $releasever - $basearch - Updates - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&arch=$basearch\r\nenabled=1\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n\r\n[updates-debuginfo]\r\nname=Fedora $releasever - $basearch - Updates - Debug - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/debug/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-debug-f$releasever&arch=$basearch\r\nenabled=0\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n\r\n[updates-source]\r\nname=Fedora $releasever - Updates Source - LZUOSS\r\nfailovermethod=priority\r\nbaseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/SRPMS/\r\nmirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-source-f$releasever&arch=$basearch\r\nenabled=0\r\ngpgcheck=1\r\ngpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n############END_FEDORA_UPDATES_LZUOSS.REPO\r\n#And put this file in /etc/yum.repos.d/\r\n#\r\n#Make cahce use command like this: yum makecache\r\n</string><string name="gentoo">#Welcome to LZUOSS Mirror Website\r\n#\r\n##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:\r\n#Email:[email protected]\r\n#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)\r\n#QQ Group:247736999\r\n#\r\n#Our Website:http://oss.lzu.edu.cn\r\n#Our Mirror:http://mirror.lzu.edu.cn\r\n#Our Project Center(GIT):http://oss.lzu.edu.cn/project\r\n#Our Blog:http://oss.lzu.edu.cn/blog\r\n#\r\n#\r\n#\r\n#Back up your files before any change\r\n#\r\n#\r\n#Add or update the file in /etc/make.conf with the last line\r\n\r\nGENTOO_MIRRORS="http://mirror.lzu.edu.cn/gentoo/"   \r\n</string><string name="opensuse"></string><string name="users">ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3\r\nftpserver.user.admin.homedirectory=/mnt/sdcard/\r\nftpserver.user.admin.enableflag=true\r\nftpserver.user.admin.writepermission=true\r\nftpserver.user.admin.maxloginnumber=0\r\nftpserver.user.admin.maxloginperip=0\r\nftpserver.user.admin.idletime=0\r\nftpserver.user.admin.uploadrate=0\r\nftpserver.user.admin.downloadrate=0\r\nftpserver.user.anonymous.userpassword=\r\nftpserver.user.anonymous.homedirectory=/mnt/sdcard/\r\nftpserver.user.anonymous.enableflag=true\r\nftpserver.user.anonymous.writepermission=false\r\nftpserver.user.anonymous.maxloginnumber=20\r\nftpserver.user.anonymous.maxloginperip=2\r\nftpserver.user.anonymous.idletime=300\r\nftpserver.user.anonymous.uploadrate=4800\r\nftpserver.user.anonymous.downloadrate=4800\r\n</string></resources>
 以上是主要代码,参考自http://www.eoeandroid.com/thread-281101-1-1.html

   另外有比较好的参考资料:http://www.tuicool.com/articles/iqE3Ef

 



Apache FtpServer是一个纯Java实现的FTP服务器,基于大名鼎鼎的网络框架apache MINA实现。它既可以作为一个完整的FTP服务器单独使用,也可以在Java程序中调用,类似于Jetty可以作为嵌入式的HTTP服务器。

下面介绍如何在Java中启动FTP服务器。 

Apache FtpServer下载地址,目前最新版是1.0.6: 

http://mina.apache.org/ftpserver-project/index.html 

解压后在apache-ftpserver-1.0.6\common\lib文件夹中添加需要的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 

另外,项目中还需要加入log4j的配置文件,当然没有话程序也可以跑,只是会出现一些警告信息而且没有日志记录。 

public static void main(String[] args) throws FtpException {    FtpServerFactory serverFactory = new FtpServerFactory();  FtpServer server = serverFactory.createServer();  server.start();  }
这是最简单的FTP服务器。运行程序,启动FTP服务器后,在地址栏中输入ftp://localhost,可以看到以下界面,要求输入用户名密码。当然这个FTP是进不去的,因为它是最简单的FTP服务器,简单到没有用户。 


2、设置匿名用户及对应的服务器文件夹 

public static void main(String[] args) throws FtpException {    FtpServerFactory serverFactory = new FtpServerFactory();  BaseUser user = new BaseUser();  user.setName("anonymous");  user.setHomeDirectory("D:/test");    serverFactory.getUserManager().save(user);    FtpServer server = serverFactory.createServer();  server.start();  }
添加一个匿名用户anonymous,并设置它对应的文件夹是D:/test。再次进入ftp://localhost,可以看到D:/test中的文件。但是此时的FTP权限是只读的,也就是也可查看文件,但是不能增删改。 


public static void main(String[] args) throws FtpException {    FtpServerFactory serverFactory = new FtpServerFactory();  BaseUser user = new BaseUser();  user.setName("anonymous");  user.setHomeDirectory("D:/test");    List<Authority> authorities = new ArrayList<Authority>();  authorities.add(new WritePermission());  user.setAuthorities(authorities);    serverFactory.getUserManager().save(user);    FtpServer server = serverFactory.createServer();  server.start();  }
加入可写的权限,此时就能对FTP服务器上的文件进行增删改了。 
public static void main(String[] args) throws FtpException {    FtpServerFactory serverFactory = new FtpServerFactory();  BaseUser user = new BaseUser();  user.setName("test");  user.setPassword("123456");  user.setHomeDirectory("D:/test");    serverFactory.getUserManager().save(user);    FtpServer server = serverFactory.createServer();  server.start();  }
添加用户test,密码是123456,此时客户端要想进入ftp,必须输入正确的用户名密码。 
public static void main(String[] args) throws FtpException {    FtpServerFactory serverFactory = new FtpServerFactory();  PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();  userManagerFactory.setFile(new File("users.properties"));    serverFactory.setUserManager(userManagerFactory.createUserManager());    FtpServer server = serverFactory.createServer();  server.start();  }
配置文件users.properties:
# Password is "admin"ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3ftpserver.user.admin.homedirectory=D:/testftpserver.user.admin.enableflag=trueftpserver.user.admin.writepermission=trueftpserver.user.admin.maxloginnumber=0ftpserver.user.admin.maxloginperip=0ftpserver.user.admin.idletime=0ftpserver.user.admin.uploadrate=0ftpserver.user.admin.downloadrate=0ftpserver.user.anonymous.userpassword=ftpserver.user.anonymous.homedirectory=D:/testftpserver.user.anonymous.enableflag=trueftpserver.user.anonymous.writepermission=falseftpserver.user.anonymous.maxloginnumber=20ftpserver.user.anonymous.maxloginperip=2ftpserver.user.anonymous.idletime=300ftpserver.user.anonymous.uploadrate=4800ftpserver.user.anonymous.downloadrate=4800
这是通过配置文件users.properties设置用户。配置文件中包含两个用户:匿名用户anonymous和admin。anonymous只有只读权限,admin有可写权限。其中userpassword配置项是MD5加密的。其他配置项也很好理解。
1楼adayabetter昨天 16:36
站在巨人肩上,大家互相学习。
  相关解决方案