设为首页 收藏本站
查看: 759|回复: 0

[经验分享] android 基于apache ftp server

[复制链接]
发表于 2015-11-6 07:51:11 | 显示全部楼层 |阅读模式
  最近研究了一下在android端实现ftp server 功能,在网上搜了几个,没有能用的基本是各种抄袭,还是自己研究吧
  首先到 apache官网下载ftp server 相关jar和配置文件,最新的是Apache FtpServer 1.0.6 Release版本
  


  看一下效果图:
DSC0000.png


  


DSC0001.png


  


  由于是apache已经将ftp server相关的实现封装的很好了,所以实现起来就简单多了
  导入路径\apache-ftpserver-1.0.6\common\lib下相关jar包
  主要的jar包文件
DSC0002.png


  


  实现代码:
  


  package com.orgcent.ftp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
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.ssl.SslConfigurationFactory;
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
public class FtpServerActivity extends Activity {
private FtpServer mFtpServer;
private String ftpConfigDir= Environment.getExternalStorageDirectory().getAbsolutePath()+"/ftpConfig/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.tvText);
String info="请通过浏览器或者我的电脑访问以下地址\n"+"ftp://"+getLocalIpAddress()+":2221\n";
tv.setText(info);
File f=new File(ftpConfigDir);
if(!f.exists())
f.mkdir();
copyResourceFile(R.raw.users, ftpConfigDir+"users.properties");
Config1();
}
public String getLocalIpAddress() {
String strIP=null;
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
strIP= inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(&quot;msg&quot;, ex.toString());
}
return strIP;
}
private void copyResourceFile(int rid, String targetFile){
InputStream fin = ((Context)this).getResources().openRawResource(rid);
FileOutputStream fos=null;
int length;
try {
fos = new FileOutputStream(targetFile);
byte[] buffer = new byte[1024];
while( (length = fin.read(buffer)) != -1){
fos.write(buffer,0,length);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fin!=null){
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
void Config1(){
//Now, let's configure the port on which the default listener waits for connections.
FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
String[] str ={&quot;mkdir&quot;, ftpConfigDir};
try {
Process ps = Runtime.getRuntime().exec(str);
try {
ps.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
String filename=ftpConfigDir+&quot;users.properties&quot;;//&quot;/sdcard/users.properties&quot;;
File files=new File(filename);
userManagerFactory.setFile(files);
serverFactory.setUserManager(userManagerFactory.createUserManager());
// set the port of the listener
factory.setPort(2221);
// replace the default listener
serverFactory.addListener(&quot;default&quot;, factory.createListener());
// start the server
FtpServer server = serverFactory.createServer();
this.mFtpServer = server;      
try {
server.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
void Config2(){
//Now, let's make it possible for a client to use FTPS (FTP over SSL) for the default listener.

FtpServerFactory serverFactory = new FtpServerFactory();
ListenerFactory factory = new ListenerFactory();
// set the port of the listener
factory.setPort(2221);
// define SSL configuration
SslConfigurationFactory ssl = new SslConfigurationFactory();
ssl.setKeystoreFile(new File(ftpConfigDir+&quot;ftpserver.jks&quot;));
ssl.setKeystorePassword(&quot;password&quot;);
// set the SSL configuration for the listener
factory.setSslConfiguration(ssl.createSslConfiguration());
factory.setImplicitSsl(true);
// replace the default listener
serverFactory.addListener(&quot;default&quot;, factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File(ftpConfigDir+&quot;users.properties&quot;));
serverFactory.setUserManager(userManagerFactory.createUserManager());
// start the server
FtpServer server = serverFactory.createServer();
this.mFtpServer = server;  
try {
server.start();
} catch (FtpException e) {
e.printStackTrace();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if(null != mFtpServer) {
mFtpServer.stop();
mFtpServer = null;
}
}
}




  最重要一步,配置文件修改
  配置文件所在目录:apache-ftpserver-1.0.6\res\conf
  这里我们主要使用users.properties配置文件,修改内容如下


  

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# &quot;License&quot;); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#  http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# &quot;AS IS&quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
# Password is &quot;admin&quot;
ftpserver.user.admin.userpassword=21232F297A57A5A743894A0E4A801FC3
ftpserver.user.admin.homedirectory=/mnt/sdcard
ftpserver.user.admin.enableflag=true
ftpserver.user.admin.writepermission=true
ftpserver.user.admin.maxloginnumber=0
ftpserver.user.admin.maxloginperip=0
ftpserver.user.admin.idletime=0
ftpserver.user.admin.uploadrate=0
ftpserver.user.admin.downloadrate=0
ftpserver.user.anonymous.userpassword=admin
ftpserver.user.anonymous.homedirectory=/mnt/sdcard
ftpserver.user.anonymous.enableflag=true
ftpserver.user.anonymous.writepermission=true
ftpserver.user.anonymous.maxloginnumber=20
ftpserver.user.anonymous.maxloginperip=2
ftpserver.user.anonymous.idletime=300
ftpserver.user.anonymous.uploadrate=4800
ftpserver.user.anonymous.downloadrate=4800


  

最后别忘了相关权限:  
  

<uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; >
</uses-permission>
<uses-permission android:name=&quot;android.permission.INTERNET&quot; >
</uses-permission>
<uses-permission android:name=&quot;android.permission.ACCESS_WIFI_STATE&quot; >
</uses-permission>
<uses-permission android:name=&quot;android.permission.READ_PHONE_STATE&quot; >
</uses-permission>
<uses-permission android:name=&quot;android.permission.WAKE_LOCK&quot; >
</uses-permission>
<uses-permission android:name=&quot;android.permission.CHANGE_WIFI_STATE&quot; >
</uses-permission>

ok,这样简单的就完成了android端ftp server 服务器功能  
  


  


  


  


  


  


  



版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-135608-1-1.html 上篇帖子: 如何实现自动ftp上传功能:expect 下篇帖子: python 利用ftplib模块 实现ftp上传下载代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表