1. 程式人生 > >Android中FTP伺服器搭建入門

Android中FTP伺服器搭建入門

 ftp伺服器簡單介紹:FTP(File Transfer Protocol)是檔案傳輸協議的簡稱。

作用:讓使用者連線上一個遠端計算機(該計算機上執行著FTP伺服器程式)察看遠端計算機有哪些檔案,然後把檔案從遠端計算機上拷到本地計算機,或把本地計算機的檔案送到遠端計算機去。

首先要閱讀官網上的介紹,並下載相應資源包。

注意不要下載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\n
deb http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse\r\n
deb http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse\r\n
deb 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
deb-src http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse\r\n
deb-src http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse\r\n
deb-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
#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\n
Server = http://mirror.lzu.edu.cn/archlinux/$repo/os/i686\r\n
#For x86_64\r\n
Server = 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\n
name=CentOS-$releasever - Base - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os\r\n
gpgcheck=1\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n
\r\n
#released updates \r\n
[updates]\r\n
name=CentOS-$releasever - Updates - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates\r\n
gpgcheck=1\r\n
gpgkey=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\n
name=CentOS-$releasever - Addons - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/addons/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons\r\n
gpgcheck=1\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n
\r\n
#additional packages that may be useful\r\n
[extras]\r\n
name=CentOS-$releasever - Extras - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras\r\n
gpgcheck=1\r\n
gpgkey=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\n
name=CentOS-$releasever - Plus - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus\r\n
gpgcheck=1\r\n
enabled=0\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5\r\n
\r\n
#contrib - packages by Centos Users\r\n
[contrib]\r\n
name=CentOS-$releasever - Contrib - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib\r\n
gpgcheck=1\r\n
enabled=0\r\n
gpgkey=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\n
name=CentOS-$releasever - Base  - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os\r\n
gpgcheck=1\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n
\r\n
#released updates \r\n
[updates]\r\n
name=CentOS-$releasever - Updates  - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates\r\n
gpgcheck=1\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n
\r\n
#additional packages that may be useful\r\n
[extras]\r\n
name=CentOS-$releasever - Extras  - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras\r\n
gpgcheck=1\r\n
gpgkey=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\n
name=CentOS-$releasever - Plus  - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus\r\n
gpgcheck=1\r\n
enabled=0\r\n
gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6\r\n
\r\n
#contrib - packages by Centos Users\r\n
[contrib]\r\n
name=CentOS-$releasever - Contrib  - LZUOSS\r\n
baseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/\r\n
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib\r\n
gpgcheck=1\r\n
enabled=0\r\n
gpgkey=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\n
deb http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse\r\n
deb http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse\r\n
deb 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\n
deb-src http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse\r\n
deb-src http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse\r\n
deb-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\n
deb http://mirror.lzu.edu.cn/deepin quantal main non-free\r\n
deb-src http://mirror.lzu.edu.cn/deepin quantal main non-free\r\n
\r\n
deb http://mirror.lzu.edu.cn/deepin quantal-updates main non-free\r\n
deb-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\n
name=Fedora $releasever - $basearch - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch\r\n
enabled=1\r\n
metadata_expire=7d\r\n
gpgcheck=1\r\n
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n
\r\n
[fedora-debuginfo]\r\n
name=Fedora $releasever - $basearch - Debug - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/debug/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-debug-$releasever&arch=$basearch\r\n
enabled=0\r\n
metadata_expire=7d\r\n
gpgcheck=1\r\n
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n
\r\n
[fedora-source]\r\n
name=Fedora $releasever - Source - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/source/SRPMS/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-source-$releasever&arch=$basearch\r\n
enabled=0\r\n
metadata_expire=7d\r\n
gpgcheck=1\r\n
gpgkey=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\n
name=Fedora $releasever - $basearch - Updates - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&arch=$basearch\r\n
enabled=1\r\n
gpgcheck=1\r\n
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n
\r\n
[updates-debuginfo]\r\n
name=Fedora $releasever - $basearch - Updates - Debug - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/debug/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-debug-f$releasever&arch=$basearch\r\n
enabled=0\r\n
gpgcheck=1\r\n
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch\r\n
\r\n
[updates-source]\r\n
name=Fedora $releasever - Updates Source - LZUOSS\r\n
failovermethod=priority\r\n
baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/SRPMS/\r\n
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-source-f$releasever&arch=$basearch\r\n
enabled=0\r\n
gpgcheck=1\r\n
gpgkey=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\n
GENTOO_MIRRORS="http://mirror.lzu.edu.cn/gentoo/"   \r\n
</string>
<string name="opensuse"></string>
<string name="users">
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3\r\n

ftpserver.user.admin.homedirectory=/mnt/sdcard/\r\n

ftpserver.user.admin.enableflag=true\r\n

ftpserver.user.admin.writepermission=true\r\n

ftpserver.user.admin.maxloginnumber=0\r\n

ftpserver.user.admin.maxloginperip=0\r\n

ftpserver.user.admin.idletime=0\r\n

ftpserver.user.admin.uploadrate=0\r\n

ftpserver.user.admin.downloadrate=0\r\n


ftpserver.user.anonymous.userpassword=\r\n

ftpserver.user.anonymous.homedirectory=/mnt/sdcard/\r\n

ftpserver.user.anonymous.enableflag=true\r\n

ftpserver.user.anonymous.writepermission=false\r\n

ftpserver.user.anonymous.maxloginnumber=20\r\n

ftpserver.user.anonymous.maxloginperip=2\r\n

ftpserver.user.anonymous.idletime=300\r\n

ftpserver.user.anonymous.uploadrate=4800\r\n

ftpserver.user.anonymous.downloadrate=4800\r\n

</string>

</resources>

Apache FtpServer是一個純Java實現的FTP伺服器,基於大名鼎鼎的網路框架apache MINA實現。它既可以作為一個完整的FTP伺服器單獨使用,也可以在Java程式中呼叫,類似於Jetty可以作為嵌入式的HTTP伺服器。

下面介紹如何在Java中啟動FTP伺服器。 

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

解壓後在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=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=D:/test
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=
ftpserver.user.anonymous.homedirectory=D:/test
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
這是通過配置檔案users.properties設定使用者。配置檔案中包含兩個使用者:匿名使用者anonymous和admin。anonymous只有只讀許可權,admin有可寫許可權。其中userpassword配置項是MD5加密的。其他配置項也很好理解。