Android uses ApacheFtpServer to implement Ftp Server

Source: Internet
Author: User

To facilitate file transfer and management between Android phones and PCs, we wanted to create an FTP server on Android for file access and management. From the Internet, I found that the FtpServer of Apache has a java version, so it is very convenient.
1. Import Apache FtpServer dependent jar package
Commons-net-ftp-2.0.jar
Ftpserver-core-1.0.6.jar
Log4j-1.2.14.jar
Mina-core-2.0.4.jar
Slf4j-api-1.5.2.jar
Slf4j-log4j12-1.5.2.jar
2. Sample Code for Android to start FtpServer
Package com. orgcent. ftp;

Import org. apache. ftpserver. FtpServer;
Import org. apache. ftpserver. FtpServerFactory;
Import org. apache. ftpserver. ftplet. FtpException;
Import org. apache. ftpserver. listener. ListenerFactory;

Import android. app. Activity;
Import android. OS. Bundle;

Public class FtpServerActivity extends Activity {

Static {// due to the Android version, some ipv6 support bugs exist. For exceptions, see the introduction at the bottom of the article.
System. setProperty ("java.net. preferIPv6Addresses", "false ");
}

Private FtpServer mFtpServer;

@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );

// Try {
// InputStream is = getResources (). getAssets (). open ("log4j. properties ");
// Properties prop = new Properties ();
// Prop. load (is );
//
// File file = new File (getDir ("log", MODE_WORLD_WRITEABLE), "ftp. log ");
// Prop. setProperty ("log4j. appender. R. File", file. getAbsolutePath ());
// PropertyConfigurator. configure (prop );
//} Catch (IOException e ){
// E. printStackTrace ();
//}

StartFtpServer ();
}

Private void startFtpServer (){
FtpServerFactory serverFactory = new FtpServerFactory ();

ListenerFactory factory = new ListenerFactory ();

// Set the port of the listener
Int port = 2221;
Factory. setPort (port );

// 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 ();
}

}

@ Override
Protected void onDestroy (){
Super. onDestroy ();

If (null! = MFtpServer ){
MFtpServer. stop ();
MFtpServer = null;
}
}
}
The above configuration basically realizes the establishment of FtpServer on Android. For IPV6 support bugs, you can view and solve the android network exception java.net. SocketException: Bad address family.
Demo: http://code.google.com/p/android-custom-view/downloads/list

 


From the blog of radish Cabbage

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.