Android Transfer photos to FTP server

Source: Internet
Author: User
Tags ftp login

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
  1. Package Com.photo;
  2. Import Java.io.File;
  3. Import Java.io.FileInputStream;
  4. Import java.io.FileNotFoundException;
  5. Import java.io.IOException;
  6. Import Java.io.InputStream;
  7. Import org.apache.commons.net.ftp.FTPClient;
  8. Import org.apache.commons.net.ftp.FTPReply;
  9. Public class Filetool {
  10. /** 
  11. * Description: Uploading files to FTP server
  12. *
  13. * @param URL
  14. * FTP server hostname
  15. * @param Port
  16. * FTP Server port
  17. * @param username
  18. * FTP login account
  19. * @param password
  20. * FTP Login Password
  21. * @param path
  22. * FTP Server Save directory, is the directory form under Linux, such as/photo/
  23. * @param filename
  24. * uploaded to the FTP server file name, is your own definition of the name,
  25. * @param input
  26. * Input Stream
  27. * @return returns TRUE, otherwise false
  28. */
  29. public Static boolean uploadfile (string URL, int port, string username,
  30. string password, string path, string filename, InputStream input) {
  31. Boolean success = false;
  32. ftpclient ftp = new FtpClient ();
  33. try {
  34. int reply;
  35. Ftp.connect (URL, port); //Connect FTP server
  36. //If using the default port, you can connect directly to the FTP server using Ftp.connect (URL)
  37. Ftp.login (username, password); //Login
  38. Reply = Ftp.getreplycode ();
  39. if (! Ftpreply.ispositivecompletion (Reply)) {
  40. Ftp.disconnect ();
  41. return success;
  42. }
  43. Ftp.changeworkingdirectory (path);
  44. Ftp.storefile (filename, input);
  45. Input.close ();
  46. Ftp.logout ();
  47. Success = true;
  48. } catch (IOException e) {
  49. E.printstacktrace ();
  50. } finally {
  51. if (ftp.isconnected ()) {
  52. try {
  53. Ftp.disconnect ();
  54. } catch (IOException IoE) {
  55. }
  56. }
  57. }
  58. return success;
  59. }
  60. //Test
  61. public static void Main (string[] args) {
  62. FileInputStream in = null;
  63. File dir = new file ("G://pathnew");
  64. File files[] = Dir.listfiles ();
  65. if (dir.isdirectory ()) {
  66. For (int i=0;i<files.length;i++) {
  67. try {
  68. in = new FileInputStream (Files[i]);
  69. Boolean flag = UploadFile ("17.8.119.77", + , "Android", "Android ",
  70. "/photo/", "412424123412341234_20130715120334_" + i + ". jpg", in);
  71. SYSTEM.OUT.PRINTLN (flag);
  72. } catch (FileNotFoundException e) {
  73. E.printstacktrace ();
  74. }
  75. }
  76. }
  77. }
  78. }

The above is the Java code, the following is the Android code.

[Java]View Plaincopy
  1. Package com.ftp;
  2. Import Java.io.File;
  3. Import Java.io.FileInputStream;
  4. Import java.io.FileNotFoundException;
  5. Import Android.os.Bundle;
  6. Import android.app.Activity;
  7. Import Android.util.Log;
  8. Import Android.view.Menu;
  9. Public class Mainactivity extends Activity {
  10. @Override
  11. protected void OnCreate (Bundle savedinstancestate) {
  12. super.oncreate (savedinstancestate);
  13. Setcontentview (R.layout.activity_main);
  14. new Uploadthread (). Start ();
  15. }
  16. class Uploadthread extends Thread {
  17. @Override
  18. public Void Run () {
  19. FileInputStream in = null;
  20. File dir = new file ("/mnt/sdcard/dcim/camera/test/");
  21. File files[] = Dir.listfiles ();
  22. if (dir.isdirectory ()) {
  23. For (int i=0;i<files.length;i++) {
  24. try {
  25. in = new FileInputStream (Files[i]);
  26. Boolean flag = Filetool.uploadfile ("17.8.119.77", + , "Android", "Android ",
  27. " /", "412424123412341234_20130715120334_" + i + ". jpg", in);
  28. SYSTEM.OUT.PRINTLN (flag);
  29. } catch (FileNotFoundException e) {
  30. E.printstacktrace ();
  31. }
  32. }
  33. }
  34. }
  35. }
  36. }


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

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.