Android FTP服務器上傳文件攻略(代碼詳解)
1.前言
在開發中,會遇到向FTP服務器上傳文件的需求,首先要導入commons-net-3.3.jar 然后利用api進行相關操作,具體功能如下:
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服務器上傳文件攻略的文章就介紹到這了,更多相關Android FTP服務器上傳內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
