SoFunction
Updated on 2025-04-05

Android details about FTP file upload and download functions

This article shares the specific code for Android nine-grid picture display for your reference. The specific content is as follows

This blog is a compilation of articles for everyone to learn.

1. First download the commons-net jar package, which can be downloaded on Baidu.

FTP file upload and download tool classes:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
public class FTPManager { 
 FTPClient ftpClient = null; 
 
 public FTPManager() { 
  ftpClient = new FTPClient(); 
 } 
 
 // Connect to the ftp server public synchronized boolean connect() throws Exception { 
  boolean bool = false; 
  if (()) {//Judge whether you have logged in   (); 
  } 
  (20000);//Set the connection timeout time  ("utf-8"); 
  ("IP address", port); 
  if ((())) { 
   if (("username", "password")) { 
    bool = true; 
    ("FTP connection is successful"); 
   } 
  } 
  return bool; 
 } 
 
 // Create folder public boolean createDirectory(String path) throws Exception { 
  boolean bool = false; 
  String directory = (0, ("/") + 1); 
  int start = 0; 
  int end = 0; 
  if (("/")) { 
   start = 1; 
  } 
  end = ("/", start); 
  while (true) { 
   String subDirectory = (start, end); 
   if (!(subDirectory)) { 
    (subDirectory); 
    (subDirectory); 
    bool = true; 
   } 
   start = end + 1; 
   end = ("/", start); 
   if (end == -1) { 
    break; 
   } 
  } 
  return bool; 
 } 
 
 // Implement the function of uploading files public synchronized boolean uploadFile(String localPath, String serverPath) 
   throws Exception { 
  // Before uploading the file, first determine whether the local file exists  File localFile = new File(localPath); 
  if (!()) { 
   ("The local file does not exist"); 
   return false; 
  } 
  ("The local file exists with name:" + ()); 
  createDirectory(serverPath); // If the folder does not exist, create a folder  ("Server file storage path:" + serverPath + ()); 
  String fileName = (); 
  // If the local file exists and the server file is also present, upload the file. This method also includes breakpoint upload  long localSize = (); //Length of local file  FTPFile[] files = (fileName); 
  long serverSize = 0; 
  if ( == 0) { 
   ("The server file does not exist"); 
   serverSize = 0; 
  } else { 
   serverSize = files[0].getSize(); // Length of server file  } 
  if (localSize <= serverSize) { 
   if ((fileName)) { 
    ("The server file exists, delete the file, and start re-uploading"); 
    serverSize = 0; 
   } 
  } 
  RandomAccessFile raf = new RandomAccessFile(localFile, "r"); 
  // Progress  long step = localSize / 100; 
  long process = 0; 
  long currentSize = 0; 
  // OK, officially start uploading files  (); 
  (FTP.BINARY_FILE_TYPE); 
  (serverSize); 
  (serverSize); 
  OutputStream output = (fileName); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = (b)) != -1) { 
   (b, 0, length); 
   currentSize = currentSize + length; 
   if (currentSize / step != process) { 
    process = currentSize / step; 
    if (process % 10 == 0) { 
     ("Upload progress:" + process); 
    } 
   } 
  } 
  (); 
  (); 
  (); 
  if (()) { 
   ("File upload successfully"); 
   return true; 
  } else { 
   ("File upload failed"); 
   return false; 
  } 
 } 
 
 // Implement the function of downloading files, which can realize breakpoint downloading public synchronized boolean downloadFile(String localPath, String serverPath) 
   throws Exception { 
  // First determine whether the server file exists  FTPFile[] files = (serverPath); 
  if ( == 0) { 
   ("The server file does not exist"); 
   return false; 
  } 
  ("The remote file exists, with the name:" + files[0].getName()); 
  localPath = localPath + files[0].getName(); 
  // Then determine whether the downloaded file can be downloaded after breaking the point  long serverSize = files[0].getSize(); // Get the length of the remote file  File localFile = new File(localPath); 
  long localSize = 0; 
  if (()) { 
   localSize = (); // If the local file exists, get the length of the local file   if (localSize >= serverSize) { 
    ("The file has been downloaded"); 
    File file = new File(localPath); 
    (); 
    ("The local file exists, deletes successfully, and re-downloading begins"); 
    return false; 
   } 
  } 
  // Progress  long step = serverSize / 100; 
  long process = 0; 
  long currentSize = 0; 
  // Start preparing to download files  (); 
  (FTP.BINARY_FILE_TYPE); 
  OutputStream out = new FileOutputStream(localFile, true); 
  (localSize); 
  InputStream input = (serverPath); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = (b)) != -1) { 
   (b, 0, length); 
   currentSize = currentSize + length; 
   if (currentSize / step != process) { 
    process = currentSize / step; 
    if (process % 10 == 0) { 
     ("Download progress:" + process); 
    } 
   } 
  } 
  (); 
  (); 
  (); 
  // This method is to ensure that the stream is processed. If this method is not available, the current program may die.  if (()) { 
   ("File download successfully"); 
   return true; 
  } else { 
   ("File download failed"); 
   return false; 
  } 
 } 
 
 // If ftp upload is opened, close it public void closeFTP() throws Exception { 
  if (()) { 
   (); 
  } 
 } 
} 

