Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android中FTP服務器搭建入門

Android中FTP服務器搭建入門

編輯:關於Android編程

ftp服務器簡單介紹:FTP(File Transfer Protocol)是文件傳輸協議的簡稱。

作用:讓用戶連接上一個遠程計算機(該計算機上運行著FTP服務器程序)察看遠程計算機有哪些文件,然後把文件從遠程計算機上拷到本地計算機,或把本地計算機的文件送到遠程計算機去。

Apache 官網ftpserver有詳細介紹和使用說明以及必要文件下載:www.2cto.com

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

 

注意不要下載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 en = NetworkInterface
//					.getNetworkInterfaces(); en.hasMoreElements();) {
//				NetworkInterface intf = en.nextElement();
//				for (Enumeration 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> getData() {
		List> list = new ArrayList>();
		Map map = new HashMap();
		map.put(name, archlinux);
		map.put(img, R.drawable.arch);
		list.add(map);

		map = new HashMap();
		map.put(name, dentos);
		map.put(img, R.drawable.centos);
		list.add(map);

		map = new HashMap();
		map.put(name, deepin);
		map.put(img, R.drawable.deepin);
		list.add(map);

		map = new HashMap();
		map.put(name, fedora);
		map.put(img, R.drawable.fedora);
		list.add(map);

		map = new HashMap();
		map.put(name, gentoo);
		map.put(img, R.drawable.gentoo);
		list.add(map);

		map = new HashMap();
		map.put(name, opensuse);
		map.put(img, R.drawable.opensuse);
		list.add(map);

		map = new HashMap();
		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

 

 




    AndroidFtp
    Settings
    Hello world!
    #Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#Rename this file to sources.list(without quotation marks)

#Put this file in /etc/apt/sources.list

#ubuntu12.04

deb http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu/ precise-updates main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu/ precise-backports main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu/ precise main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu/ precise-security main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu/ precise-updates main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu/ precise-proposed main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu/ precise-backports main restricted universe multiverse

#ubuntu12.10

#網易的源(163源,無論是不是教育網,速度都很快)

#deb http://mirrors.163.com/ubuntu/ quantal main universe restricted multiverse

#deb-src http://mirrors.163.com/ubuntu/ quantal main universe restricted multiverse

#deb http://mirrors.163.com/ubuntu/ quantal-security universe main multiverse restricted

#deb-src http://mirrors.163.com/ubuntu/ quantal-security universe main multiverse restricted

#deb http://mirrors.163.com/ubuntu/ quantal-updates universe main multiverse restricted

#deb http://mirrors.163.com/ubuntu/ quantal-proposed universe main multiverse restricted

#deb-src http://mirrors.163.com/ubuntu/ quantal-proposed universe main multiverse restricted

#deb http://mirrors.163.com/ubuntu/ quantal-backports universe main multiverse restricted

#deb-src http://mirrors.163.com/ubuntu/ quantal-backports universe main multiverse restricted

#deb-src http://mirrors.163.com/ubuntu/ quantal-updates universe main multiverse restricted



#搜狐的源(sohu 源今天還沒有更新,不過應該快了)

#deb http://mirrors.sohu.com/ubuntu/ quantal main restricted

#deb-src http://mirrors.sohu.com/ubuntu/ quantal main restricted

#deb http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates main restricted

#deb http://mirrors.sohu.com/ubuntu/ quantal universe

#deb-src http://mirrors.sohu.com/ubuntu/ quantal universe

#deb http://mirrors.sohu.com/ubuntu/ quantal-updates universe

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates universe

#deb http://mirrors.sohu.com/ubuntu/ quantal multiverse

#deb-src http://mirrors.sohu.com/ubuntu/ quantal multiverse

#deb http://mirrors.sohu.com/ubuntu/ quantal-updates multiverse

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-updates multiverse

#deb http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-backports main restricted universe multiverse

#deb http://mirrors.sohu.com/ubuntu/ quantal-security main restricted

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security main restricted

#deb http://mirrors.sohu.com/ubuntu/ quantal-security universe

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security universe

#deb http://mirrors.sohu.com/ubuntu/ quantal-security multiverse

#deb-src http://mirrors.sohu.com/ubuntu/ quantal-security multiverse

#deb http://extras.ubuntu.com/ubuntu quantal main

#deb-src http://extras.ubuntu.com/ubuntu quantal main


#台灣源(台灣的ubuntu 更新源還是很給力的)

#deb http://tw.archive.ubuntu.com/ubuntu/ quantal main universe restricted multiverse

#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal main universe restricted multiverse

#deb http://tw.archive.ubuntu.com/ubuntu/ quantal-security universe main multiverse restricted

