Android打开FTP服务端。
这个功能还是很实用的,可以方便手机和电脑之间传输文件。
这个功能其实也很容易,导入几个包,加几个权限,然后几行代码就搞定了。
一.导入几个包:
1.ftpserver-core-1.0.6.jar
2.log4j-1.2.14.jar
3.mina-core-2.0.4.jar
4.slf4j-api-1.5.2.jar
5.slf4j-log4j12-1.5.2.jar
6.commons-net-3.0.1.jar
二.加几个权限:
1.<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />(从存储卡上读取相关配置)
2.<uses-permission android:name="android.permission.INTERNET" />(没有这个绑定端口的时候会失败的)
三.几行代码:
/*
* Created by yy on 2016/11/3.
*/
public class FtpServerManager {
private static final String TAG = "FileUtil";
private static FtpServerManager instance = new FtpServerManager();
private FtpServerManager() {}//构造函数私有化防止创建实例
public static FtpServerManager getInstance() {
return instance;
}
private static FtpServer mFtpServer;
private static int port = 2222;// 端口号
private static String ftpConfigDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ftpConfig/";
private static FtpServerFactory serverFactory;
public void initFTPServer() {
System.setProperty("java.net.preferIPv4Stack", "true");
serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
String[] str = {"mkdir", ftpConfigDir};
try {
Process ps = Runtime.getRuntime().exec(str);
try {
ps.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
String filename = ftpConfigDir + "users.properties";
File files = new File(filename);
userManagerFactory.setFile(files);
serverFactory.setUserManager(userManagerFactory.createUserManager());
factory.setPort(port);
serverFactory.addListener("default", factory.createListener());
FtpServer server = serverFactory.createServer();
mFtpServer = server;
}
public void startFTPService() {
// Log.i(TAG, "通知: 开启FTPService");
if (mFtpServer == null) {
// Log.i(TAG, "通知: mFtpServer为null");
mFtpServer = serverFactory.createServer();
try {
mFtpServer.start();
} catch (FtpException e) {
e.printStackTrace();
}
} else {
// Log.i(TAG, "通知: mFtpServer不为null");
if (mFtpServer.isStopped() == true) {
try {
mFtpServer.start();
// Log.i(TAG, "通知: 开启FTPService===2");
} catch (FtpException e) {
e.printStackTrace();
}
}
}
// Log.i(TAG, "通知: mFtpServer.isStopped() " + mFtpServer.isStopped());
}
public void restartFTPService() {
Log.i(TAG, "通知: 重启FTPService");
if (mFtpServer != null) {
if (mFtpServer.isStopped() == false) {
mFtpServer.resume();
}
}
}
public void stopFTPService() {
Log.i(TAG, "通知: 关闭FTPService");
if (mFtpServer != null) {
Log.i(TAG, "通知: mFtpServer不为null");
if (mFtpServer.isStopped() == false) {
mFtpServer.stop();
Log.i(TAG, "mFtpServer.isStopped():" + mFtpServer.isStopped());
}
mFtpServer = null;
}
Log.i(TAG, "通知: 关闭成功");
}
}
四.使用说明:
1.在/sdcard/ftpConfig目录下放入users.properties文件,内容如下:
# Password is "admin"
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=/sdcard/
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=20
ftpserver.user.admin.maxloginperip=5
ftpserver.user.admin.idletime=300
ftpserver.user.admin.uploadrate=4800000
ftpserver.user.admin.downloadrate=4800000
2.调用时先初始化,再开始,最后停止。
3.长时间开始会导致FTP服务有时不好用,可能跟android资源回收导致,所以每一段时间可以选择restart一下
恩,就到这里,随后写一个demo上传上来方便自己使用,GitHub地址如下:
https://github.com/yy4myself/FTPService.git
后面还会继续维护,要改的地方还很多