首页 技术 正文
技术 2022年11月22日
0 收藏 369 点赞 4,435 浏览 3331 个字

一、为啥

在android设备跑ftp服务,在现场方便查看日志,目前就是这么用的。

二、前提:

从apache的官网下载依赖包:http://mina.apache.org/ftpserver-project/download_1.0.6.html

解压后如下:
26、android上跑apache的ftp服务

在最右侧的jar包列表中,并不需要全部导入我们的工程,需要导入的包为:
26、android上跑apache的ftp服务

记得把jar包添加到buildPath,同时在order and export选项选中上步添加的jar包

三、如何用:

 package com.example.zzk; import java.io.File;
import java.io.IOException; import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends Activity { public static String path = Environment.getExternalStorageDirectory()+File.separator+"FTP_TEST"+ File.separator;
public Context ctxt;
public String tag = "zzk";
public static FtpServer ftpServer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); ctxt = getApplicationContext();
ftpServiceStart(ctxt);
} /**
*
* 启动ftp服务
*/
public static void ftpServiceStart(Context ctxt)
{
FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); factory.setPort(12345); // set the port of the listener FIleUtils fu = new FIleUtils(ctxt);
try {
File file = new File(path);
if(!file.isDirectory())
{
file.mkdir();
}
fu.createFile(path+"ftpserver.properties");
String str = "" +
"ftpserver.user.admin.username=admin\n"+
"ftpserver.user.admin.userpassword=bff4d7685c1475b68c016c478a728b6e\n"+
"ftpserver.user.admin.homedirectory=/mnt/sdcard\n"+
"ftpserver.user.admin.enableflag=true\n"+
"ftpserver.user.admin.writepermission=true\n"+
"ftpserver.user.admin.maxloginnumber=250\n"+
"ftpserver.user.admin.maxloginperip=250\n"+
"ftpserver.user.admin.idletime=300\n"+
"ftpserver.user.admin.uploadrate=10000\n"+
"ftpserver.user.admin.downloadrate=10000\n"; fu.writeFile(str, path+"ftpserver.properties"); File files=new File(path+"ftpserver.properties"); PropertiesUserManagerFactory usermanagerfactory = new PropertiesUserManagerFactory();
usermanagerfactory.setFile(files);
serverFactory.setUserManager(usermanagerfactory.createUserManager()); } catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} serverFactory.addListener("default", factory.createListener()); // replace the default listener
if(ftpServer != null)
{
ftpServer.stop();
} FtpServer server = serverFactory.createServer();
ftpServer = server;
// start the server
try {
server.start();
} catch (FtpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }

四、总结

1、大致过程就是:

①创建ftp服务器工厂

②创建ftp监听工厂

③设置监听端口(这里设置的是12345)

④创建ftp配置文件

 String str = "" +
"ftpserver.user.admin.username=admin\n"+
"ftpserver.user.admin.userpassword=bff4d7685c1475b68c016c478a728b6e\n"+
"ftpserver.user.admin.homedirectory=/mnt/sdcard\n"+
"ftpserver.user.admin.enableflag=true\n"+
"ftpserver.user.admin.writepermission=true\n"+
"ftpserver.user.admin.maxloginnumber=250\n"+
"ftpserver.user.admin.maxloginperip=250\n"+
"ftpserver.user.admin.idletime=300\n"+
"ftpserver.user.admin.uploadrate=10000\n"+
"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)

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838