#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal-security universe main multiverse restricted

#deb http://tw.archive.ubuntu.com/ubuntu/ quantal-updates universe main multiverse restricted

#deb-src http://tw.archive.ubuntu.com/ubuntu/ quantal-updates universe main multiverse restricted



#骨頭源,骨頭源是bones7456架設的一個Ubuntu源 ,提供ubuntu,deepin

#deb http://ubuntu.srt.cn/ubuntu/ quantal main universe restricted multiverse

#deb-src http://ubuntu.srt.cn/ubuntu/ quantal main universe restricted multiverse

#deb http://ubuntu.srt.cn/ubuntu/ quantal-security universe main multiverse restricted

#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-security universe main multiverse restricted

#deb http://ubuntu.srt.cn/ubuntu/ quantal-updates universe main multiverse restricted

#deb http://ubuntu.srt.cn/ubuntu/ quantal-proposed universe main multiverse restricted

#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-proposed universe main multiverse restricted

#deb http://ubuntu.srt.cn/ubuntu/ quantal-backports universe main multiverse restricted

#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-backports universe main multiverse restricted

#deb-src http://ubuntu.srt.cn/ubuntu/ quantal-updates universe main multiverse restricted



#ubuntu.cn99.com源(推薦):

#deb http://ubuntu.cn99.com/ubuntu/ quantal main restricted universe multiverse

#deb http://ubuntu.cn99.com/ubuntu/ quantal-updates main restricted universe multiverse

#deb http://ubuntu.cn99.com/ubuntu/ quantal-security main restricted universe multiverse

#deb http://ubuntu.cn99.com/ubuntu/ quantal-backports main restricted universe multiverse

#deb http://ubuntu.cn99.com/ubuntu-cn/ quantal main restricted universe multiverse



#教育網源

#電子科技大學

#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal main restricted universe multiverse

#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse

#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse

#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse

#deb http://ubuntu.uestc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse

#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal main restricted universe multiverse

#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse

#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse

#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse

#deb-src http://ubuntu.uestc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse



#中國科技大學

#deb http://debian.ustc.edu.cn/ubuntu/ quantal main restricted universe multiverse

#deb http://debian.ustc.edu.cn/ubuntu/ quantal-backports restricted universe multiverse

#deb http://debian.ustc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse

#deb http://debian.ustc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse

#deb http://debian.ustc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse

#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal main restricted universe multiverse

#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-backports main restricted universe multiverse

#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-proposed main restricted universe multiverse

#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-security main restricted universe multiverse

#deb-src http://debian.ustc.edu.cn/ubuntu/ quantal-updates main restricted universe multiverse

#北京理工大學

#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe

#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe

#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe

#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe

#deb http://mirror.bjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe

#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe

#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe

#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe

#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe

#deb-src http://mirror.bjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe



#蘭州大學

#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal main multiverse restricted universe

#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe

#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe

#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe

#deb ftp://mirror.lzu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe

#deb ftp://mirror.lzu.edu.cn/ubuntu-cn/ quantal main multiverse restricted universe



#上海交通大學(上海交大源,教育網的速度不用說了)

#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe

#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe

#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe

#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe

#deb http://ftp.sjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe

#deb http://ftp.sjtu.edu.cn/ubuntu-cn/ quantal main multiverse restricted universe

#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal main multiverse restricted universe

#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-backports main multiverse restricted universe

#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-proposed main multiverse restricted universe

#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-security main multiverse restricted universe

#deb-src http://ftp.sjtu.edu.cn/ubuntu/ quantal-updates main multiverse restricted universe


#Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#Rename this file to mirrorlist(without quotation marks)

#Put this file in /etc/pacman.d/

#

#

#

#Example

#For i686

Server = http://mirror.lzu.edu.cn/archlinux/$repo/os/i686

#For x86_64

Server = http://mirror.lzu.edu.cn/archlinux/$repo/os/x86_64


#Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#

#

#For CentOS5 Users

#Create a new file CentOS5-Base-LZUOSS.repo(without quotation marks) with content below:

############START_CENTOS_5

[base]

name=CentOS-$releasever - Base - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5



#released updates 

[updates]

name=CentOS-$releasever - Updates - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5



#packages used/produced in the build but not released

[addons]

name=CentOS-$releasever - Addons - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/addons/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=addons

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5



#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5



#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus

gpgcheck=1

enabled=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5



#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib

gpgcheck=1

enabled=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-5

############END_CENTOS_5

#And put this file in /etc/yum.repos.d/

#

#

#For CentOS6 Users

#Create a new file CentOS6-Base-LZUOSS.repo(without quotation marks) with content below:

############START_CENTOS_6

[base]

