SoFunction
Updated on 2025-04-10

Implementation code for android transferring photos to FTP server

This article has shared the specific code for android to send photos to FTP server for your reference. The specific content is as follows

It can be used in the Android environment and in the Java environment. I first implemented the functions in the Java environment, and then ported them to an Android phone. Everything else is the same.

package ;
 
import ;
import ;
import ;
import ;
import ;
 
import ;
import ;
 
public class FileTool {
 
 /**
  * Description: Upload files to the FTP server
  *
  * @param url
  * FTP server hostname
  * @param port
  * FTP server port
  * @param username
  * FTP login account
  * @param password
  * FTP login password
  * @param path
  * The FTP server saves the directory, which is the directory form under Linux, such as /photo/
  * @param filename
  * The file name uploaded to the FTP server is the name you defined.
  * @param input
  * Input Stream
  * @return Return true successfully, otherwise return 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;
  (url, port);// Connect to the FTP server  // If the default port is used, you can directly connect to the FTP server using (url) method  (username, password);//Log in  reply = ();
  if (!(reply)) {
  ();
  return success;
  }
  (path);
  (filename, input);
 
  ();
  ();
  success = true;
 } catch (IOException e) {
  ();
 } finally {
  if (()) {
  try {
   ();
  } catch (IOException ioe) {
  }
  }
 }
 return success;
 }
 
 // test public static void main(String[] args) {
 
 FileInputStream in = null ;
 File dir = new File("G://pathnew");
 File files[] = ();
 if(()) {
  for(int i=0;i<;i++) {
  try {
   in = new FileInputStream(files[i]);
   boolean flag = uploadFile("17.8.119.77", 21, "android", "android",
    "/photo/", "412424123412341234_20130715120334_" + i + ".jpg", in);
   (flag);
  } catch (FileNotFoundException e) {
   ();
  }
  }
 }
 
 }
}

The above is java code, and the following is android code.

package ;
 
import ;
import ;
import ;
 
import ;
import ;
import ;
import ;
 
public class MainActivity extends Activity {
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 (savedInstanceState);
 setContentView(.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[] = ();
  if(()) {
  for(int i=0;i<;i++) {
   try {
    in = new FileInputStream(files[i]);
   boolean flag = ("17.8.119.77", 21, "android", "android",
    "/", "412424123412341234_20130715120334_" + i + ".jpg", in);
   (flag);
   } catch (FileNotFoundException e) {
   ();
   }
  }
  }
 }
 }
}

After my test, it can run normally and is for reference only. Please contact me if you have any questions.

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.