26、android上跑apache的ftp服务
一、为啥
在android设备跑ftp服务,在现场方便查看日志,目前就是这么用的。
二、前提:
从apache的官网下载依赖包:http://mina.apache.org/ftpserver-project/download_1.0.6.html
解压后如下:
在最右侧的jar包列表中,并不需要全部导入我们的工程,需要导入的包为:
记得把jar包添加到buildPath,同时在order and export选项选中上步添加的jar包
三、如何用:
1 package com.example.zzk; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 import org.apache.ftpserver.FtpServer; 7 import org.apache.ftpserver.FtpServerFactory; 8 import org.apache.ftpserver.ftplet.FtpException; 9 import org.apache.ftpserver.listener.ListenerFactory; 10 import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; 11 12 13 import android.app.Activity; 14 import android.content.Context; 15 import android.os.Bundle; 16 import android.os.Environment; 17 import android.view.Menu; 18 import android.view.MenuItem; 19 20 public class MainActivity extends Activity { 21 22 public static String path = Environment.getExternalStorageDirectory()+File.separator+"FTP_TEST"+ File.separator; 23 public Context ctxt; 24 public String tag = "zzk"; 25 public static FtpServer ftpServer; 26 27 @Override 28 protected void onCreate(Bundle savedInstanceState) { 29 super.onCreate(savedInstanceState); 30 setContentView(R.layout.activity_main); 31 32 ctxt = getApplicationContext(); 33 ftpServiceStart(ctxt); 34 } 35 36 37 /** 38 * 39 * 启动ftp服务 40 */ 41 public static void ftpServiceStart(Context ctxt) 42 { 43 FtpServerFactory serverFactory = new FtpServerFactory(); 44 45 ListenerFactory factory = new ListenerFactory(); 46 47 factory.setPort(12345); // set the port of the listener 48 49 FIleUtils fu = new FIleUtils(ctxt); 50 try { 51 File file = new File(path); 52 if(!file.isDirectory()) 53 { 54 file.mkdir(); 55 } 56 fu.createFile(path+"ftpserver.properties"); 57 String str = "" + 58 "ftpserver.user.admin.username=admin\n"+ 59 "ftpserver.user.admin.userpassword=bff4d7685c1475b68c016c478a728b6e\n"+ 60 "ftpserver.user.admin.homedirectory=/mnt/sdcard\n"+ 61 "ftpserver.user.admin.enableflag=true\n"+ 62 "ftpserver.user.admin.writepermission=true\n"+ 63 "ftpserver.user.admin.maxloginnumber=250\n"+ 64 "ftpserver.user.admin.maxloginperip=250\n"+ 65 "ftpserver.user.admin.idletime=300\n"+ 66 "ftpserver.user.admin.uploadrate=10000\n"+ 67 "ftpserver.user.admin.downloadrate=10000\n"; 68 69 fu.writeFile(str, path+"ftpserver.properties"); 70 71 File files=new File(path+"ftpserver.properties"); 72 73 PropertiesUserManagerFactory usermanagerfactory = new PropertiesUserManagerFactory(); 74 usermanagerfactory.setFile(files); 75 serverFactory.setUserManager(usermanagerfactory.createUserManager()); 76 77 } catch (IOException e1) { 78 // TODO Auto-generated catch block 79 e1.printStackTrace(); 80 } 81 82 83 84 85 86 serverFactory.addListener("default", factory.createListener()); // replace the default listener 87 if(ftpServer != null) 88 { 89 ftpServer.stop(); 90 } 91 92 FtpServer server = serverFactory.createServer(); 93 ftpServer = server; 94 // start the server 95 try { 96 server.start(); 97 } catch (FtpException e) { 98 // TODO Auto-generated catch block 99 e.printStackTrace(); 100 } 101 102 } 103 104 105 }
四、总结
1、大致过程就是:
①创建ftp服务器工厂
②创建ftp监听工厂
③设置监听端口(这里设置的是12345)
④创建ftp配置文件
1 String str = "" + 2 "ftpserver.user.admin.username=admin\n"+ 3 "ftpserver.user.admin.userpassword=bff4d7685c1475b68c016c478a728b6e\n"+ 4 "ftpserver.user.admin.homedirectory=/mnt/sdcard\n"+ 5 "ftpserver.user.admin.enableflag=true\n"+ 6 "ftpserver.user.admin.writepermission=true\n"+ 7 "ftpserver.user.admin.maxloginnumber=250\n"+ 8 "ftpserver.user.admin.maxloginperip=250\n"+ 9 "ftpserver.user.admin.idletime=300\n"+ 10 "ftpserver.user.admin.uploadrate=10000\n"+ 11 "ftpserver.user.admin.downloadrate=10000\n";
这里的配置为:用户名为admin 密码为经过md5加密的一串字符串(你可以自行设置,百度md5加密,自己找去吧),还有设置登录个、ip、上下行速度等
⑤将配置文件生效
⑥对ftp服务设置监听端口
⑦创建ftpServer
⑧启动服务
四、demo地址
http://pan.baidu.com/s/1qWA04Ja
五、如何判断ftp服务是否启动成功
一个很方便的工具:chrome,直接输入ftp://+你的ftp地址+:+端口 (举例:ftp://192.168.1.2:12345)