name=CentOS-$releasever - Base  - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/os/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6



#released updates 

[updates]

name=CentOS-$releasever - Updates  - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/updates/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6



#additional packages that may be useful

[extras]

name=CentOS-$releasever - Extras  - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/extras/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras

gpgcheck=1

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6



#additional packages that extend functionality of existing packages

[centosplus]

name=CentOS-$releasever - Plus  - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/centosplus/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus

gpgcheck=1

enabled=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6



#contrib - packages by Centos Users

[contrib]

name=CentOS-$releasever - Contrib  - LZUOSS

baseurl=http://mirror.lzu.edu.cn/centos/$releasever/contrib/$basearch/

#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib

gpgcheck=1

enabled=0

gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-6

############END_CENTOS_6

#And Put this file in /etc/yum.repos.d/

#

#Make cahce use command like this: yum makecache


#Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#Rename this file to source.list(without quotation marks)

#Put this file in /etc/apt/

#

#

#

deb http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse

deb http://mirror.lzu.edu.cn/ubuntu precise-updates main restricted universe multiverse

#deb http://mirror.lzu.edu.cn/ubuntu precise-proposed main restricted universe multiverse

#deb http://mirror.lzu.edu.cn/ubuntu precise-backports main restricted universe multiverse



deb-src http://mirror.lzu.edu.cn/ubuntu precise main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu precise-security main restricted universe multiverse

deb-src http://mirror.lzu.edu.cn/ubuntu precise-updates main restricted universe multiverse

#deb-src http://mirror.lzu.edu.cn/ubuntu precise-proposed main restricted universe multiverse

#deb-src http://mirror.lzu.edu.cn/ubuntu precise-backports main restricted universe multiverse



deb http://mirror.lzu.edu.cn/deepin quantal main non-free

deb-src http://mirror.lzu.edu.cn/deepin quantal main non-free



deb http://mirror.lzu.edu.cn/deepin quantal-updates main non-free

deb-src http://mirror.lzu.edu.cn/deepin quantal-updates main non-free


#Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#

#Create a new file fedora-LZUOSS.repo(without quotation marks) with content below:

############START_FEDORA_LZUOSS.REPO

[fedora]

name=Fedora $releasever - $basearch - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/os/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-$releasever&arch=$basearch

enabled=1

metadata_expire=7d

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch



[fedora-debuginfo]

name=Fedora $releasever - $basearch - Debug - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/$basearch/debug/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-debug-$releasever&arch=$basearch

enabled=0

metadata_expire=7d

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch



[fedora-source]

name=Fedora $releasever - Source - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/releases/$releasever/Everything/source/SRPMS/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=fedora-source-$releasever&arch=$basearch

enabled=0

metadata_expire=7d

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch

############END_FEDORA_LZUOSS.REPO

#And put this file in /etc/yum.repos.d/

#

#Create a new file fedora-updates-LZUOSS.repo(without quotation marks) with content below:

############START_FEDORA_UPDATES_LZUOSS.REPO

[updates]

name=Fedora $releasever - $basearch - Updates - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-f$releasever&arch=$basearch

enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch



[updates-debuginfo]

name=Fedora $releasever - $basearch - Updates - Debug - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/$basearch/debug/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-debug-f$releasever&arch=$basearch

enabled=0

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch



[updates-source]

name=Fedora $releasever - Updates Source - LZUOSS

failovermethod=priority

baseurl=http://mirror.lzu.edu.cn/fedora/updates/$releasever/SRPMS/

mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=updates-released-source-f$releasever&arch=$basearch

enabled=0

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$basearch

############END_FEDORA_UPDATES_LZUOSS.REPO

#And put this file in /etc/yum.repos.d/

#

#Make cahce use command like this: yum makecache


#Welcome to LZUOSS Mirror Website

#

##This mirror is maintained by LZUOSS, if you have any problem, please let us know in several approaches:

#Email:[email protected]

#Google Public Group:[email protected](https://groups.google.com/d/forum/come-on-all?hl=zh-CN)

#QQ Group:247736999

#

#Our Website:http://oss.lzu.edu.cn

#Our Mirror:http://mirror.lzu.edu.cn

#Our Project Center(GIT):http://oss.lzu.edu.cn/project

#Our Blog:http://oss.lzu.edu.cn/blog

#

#

#

#Back up your files before any change

#

#

#Add or update the file in /etc/make.conf with the last line



GENTOO_MIRRORS=http://mirror.lzu.edu.cn/gentoo/   




ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3


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



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




以上是主要代碼,參考自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.6commonlib文件夾中添加需要的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 authorities = new ArrayList();
  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加密的。其他配置項也很好理解。
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved