最近研究了一下在android端实现ftp server 功能,在网上搜了几个,没有能用的基本是各种抄袭,还是自己研究吧
首先到 apache官网下载ftp server 相关jar和配置文件,最新的是Apache FtpServer 1.0.6 Release版本
看一下效果图:
由于是apache已经将ftp server相关的实现封装的很好了,所以实现起来就简单多了
导入路径\apache-ftpserver-1.0.6\common\lib下相关jar包
主要的jar包文件
实现代码:
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("msg", 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 ={"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";//"/sdcard/users.properties";
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("default", 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+"ftpserver.jks"));
ssl.setKeystorePassword("password");
// set the SSL configuration for the listener
factory.setSslConfiguration(ssl.createSslConfiguration());
factory.setImplicitSsl(true);
// replace the default listener
serverFactory.addListener("default", factory.createListener());
PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory();
userManagerFactory.setFile(new File(ftpConfigDir+"users.properties"));
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
# "License"); 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
# "AS IS" 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 "admin"
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="android.permission.WRITE_EXTERNAL_STORAGE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE" >
</uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK" >
</uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" >
</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