您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關Android FTP服務器上傳文件如何實現?,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
Ftp相關代碼
import android.util.Log; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import java.io.FileInputStream; public class FTPClientUtils { private static final String TAG = "MainActivity"; private FTPClient ftpClient = null; // FTP客戶端 /** * 連接到FTP服務器 * * @param host ftp服務器域名 * @param username 訪問用戶名 * @param password 訪問密碼 * @param port 端口 * @return 是否連接成功 */ public boolean ftpConnect(String host, String username, String password, int port) { try { ftpClient = new FTPClient(); ftpClient.connect(host,port); // 根據返回的狀態碼,判斷鏈接是否建立成功 if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { boolean status = ftpClient.login(username, password); /* * 設置文件傳輸模式 * 避免一些可能會出現的問題,在這里必須要設定文件的傳輸格式。 * 在這里我們使用BINARY_FILE_TYPE來傳輸文本、圖像和壓縮文件。 */ ftpClient.setFileType(FTP.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); return status; } } catch (Exception e) { e.printStackTrace(); } return false; } /** * 斷開ftp服務器連接 * * @return 斷開結果 */ public boolean ftpDisconnect() { // 判斷空指針 if (ftpClient == null) { return true; } // 斷開ftp服務器連接 try { ftpClient.logout(); ftpClient.disconnect(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } /** * ftp 文件上傳 * * @param srcFilePath 源文件目錄 * @param desFileName 文件名稱 * @return 文件上傳結果 */ public boolean ftpUpload(String srcFilePath, String desFileName) { boolean status = false; try { FileInputStream srcFileStream = new FileInputStream(srcFilePath); status = ftpClient.storeFile(desFileName, srcFileStream); srcFileStream.close(); return status; } catch (Exception e) { e.printStackTrace(); } return status; } /** * ftp 更改目錄 * * @param path 更改的路徑 * @return 更改是否成功 */ public boolean ftpChangePath(String path) { boolean status = false; try { status = ftpClient.changeWorkingDirectory(path); } catch (Exception e) { e.printStackTrace(); } return status; } }
2.調用api
boolean isConnect = mFtpClient.ftpConnect("服務器host", "用戶名", "密碼", 21);//默認端口號是21 if (isConnect) { boolean isSuccessful = mFtpClient.ftpUpload("/sdcard/" + folderName + "/" + mPicturename, "/htdocs/pics/" + mPicturename); if (isSuccessful) { mFtpClient.ftpDisconnect(); //上傳成功 } else { //上傳失敗 } } else { //服務器連接失敗 }
附錄:自己之前做項目的時候寫過的FTP上傳代碼:
package com.kandao.yunbell.videocall; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.SocketException; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import com.kandao.yunbell.common.SysApplication; import android.content.Context; import android.util.Log; public class MyUploadThread extends Thread { private String fileName;// 文件名字 private String filePath;// 文件本地路徑 private String fileStoragePath;// 文件服務器存儲路徑 private String serverAddress;// 服務器地址 private String ftpUserName;// ftp賬號 private String ftpPassword;// ftp密碼 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(); this.fileName = fileName; this.filePath = filePath; this.fileStoragePath = fileStoragePath; this.serverAddress = serverAddress; this.ftpUserName = ftpUserName; this.ftpPassword = ftpPassword; this.mContext=mContext; } @Override public void run() { super.run(); try { FileInputStream fis=null; FTPClient ftpClient = new FTPClient(); String[] idPort = serverAddress.split(":"); ftpClient.connect(idPort[0], Integer.parseInt(idPort[1])); int returnCode = ftpClient.getReplyCode(); Log.i("caohai", "returnCode,upload:"+returnCode); boolean loginResult = ftpClient.login(ftpUserName, ftpPassword); Log.i("caohai", "loginResult:"+loginResult); if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登錄成功 // 設置上傳目錄 if (((SysApplication) mContext).getIsVideo()) { ((SysApplication) mContext).setIsVideo(false); boolean ff=ftpClient.changeWorkingDirectory(fileStoragePath + "/video/"); Log.i("caohai", "ff:"+ff); }else{ boolean ee=ftpClient.changeWorkingDirectory(fileStoragePath + "/photo/"); Log.i("caohai", "ee:"+ee); } ftpClient.setBufferSize(1024); // ftpClient.setControlEncoding("iso-8859-1"); // ftpClient.enterLocalPassiveMode(); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); fis = new FileInputStream(filePath + "/" + fileName); Log.i("caohai", "fileStoragePath00000:"+fileStoragePath); String[] path = fileStoragePath.split("visitorRecord"); boolean fs = ftpClient.storeFile(new String((path[1] + "/photo/" + fileName).getBytes(), "iso-8859-1"), fis); Log.i("caohai", "shifoushangchuanchenggong:"+fs); fis.close(); ftpClient.logout(); //ftpClient.disconnect(); } else {// 如果登錄失敗 ftpClient.disconnect(); } } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
關于Android FTP服務器上傳文件如何實現?就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。