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.