1. Preface
During development, you will encounter the need to upload files to the FTP server. You must first import
commons-net-3. Then use the API to perform related operations, the specific functions are as follows:
Ftp related code
import ; import ; import ; import ; import ; public class FTPClientUtils { private static final String TAG = "MainActivity"; private FTPClient ftpClient = null; // FTP client /** * Connect to the FTP server * * @param host ftp server domain name * @param username Access username * @param password access password * @param port * @return Whether the connection is successful */ public boolean ftpConnect(String host, String username, String password, int port) { try { ftpClient = new FTPClient(); (host,port); // Based on the returned status code, determine whether the link is successfully established if ((())) { boolean status = (username, password); /* * Set file transfer mode * To avoid some possible problems, you must set the file transfer format here. * Here we use BINARY_FILE_TYPE to transfer text, images and compressed files. */ (FTP.BINARY_FILE_TYPE); (); return status; } } catch (Exception e) { (); } return false; } /** * Disconnect the ftp server * * @return Disconnect result */ public boolean ftpDisconnect() { //Judge empty pointer if (ftpClient == null) { return true; } // Disconnect the ftp server try { (); (); return true; } catch (Exception e) { (); } return false; } /** * ftp file upload * * @param srcFilePath Source file directory * @param desFileName File name * @return File upload result */ public boolean ftpUpload(String srcFilePath, String desFileName) { boolean status = false; try { FileInputStream srcFileStream = new FileInputStream(srcFilePath); status = (desFileName, srcFileStream); (); return status; } catch (Exception e) { (); } return status; } /** * ftp Change directory * * @param path Change path * @return Whether the change is successful */ public boolean ftpChangePath(String path) { boolean status = false; try { status = (path); } catch (Exception e) { (); } return status; } }
2. Call API
boolean isConnect = ("Server host", "username", "password", 21);//The default port number is 21 if (isConnect) { boolean isSuccessful = ("/sdcard/" + folderName + "/" + mPicturename, "/htdocs/pics/" + mPicturename); if (isSuccessful) { (); //Upload successfully } else { //Upload failed } } else { //The server connection failed }
appendix: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 (); } } }
Summarize
This is the article about the Android FTP server upload file strategy. For more relevant Android FTP server upload content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!