Android Note: FTP File Upload

Source: Internet
Author: User
Tags ftp connection ftp file ftp client

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
 

Related Article

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.