FTP communication between Android terminal and notebook using local area network

First look at the
picture Before opening:



After opening:





Activity class: (Don't worry about the integrity of this class, just look at how to operate the ServerFtplet class)
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;

/**
 * Remote management
 * @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("Remote Management", null, null);
		initViews();
	}

	@Override
	public void initViews() {
		// TODO Auto-generated method stub
		TextView tv_state=$(R.id.tv_state);
		tv_state.setText("Network state\n"+wifiInfo.getSSID());
		//0 to -50 means the best signal, -50 to -70 means the signal deviation, less than -70 means the worst, it may not be connected or dropped
		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("After opening, you can manage the files on this device on the computer");
				et_ftp.setVisibility(View.GONE);
				et_ftp.setText("");
				((TextView)v).setText("Open");
				ftp.stop();
			}else{
				tv_des.setText("Enter in the computer:");
				et_ftp.setVisibility(View.VISIBLE);
				et_ftp.setText("ftp://"+getIP()+":"+ServerFtplet.PORT+"/");
				((TextView)v).setText("Close");
				ftp.start();
			}
			checked=!checked;
			break;

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


Key ServerFtplet tool classes:
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;

/**
 * Utilize Android device as FTP server
 * @author Ni Guijun
 *
 */
public class ServerFtplet extends DefaultFtplet {

	public static final int PORT = 3333;
// public static final String directory = Environment.getExternalStorageDirectory().getPath();//Specify SD card root directory
	public static final String directory = Environment.getExternalStorageDirectory().getPath() + "/cloudStore/data/";//Specify to a directory on the SD card
	
	private FtpServer mFtpServer;
	private boolean isAnonymous = true;//Do you need anonymous login

	private final String mUser = "admin";//Account for non-anonymous login
	private final String mPassword = "";

	private static ServerFtplet mInstance;

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

	/**
	 * FTP start
	 *
	 * @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();
	
			// Setting terminal number
			listenerFactory.setPort(PORT);
	
			// Create UserManager through PropertiesUserManagerFactory and add users to the configuration file
			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);
	
			// Add user
			BaseUser user = new BaseUser();
			if(isAnonymous){
				user.setName("anonymous");//Anonymous login, no password required
			}else{
				user.setName(mUser);
				user.setPassword(mPassword);
				user.setAuthorities(auths);
			}
			user.setHomeDirectory(directory);
			userManager.save(user);
	
			// set Ftplet
			Map<String, Ftplet> ftpletMap = new HashMap<String, Ftplet>();
			ftpletMap.put("Ftplet", this);
	
			serverFactory.setUserManager(userManager);
			serverFactory.addListener("default", listenerFactory.createListener());
			serverFactory.setFtplets(ftpletMap);
	
			// Create and start FTPServer
			mFtpServer = serverFactory.createServer();
			mFtpServer.start();
		}catch(FtpException e){
			e.printStackTrace ();
		}
	}

	/**
	 * FTP stop
	 */
	public void stop() {
		// FtpServer does not exist and FtpServer is running
		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();
		// delete the file as soon as it is received
		File uploadFile = new File(FtpUploadPath);
		uploadFile.delete();
		return super.onUploadEnd(session, request);
	}
}


I also need to import 5 jar packages (it is said that it is enough to import two packages, but I tried it and it didn’t work, so I imported 5 jar packages), see the attachment

Android side and Android side use WIFI for FTP communication
http://www .cnblogs.com/zhangkai5157/p/4303188.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326262707&siteId=291194637