It is not difficult to upload FTP files (including images) on the android client. Write it down to record it. Hope it will help new users.
Need to use commons-net-3.0.1.jar, followed by a jar package.
Directly run the Code:
[Java]
/**
* Uploading files through ftp
* @ Param url the ftp server address, for example, 192.168.1.110
* @ Param port: port 21
* @ Param username: Login Name
* @ Param password
* @ Param remotePath: disk path to the ftp server
* @ Param fileNamePath: Path of the file to be uploaded
* @ Param fileName the name of the file to be uploaded
* @ Return
* // Www.2cto.com
Public String ftpUpload (String url, String port, String username, String password, String remotePath, String fileNamePath, String fileName ){
FTPClient ftpClient = new FTPClient ();
FileInputStream FCM = null;
String returnMessage = "0 ";
Try {
FtpClient. connect (url, Integer. parseInt (port ));
Boolean loginResult = ftpClient. login (username, password );
Int returnCode = ftpClient. getReplyCode ();
If (loginResult & FTPReply. isPositiveCompletion (returnCode) {// if the logon is successful
FtpClient. makeDirectory (remotePath );
// Set the upload directory
FtpClient. changeWorkingDirectory (remotePath );
FtpClient. setBufferSize (1024 );
FtpClient. setControlEncoding ("UTF-8 ");
FtpClient. enterLocalPassiveMode ();
FS = new FileInputStream (fileNamePath + fileName );
FtpClient. storeFile (fileName, ISI );
ReturnMessage = "1"; // upload successful
} Else {// If logon fails
ReturnMessage = "0 ";
}
} Catch (IOException e ){
E. printStackTrace ();
Throw new RuntimeException ("FTP client error! ", E );
} Finally {
// IOUtils. closeQuietly (FCM );
Try {
FtpClient. disconnect ();
} Catch (IOException e ){
E. printStackTrace ();
Throw new RuntimeException ("an exception occurred when closing the FTP connection! ", E );
}
}
Return returnMessage;
}
From Scott's column