Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android系統教程 >> Android開發教程 >> Android端與筆記簿利用局域網進行FTP通信

Android端與筆記簿利用局域網進行FTP通信

編輯:Android開發教程

Android端與筆記本利用局域網進行FTP通信
先看圖
打開前:



打開後:





Activity類:(不要糾結於這個類的完整性,主要看一下怎麼操作ServerFtplet類即可)
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mb.goods.app.BaseActivity;
import com.mb.goods.app.R;
import com.mb.goods.util.ServerFtplet;

/**
 * 遠程管理
 * @author pythoner
 * 
 */
public class RemoteManagementActivity extends BaseActivity implements View.OnClickListener{

	private Context context;
	private TextView tv_des;
	private EditText et_ftp;
	private WifiInfo wifiInfo;
	private boolean checked=false;
	private ServerFtplet ftp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_remote_management);
		context = this;
		WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
		wifiInfo = wifiManager.getConnectionInfo();
		ftp=ServerFtplet.getInstance();
		initActionBar("遠程管理", null, null);
		initViews();
	}

	@Override
	public void initViews() {
		// TODO Auto-generated method stub
		TextView tv_state=$(R.id.tv_state);
		tv_state.setText("網絡狀態\n"+wifiInfo.getSSID());
		//0到-50表示信號最好,-50到-70表示信號偏差,小於-70表示最差,有可能連接不上或者掉線
		int level=Math.abs(wifiInfo.getRssi());
		ImageView tv_rssi=$(R.id.tv_rssi);
		tv_rssi.setImageLevel(level);
		
		tv_des=$(R.id.tv_des);
		et_ftp=$(R.id.et_ftp);
		$(R.id.btn_ok).setOnClickListener(this);
	}

	@Override
	public void updateViews(Object obj) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateTheme(int color) {
		super.updateTheme(color);
		setThemeDrawable(context, R.id.btn_ok);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btn_ok:
			if(checked){
				tv_des.setText("打開後可以在計算機上管理本設備上的文件");
				et_ftp.setVisibility(View.GONE);
				et_ftp.setText("");
				((TextView)v).setText("打開");
				ftp.stop();
			}else{
				tv_des.setText("在計算機中輸入:");
				et_ftp.setVisibility(View.VISIBLE);
				et_ftp.setText("ftp://"+getIP()+":"+ServerFtplet.PORT+"/");
				((TextView)v).setText("關閉");
				ftp.start();
			}
			checked=!checked;
			break;

		default:
			break;
		}
	}
	
	 @Override
    protected void onDestroy() {
        super.onDestroy();
        ftp.stop();
    }
	
}


關鍵的ServerFtplet工具類:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.Authority;
import org.apache.ftpserver.ftplet.DefaultFtplet;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpSession;
import org.apache.ftpserver.ftplet.Ftplet;
import org.apache.ftpserver.ftplet.FtpletResult;
import org.apache.ftpserver.ftplet.UserManager;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import org.apache.ftpserver.usermanager.SaltedPasswordEncryptor;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.usermanager.impl.WritePermission;

import android.os.Environment;

/**
 * 利用Android設備作為FTP服務器
 * @author Ni Guijun
 *
 */
public class ServerFtplet extends DefaultFtplet {

	public static final int PORT = 3333;
//	public static final String directory = Environment.getExternalStorageDirectory().getPath();//指定SD卡根目錄
	public static final String directory = Environment.getExternalStorageDirectory().getPath() + "/cloudStore/data/";//指定到SD卡某個目錄
	
	private FtpServer mFtpServer;
	private boolean isAnonymous = true;//是否需要匿名登陸

	private final String mUser = "admin";//非匿名登陸時的賬戶
	private final String mPassword = "";

	private static ServerFtplet mInstance;

	public static ServerFtplet getInstance() {
		if (mInstance == null) {
			mInstance = new ServerFtplet();
		}
		return mInstance;
	}

	/**
	 * FTP啟動
	 * 
	 * @throws FtpException
	 */
	public void start(){
		try{
			if (null != mFtpServer && false == mFtpServer.isStopped()) {
				return;
			}
	
			File file = new File(directory);
			if (!file.exists()) {
				file.mkdirs();
			}
	
			FtpServerFactory serverFactory = new FtpServerFactory();
			ListenerFactory listenerFactory = new ListenerFactory();
	
			// 設定端末番號
			listenerFactory.setPort(PORT);
	
			// 通過PropertiesUserManagerFactory創建UserManager然後向配置文件添加用戶
			PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
			userManagerFactory.setPasswordEncryptor(new SaltedPasswordEncryptor());
			UserManager userManager = userManagerFactory.createUserManager();
	
			List<Authority> auths = new ArrayList<Authority>();
			Authority auth = new WritePermission();
			auths.add(auth);
	
			// 添加用戶
			BaseUser user = new BaseUser();
			if(isAnonymous){
				user.setName("anonymous");//匿名登陸,不需要密碼
			}else{
				user.setName(mUser);
				user.setPassword(mPassword);
				user.setAuthorities(auths);
			}
			user.setHomeDirectory(directory);
			userManager.save(user);
	
			// 設定Ftplet
			Map<String, Ftplet> ftpletMap = new HashMap<String, Ftplet>();
			ftpletMap.put("Ftplet", this);
	
			serverFactory.setUserManager(userManager);
			serverFactory.addListener("default", listenerFactory.createListener());
			serverFactory.setFtplets(ftpletMap);
	
			// 創建並啟動FTPServer
			mFtpServer = serverFactory.createServer();
			mFtpServer.start();
		}catch(FtpException e){
			e.printStackTrace();
		}
	}

	/**
	 * FTP停止
	 */
	public void stop() {
		// FtpServer不存在和FtpServer正在運行中
		if (null != mFtpServer && false == mFtpServer.isStopped()) {
			mFtpServer.stop();
			mFtpServer=null;
		}
	}

	@Override
	public FtpletResult onAppendStart(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		return super.onAppendStart(session, request);
	}

	@Override
	public FtpletResult onAppendEnd(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		return super.onAppendEnd(session, request);
	}

	@Override
	public FtpletResult onLogin(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		return super.onLogin(session, request);
	}

	@Override
	public FtpletResult onConnect(FtpSession session) throws FtpException,
			IOException {
		return super.onConnect(session);
	}

	@Override
	public FtpletResult onDisconnect(FtpSession session) throws FtpException,
			IOException {
		return super.onDisconnect(session);
	}

	@Override
	public FtpletResult onUploadStart(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		return super.onUploadStart(session, request);
	}

	@Override
	public FtpletResult onUploadEnd(FtpSession session, FtpRequest request)
			throws FtpException, IOException {
		String FtpUploadPath = directory + request.getArgument();
		// 接收到文件後立即刪除
		File uploadFile = new File(FtpUploadPath);
		uploadFile.delete();
		return super.onUploadEnd(session, request);
	}
}


還要導入5個jar包(據說導入兩個包就可以了,但我試了一下不行,所以導入了5個jar包),見附件

Android端與Android端利用WIFI進行FTP通信
http://www.cnblogs.com/zhangkai5157/p/4303188.html
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved