Android is based on Apache FTP Server

Source: Internet
Author: User

I recently studied how to implement the FTP server function on the android client. I searched a few on the Internet and found that I could not use a variety of plagiarism. I 'd better study it myself.

First download the FTP server jar and configuration files from the Apache official website. The latest version is Apache ftpserver 1.0.6 release.

Take a look:

Because Apache has encapsulated FTP server-related implementations, it is much easier to implement them.

Import path \ apache-ftpserver-1.0.6 \ common \ Lib related jar package

Main jar package files

Implementation Code:

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. f Tplet. 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 ext Ends activity {private ftpserver mftpserver; private string ftpconfigdir = environment. getexternalstoragedirectory (). getabsolutepath () + "/ftpconfig/"; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); textview TV = (textview) findviewbyid (R. id. tvtext); string info = "please visit the following address \ n" + "ftp: //" + getlocalipaddress () + ": 2221 \ n" through a browser or my computer "; TV. sette XT (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 = 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 Te [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", ftpconfigdi R}; try {process PS = runtime.getruntime(cmd.exe C (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 T He port of the listenerfactory. setport (2221); // Replace the default listenerserverfactory. addlistener ("default", factory. createlistener (); // start the serverftpserver 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 defau Lt listener. ftpserverfactory serverfactory = new ftpserverfactory (); listenerfactory factory = new listenerfactory (); // set the port of the listenerfactory. setport (2221); // define SSL configurationsslconfigurationfactory SSL = new sslconfigurationfactory (); SSL. setkeystorefile (new file (ftpconfigdir + "ftpserver. jks "); SSL. setkeystorepassword ("password"); // set the SSL configuration for the listenerf Acloud. setsslconfiguration (SSL. createsslconfiguration (); factory. setimplicitssl (true); // Replace the default listenerserverfactory. addlistener ("default", factory. createlistener (); propertiesusermanagerfactory usermanagerfactory = new propertiesusermanagerfactory (); usermanagerfactory. setfile (new file (ftpconfigdir + "users. properties "); serverfactory. setusermanager (usermanagerfactory. createusermanag Er (); // start the serverftpserver Server = serverfactory. createserver (); this. mftpserver = server; try {server. start ();} catch (ftpexception e) {e. printstacktrace () ;}@ overrideprotected void ondestroy () {super. ondestroy (); If (null! = Mftpserver) {mftpserver. Stop (); mftpserver = NULL ;}}}

The most important step is to modify the configuration file.

Configuration file directory: apache-ftpserver-1.0.6 \ res \ conf

Here we mainly use the users. properties configuration file. The modification content is as follows:

# 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=21232F297A57A5A743894A0E4A801FC3ftpserver.user.admin.homedirectory=/mnt/sdcardftpserver.user.admin.enableflag=trueftpserver.user.admin.writepermission=trueftpserver.user.admin.maxloginnumber=0ftpserver.user.admin.maxloginperip=0ftpserver.user.admin.idletime=0ftpserver.user.admin.uploadrate=0ftpserver.user.admin.downloadrate=0ftpserver.user.anonymous.userpassword=adminftpserver.user.anonymous.homedirectory=/mnt/sdcardftpserver.user.anonymous.enableflag=trueftpserver.user.anonymous.writepermission=trueftpserver.user.anonymous.maxloginnumber=20ftpserver.user.anonymous.maxloginperip=2ftpserver.user.anonymous.idletime=300ftpserver.user.anonymous.uploadrate=4800ftpserver.user.anonymous.downloadrate=4800

Finally, do not forget the related permissions:

<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. In this way, the FTP server function on the android end is simply completed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.