Can be used in the Android environment, in the Java environment can also be used, I first in the Java environment to achieve the function, and then transplanted to the Android phone, the others are the same.
[Java]View Plaincopy
- 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: Uploading files to FTP server
- *
- * @param URL
- * FTP server hostname
- * @param Port
- * FTP Server port
- * @param username
- * FTP login account
- * @param password
- * FTP Login Password
- * @param path
- * FTP Server Save directory, is the directory form under Linux, such as/photo/
- * @param filename
- * uploaded to the FTP server file name, is your own definition of the name,
- * @param input
- * Input Stream
- * @return returns TRUE, 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 FTP server
- //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
- Ftp.login (username, password); //Login
- 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", + , "Android", "Android ",
- "/photo/", "412424123412341234_20130715120334_" + i + ". jpg", in);
- SYSTEM.OUT.PRINTLN (flag);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- }
- }
- }
- }
The above is the Java code, the following is the Android code.
[Java]View Plaincopy
- 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 {
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- new Uploadthread (). Start ();
- }
- class Uploadthread extends Thread {
- @Override
- public 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", + , "Android", "Android ",
- " /", "412424123412341234_20130715120334_" + i + ". jpg", in);
- SYSTEM.OUT.PRINTLN (flag);
- } catch (FileNotFoundException e) {
- E.printstacktrace ();
- }
- }
- }
- }
- }
- }
After I test pass, can run normally, for reference only, if have the question please contact me.
http://blog.csdn.net/liuzhidong123/article/details/9341269