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