Android端与笔记本利用局域网进行FTP通信

阅读更多
先看图
打开前:

Android端与笔记本利用局域网进行FTP通信_第1张图片

打开后:


Android端与笔记本利用局域网进行FTP通信_第2张图片


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 auths = new ArrayList();
			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 ftpletMap = new HashMap();
			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
  • Android端与笔记本利用局域网进行FTP通信_第3张图片
  • 大小: 36.9 KB
  • Android端与笔记本利用局域网进行FTP通信_第4张图片
  • 大小: 38.3 KB
  • ftpserver-core.zip (1.1 MB)
  • 下载次数: 5
  • 查看图片附件

你可能感兴趣的:(android)