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:

/*** Upload a file through FTP * @ Param url ftp server address, for example, 192.168.1.110 * @ Param port, for example: 21 * @ Param username * @ Param Password * @ Param remotepath: Path to the FTP server disk * @ Param filenamepath: Path of the file to be uploaded * @ Param filename: name of the file to be uploaded *@ return */Public String ftpupload (string URL, string port, string username, string password, string remotepath, string filenamepath, string filename) {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 login is successful, ftpclient. makedirectory (remotepath); // sets the upload directory ftpclient. changeworkingdirectory (remotepath); ftpclient. setbuffer Size (1024); ftpclient. setcontrolencoding ("UTF-8"); ftpclient. enterlocalpassivemode (); FCM = new fileinputstream (filenamepath + filename); ftpclient. storefile (filename, FS); returnmessage = "1"; // uploaded successfully} 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 ;}

Jar package address: http://download.csdn.net/detail/baolong47/4096104

 

Turn: http://blog.csdn.net/baolong47/article/details/7301394

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.