Android sends photos to FTP Server

Source: Internet
Author: User

It can be used in the android environment, or in the Java environment. I first implemented the function in the Java environment and then transplanted it to the Android mobile phone. The rest are the same.

Package COM. photo; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import Java. io. ioexception; import Java. io. inputstream; import org.apache.commons.net. FTP. ftpclient; import org.apache.commons.net. FTP. ftpreply; public class filetool {/*** description: upload a file to the FTP server ** @ Param URL * FTP Server Hostname * @ Param port * FTP server port * @ Param username * FTP login account * @ Param passwo Rd * FTP login password * @ Param path * FTP server storage directory, which is a directory in Linux, such as/Photo/* @ Param filename * file name uploaded to the FTP server, is your own name, * @ Param input * input stream * @ return returns true if the input stream is successful, otherwise false */public static Boolean uploadfile (string URL, int port, string username, string password, string path, string filename, inputstream input) {Boolean success = false; ftpclient FTP = new ftpclient (); try {int reply; FTP. connect (URL, Port); // connect to the FTP server Server // if you use the default port, you can use ftp. connect (URL) directly to the FTP Server FTP. login (username, password); // log on to reply = FTP. getreplycode (); If (! Ftpreply. ispositivecompletion (reply) {FTP. disconnect (); Return success;} FTP. changeworkingdirectory (PATH); FTP. storefile (filename, input); input. close (); FTP. logout (); success = true;} catch (ioexception e) {e. printstacktrace ();} finally {If (FTP. isconnected () {try {FTP. disconnect () ;}catch (ioexception IOE) {}} return success;} // test public static void main (string [] ARGs) {fileinputstream in = NULL; file dir = new file ("G: // pathnew"); file files [] = dir. listfiles (); If (dir. isdirectory () {for (INT I = 0; I <files. length; I ++) {try {In = new fileinputstream (files [I]); Boolean flag = uploadfile ("17.8.119.77", 21, "android", "android ", "/Photo/", "412316123412341234_20130715120334 _" + I + ". jpg ", in); system. out. println (FLAG);} catch (filenotfoundexception e) {e. printstacktrace ();}}}}}

The above is JavaCodeThe following is the android code.

Package COM. FTP; import Java. io. file; import Java. io. fileinputstream; import Java. io. filenotfoundexception; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; public class mainactivity extends activity {@ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); New uploadthread (). start ();} class uploadthread extends thread {@ overridepublic void run () {fileinputstream in = NULL; file dir = new file ("/mnt/sdcard/dcim/camera/test/"); file files [] = dir. listfiles (); If (dir. isdirectory () {for (INT I = 0; I <files. length; I ++) {try {In = new fileinputstream (files [I]); Boolean flag = filetool. uploadfile ("17.8.119.77", 21, "android", "android", "/", "412316123412341234_20130715120334 _" + I + ". jpg ", in); system. out. println (FLAG);} catch (filenotfoundexception e) {e. printstacktrace ();}}}}}}

I have passed the test and can run normally. It is for your reference only. If you have any questions, please contact me.

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.