Android使用ftp方式實(shí)現(xiàn)文件上傳和下載功能
近期在工作上一直再維護(hù)平臺(tái)OTA在線升級(jí)項(xiàng)目,其中關(guān)于這個(gè)升級(jí)文件主要是存放于ftp服務(wù)器上的,然后客戶端通過走ftp協(xié)議方式下載至本地Android機(jī)進(jìn)行一個(gè)系統(tǒng)升級(jí)操作。那么今天將對(duì)ftp實(shí)現(xiàn)文件上傳和下載進(jìn)行一個(gè)使用總結(jié),關(guān)于ftp這方面的理論知識(shí)如果不是太了解的各位道友,那么請(qǐng)移步HTTP和FTP的區(qū)別的一些理論知識(shí) 作個(gè)具體的了解或者查閱相關(guān)資料。那么先看看個(gè)人工作項(xiàng)目這個(gè)OTA升級(jí)效果圖吧。如下:

下面是具體的接口實(shí)現(xiàn):

那么相關(guān)ftp的操作,已經(jīng)被封裝到ota.ftp這個(gè)包下,各位童鞋可以下載示例代碼慢慢研究。另外這個(gè)要是用ftp服務(wù)我們cline端需要再項(xiàng)目工程導(dǎo)入ftp4j-1.7.2.jar包
這邊作個(gè)使用的邏輯分析:首先在我們的項(xiàng)目工程FtpApplication中啟動(dòng)這個(gè)OtaService,其中OtaService作為一個(gè)服務(wù)運(yùn)行起來,在這個(gè)服務(wù)里面拿到封裝好ftp相關(guān)接口的DownLoad.java進(jìn)行ftp文件操作,關(guān)鍵代碼如下:
public void startDownload() {
// TODO Auto-generated method stub
mDownLoad.start();
}
public void stopDownload() {
mDownLoad.stop();
}
public void cancel() {
mDownLoad.cancel();
}
public String getOldDate() {
return mDownLoad.getDatabaseOldDate();
}
public String getOldVersion() {
return mDownLoad.getDatabaseOldVersion();
}
public void checkVer(String serverRoot) {
// TODO Auto-generated method stub
mDownLoad = DownLoad.getInstance();
mDownLoad.setServeRoot(serverRoot);
mDownLoad.setFtpInfo(mApp.mFtpInfo);
mDownLoad.checkUpgrade();
}
FTPToolkit.java
package com.asir.ota.ftp;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
import java.io.File;
import java.util.List;
import com.asir.ota.clinet.PathToolkit;
import com.asir.ota.ftp.DownLoad.MyFtpListener;
/**
* FTP客戶端工具
*
*/
public final class FTPToolkit {
private FTPToolkit() {
}
/**
* 創(chuàng)建FTP連接
*
* @param host
* 主機(jī)名或IP
* @param port
* ftp端口
* @param username
* ftp用戶名
* @param password
* ftp密碼
* @return 一個(gè)客戶端
* @throws Exception
*/
public static FTPClient makeFtpConnection(String host, int port,
String username, String password) throws Exception {
FTPClient client = new FTPClient();
try {
client.connect(host, port);
if(username != null && password != null) {
client.login(username, password);
}
} catch (Exception e) {
throw new Exception(e);
}
return client;
}
/**
* FTP下載文件到本地一個(gè)文件夾,如果本地文件夾不存在,則創(chuàng)建必要的目錄結(jié)構(gòu)
*
* @param client
* FTP客戶端
* @param remoteFileName
* FTP文件
* @param localPath
* 存的本地文件路徑或目錄
* @throws Exception
*/
public static void download(FTPClient client, String remoteFileName,
String localPath, long startPoint, MyFtpListener listener) throws Exception {
String localfilepath = localPath;
int x = isExist(client, remoteFileName);
File localFile = new File(localPath);
if (localFile.isDirectory()) {
if (!localFile.exists())
localFile.mkdirs();
localfilepath = PathToolkit.formatPath4File(localPath
+ File.separator + new File(remoteFileName).getName());
}
if (x == FTPFile.TYPE_FILE) {
try {
if (listener != null)
client.download(remoteFileName, new File(localfilepath),
startPoint, listener);
else
client.download(remoteFileName, new File(localfilepath), startPoint);
} catch (Exception e) {
throw new Exception(e);
}
} else {
throw new Exception("the target " + remoteFileName + "not exist");
}
}
/**
* FTP上傳本地文件到FTP的一個(gè)目錄下
*
* @param client
* FTP客戶端
* @param localfile
* 本地文件
* @param remoteFolderPath
* FTP上傳目錄
* @throws Exception
*/
public static void upload(FTPClient client, File localfile,
String remoteFolderPath, MyFtpListener listener) throws Exception {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
try {
client.changeDirectory(remoteFolderPath);
if (!localfile.exists())
throw new Exception("the upload FTP file"
+ localfile.getPath() + "not exist!");
if (!localfile.isFile())
throw new Exception("the upload FTP file"
+ localfile.getPath() + "is a folder!");
if (listener != null)
client.upload(localfile, listener);
else
client.upload(localfile);
client.changeDirectory("/");
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* FTP上傳本地文件到FTP的一個(gè)目錄下
*
* @param client
* FTP客戶端
* @param localfilepath
* 本地文件路徑
* @param remoteFolderPath
* FTP上傳目錄
* @throws Exception
*/
public static void upload(FTPClient client, String localfilepath,
String remoteFolderPath, MyFtpListener listener) throws Exception {
File localfile = new File(localfilepath);
upload(client, localfile, remoteFolderPath, listener);
}
/**
* 批量上傳本地文件到FTP指定目錄上
*
* @param client
* FTP客戶端
* @param localFilePaths
* 本地文件路徑列表
* @param remoteFolderPath
* FTP上傳目錄
* @throws Exception
*/
public static void uploadListPath(FTPClient client,
List<String> localFilePaths, String remoteFolderPath, MyFtpListener listener) throws Exception {
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
try {
client.changeDirectory(remoteFolderPath);
for (String path : localFilePaths) {
File file = new File(path);
if (!file.exists())
throw new Exception("the upload FTP file" + path + "not exist!");
if (!file.isFile())
throw new Exception("the upload FTP file" + path
+ "is a folder!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 批量上傳本地文件到FTP指定目錄上
*
* @param client
* FTP客戶端
* @param localFiles
* 本地文件列表
* @param remoteFolderPath
* FTP上傳目錄
* @throws Exception
*/
public static void uploadListFile(FTPClient client, List<File> localFiles,
String remoteFolderPath, MyFtpListener listener) throws Exception {
try {
client.changeDirectory(remoteFolderPath);
remoteFolderPath = PathToolkit.formatPath4FTP(remoteFolderPath);
for (File file : localFiles) {
if (!file.exists())
throw new Exception("the upload FTP file" + file.getPath()
+ "not exist!");
if (!file.isFile())
throw new Exception("the upload FTP file" + file.getPath()
+ "is a folder!");
if (listener != null)
client.upload(file, listener);
else
client.upload(file);
}
client.changeDirectory("/");
} catch (Exception e) {
throw new Exception(e);
}
}
/**
* 判斷一個(gè)FTP路徑是否存在,如果存在返回類型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、
* FTPFile.TYPE_LINK=2) 如果文件不存在,則返回一個(gè)-1
*
* @param client
* FTP客戶端
* @param remotePath
* FTP文件或文件夾路徑
* @return 存在時(shí)候返回類型值(文件0,文件夾1,連接2),不存在則返回-1
*/
public static int isExist(FTPClient client, String remotePath) {
remotePath = PathToolkit.formatPath4FTP(remotePath);
FTPFile[] list = null;
try {
list = client.list(remotePath);
} catch (Exception e) {
return -1;
}
if (list.length > 1)
return FTPFile.TYPE_DIRECTORY;
else if (list.length == 1) {
FTPFile f = list[0];
if (f.getType() == FTPFile.TYPE_DIRECTORY)
return FTPFile.TYPE_DIRECTORY;
// 假設(shè)推理判斷
String _path = remotePath + "/" + f.getName();
try {
int y = client.list(_path).length;
if (y == 1)
return FTPFile.TYPE_DIRECTORY;
else
return FTPFile.TYPE_FILE;
} catch (Exception e) {
return FTPFile.TYPE_FILE;
}
} else {
try {
client.changeDirectory(remotePath);
return FTPFile.TYPE_DIRECTORY;
} catch (Exception e) {
return -1;
}
}
}
public static long getFileLength(FTPClient client, String remotePath) throws Exception {
String remoteFormatPath = PathToolkit.formatPath4FTP(remotePath);
if(isExist(client, remotePath) == 0) {
FTPFile[] files = client.list(remoteFormatPath);
return files[0].getSize();
}else {
throw new Exception("get remote file length error!");
}
}
/**
* 關(guān)閉FTP連接,關(guān)閉時(shí)候像服務(wù)器發(fā)送一條關(guān)閉命令
*
* @param client
* FTP客戶端
* @return 關(guān)閉成功,或者鏈接已斷開,或者鏈接為null時(shí)候返回true,通過兩次關(guān)閉都失敗時(shí)候返回false
*/
public static boolean closeConnection(FTPClient client) {
if (client == null)
return true;
if (client.isConnected()) {
try {
client.disconnect(true);
return true;
} catch (Exception e) {
try {
client.disconnect(false);
} catch (Exception e1) {
e1.printStackTrace();
return false;
}
}
}
return true;
}
}
包括登錄,開始下載,取消下載,獲取升級(jí)文件版本號(hào)和服務(wù)器版本校驗(yàn)等。其它的是一些數(shù)據(jù)庫(kù),SD卡文件相關(guān)操作,那么最后在我們下載完成之后需要對(duì)文件進(jìn)行一個(gè)文件解壓再執(zhí)行升級(jí)操作,這部分在ZipExtractor.java和OTAProvider.java中實(shí)現(xiàn)
總結(jié)
到此這篇關(guān)于Android使用ftp方式實(shí)現(xiàn)文件上傳和下載的文章就介紹到這了,更多相關(guān)android ftp文件上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android模糊處理實(shí)現(xiàn)圖片毛玻璃效果
這篇文章主要介紹了Android模糊處理實(shí)現(xiàn)圖片毛玻璃效果,需要的朋友可以參考下2016-02-02
Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法
這篇文章主要介紹了Android實(shí)現(xiàn)使用流媒體播放遠(yuǎn)程mp3文件的方法,結(jié)合實(shí)例形式分析了Android遠(yuǎn)程播放音頻文件的相關(guān)步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-08-08
Android開發(fā)使用UncaughtExceptionHandler捕獲全局異常
本文主要介紹在Android開發(fā)中使用UncaughtExceptionHandler捕獲全局異常,需要的朋友可以參考下。2016-06-06
Android 個(gè)人理財(cái)工具四:添加賬單頁(yè)面 下
本文主要介紹Android 個(gè)人理財(cái)工具添加賬單頁(yè)面,這里是添加賬單的詳情頁(yè)面及如何使用Android Spinner控件的簡(jiǎn)單示例,有需要的小伙伴可以參考下2016-08-08
Android實(shí)現(xiàn)從底部彈出的Dialog示例(一)
這篇文章主要介紹了Android實(shí)現(xiàn)從底部彈出的Dialog示例(一),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-01-01
Android中Fragment多層嵌套時(shí)onActivityResult無法正確回調(diào)問題的解決方法
這篇文章主要介紹了Android中Fragment多層嵌套時(shí)onActivityResult無法正確回調(diào)問題的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android仿淘寶頭條向上滾動(dòng)廣告條ViewFlipper
這篇文章主要為大家詳細(xì)介紹了Android仿淘寶頭條向上滾動(dòng)廣告條ViewFlipper,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05
Android之ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Android之ListView分頁(yè)加載數(shù)據(jù)功能實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
關(guān)于Android Device Monitor 無法打開問題
大家好,本篇文章主要講的是關(guān)于Android Device Monitor 無法打開問題,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-01-01