The specific implementation is written in detail based on the code comments.

1. FTP file upload code in Android:

// Upload exampleprivate void ftpUpload() { 
 new Thread() { 
 public void run() { 
  try { 
  ("Connecting to the ftp server..."); 
  FTPManager ftpManager = new FTPManager(); 
  if (()) { 
  if (( + "", "mnt/sdcard/")) { 
  (); 
  } 
  } 
  } catch (Exception e) { 
  // TODO: handle exception 
  // (()); 
  } 
 } 
 }.start(); 
 } 

2. Download code of FTP file in Android:

// Download exampleprivate void ftpDownload() { 
 new Thread() { 
 public void run() { 
  try { 
  ("Connecting to the ftp server..."); 
  FTPManager ftpManager = new FTPManager(); 
  if (()) { 
  if ((, "20120723_XFQ07_XZMarketPlatform.db")) { 
  (); 
  } 
  } 
  } catch (Exception e) { 
  // TODO: handle exception 
  // (()); 
  } 
 } 
 }.start(); 
 } 

The FTP upload code I wrote when I was working on the project before:

package ; 
 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
 
import ; 
import ; 
import ; 
 
import ; 
 
import ; 
import ; 
 
public class MyUploadThread extends Thread { 
 private String fileName;// File name private String filePath;// File local path private String fileStoragePath;// File server storage path private String serverAddress;// Server address private String ftpUserName;// ftp account private String ftpPassword;// ftp password private Context mContext; 
 public MyUploadThread() { 
  super(); 
  // TODO Auto-generated constructor stub 
 } 
 
 public MyUploadThread(Context mContext,String fileName, String filePath, 
    String fileStoragePath,String serverAddress,String ftpUserName,String ftpPassword) { 
  super(); 
   = fileName; 
   = filePath; 
   = fileStoragePath; 
   = serverAddress; 
   = ftpUserName; 
   = ftpPassword; 
  =mContext; 
 } 
 
 @Override 
 public void run() { 
  (); 
  try { 
   FileInputStream fis=null; 
   FTPClient ftpClient = new FTPClient(); 
   String[] idPort = (":"); 
   (idPort[0], (idPort[1])); 
   int returnCode = (); 
   ("caohai", "returnCode,upload:"+returnCode); 
   boolean loginResult = (ftpUserName, ftpPassword); 
   ("caohai", "loginResult:"+loginResult); 
   if (loginResult && (returnCode)) {// If login is successful     
    // Set up the upload directory     
    if (((SysApplication) mContext).getIsVideo()) { 
     ((SysApplication) mContext).setIsVideo(false); 
     boolean ff=(fileStoragePath + "/video/"); 
     ("caohai", "ff:"+ff); 
    }else{ 
    boolean ee=(fileStoragePath + "/photo/"); 
    ("caohai", "ee:"+ee); 
    } 
    (1024); 
    // ("iso-8859-1"); 
    // (); 
    (FTP.BINARY_FILE_TYPE); 
     fis = new FileInputStream(filePath + "/" 
      + fileName); 
     ("caohai", "fileStoragePath00000:"+fileStoragePath); 
    String[] path = ("visitorRecord"); 
     
    boolean fs = (new String((path[1] 
      + "/photo/" + fileName).getBytes(), "iso-8859-1"), fis); 
    ("caohai", "shifoushangchuanchenggong:"+fs); 
    (); 
    (); 
    //(); 
   } else {// If login fails    (); 
   } 
  } catch (NumberFormatException e) { 
   // TODO Auto-generated catch block 
   (); 
  } catch (SocketException e) { 
   // TODO Auto-generated catch block 
   (); 
  } catch (FileNotFoundException e) { 
   // TODO Auto-generated catch block 
   (); 
  } catch (UnsupportedEncodingException e) { 
   // TODO Auto-generated catch block 
   (); 
  } catch (IOException e) { 
   // TODO Auto-generated catch block 
   (); 
  } 
 
 } 
} 

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.