日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁技術文章
文章詳情頁

Android使用ftp方式實現文件上傳和下載功能

瀏覽:209日期:2022-09-23 16:16:03

近期在工作上一直再維護平臺OTA在線升級項目,其中關于這個升級文件主要是存放于ftp服務器上的,然后客戶端通過走ftp協議方式下載至本地Android機進行一個系統升級操作。那么今天將對ftp實現文件上傳和下載進行一個使用總結,關于ftp這方面的理論知識如果不是太了解的各位道友,那么請移步HTTP和FTP的區別的一些理論知識 作個具體的了解或者查閱相關資料。那么先看看個人工作項目這個OTA升級效果圖吧。如下:

Android使用ftp方式實現文件上傳和下載功能

下面是具體的接口實現:

Android使用ftp方式實現文件上傳和下載功能

那么相關ftp的操作,已經被封裝到ota.ftp這個包下,各位童鞋可以下載示例代碼慢慢研究。另外這個要是用ftp服務我們cline端需要再項目工程導入ftp4j-1.7.2.jar包

這邊作個使用的邏輯分析:首先在我們的項目工程FtpApplication中啟動這個OtaService,其中OtaService作為一個服務運行起來,在這個服務里面拿到封裝好ftp相關接口的DownLoad.java進行ftp文件操作,關鍵代碼如下:

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() { } /** * 創建FTP連接 * * @param host * 主機名或IP * @param port * ftp端口 * @param username * ftp用戶名 * @param password * ftp密碼 * @return 一個客戶端 * @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下載文件到本地一個文件夾,如果本地文件夾不存在,則創建必要的目錄結構 * * @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的一個目錄下 * * @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的一個目錄下 * * @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); } }/** * 判斷一個FTP路徑是否存在,如果存在返回類型(FTPFile.TYPE_DIRECTORY=1、FTPFile.TYPE_FILE=0、 * FTPFile.TYPE_LINK=2) 如果文件不存在,則返回一個-1 * * @param client * FTP客戶端 * @param remotePath * FTP文件或文件夾路徑 * @return 存在時候返回類型值(文件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; // 假設推理判斷 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!'); } } /** * 關閉FTP連接,關閉時候像服務器發送一條關閉命令 * * @param client * FTP客戶端 * @return 關閉成功,或者鏈接已斷開,或者鏈接為null時候返回true,通過兩次關閉都失敗時候返回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; }}

包括登錄,開始下載,取消下載,獲取升級文件版本號和服務器版本校驗等。其它的是一些數據庫,SD卡文件相關操作,那么最后在我們下載完成之后需要對文件進行一個文件解壓再執行升級操作,這部分在ZipExtractor.java和OTAProvider.java中實現

示例代碼點擊下載

總結

到此這篇關于Android使用ftp方式實現文件上傳和下載的文章就介紹到這了,更多相關android ftp文件上傳下載內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Android
相關文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
亚洲一区亚洲| 丝袜美腿诱惑一区二区三区 | 国内精品福利| 日韩在线不卡| 欧美福利一区| 日韩精品永久网址| 日韩精品影视| 免费视频一区三区| 久久av一区二区三区| 一区二区电影在线观看| 亚洲精品美女| 国产人成精品一区二区三| 欧美日韩精品一区二区三区在线观看| 日韩三区四区| 国产精品手机在线播放| 国产激情久久| 欧美国产偷国产精品三区| 成人午夜网址| 999国产精品999久久久久久| 精品成人免费一区二区在线播放| 成人片免费看| 久久久人人人| 99久久亚洲精品| 欧美在线观看视频一区| 91亚洲自偷观看高清| 国产劲爆久久| 国产精品视频3p| 久久91视频| 日韩欧美1区| 蜜桃视频欧美| 性色av一区二区怡红| 蜜臀av在线播放一区二区三区 | 国产精品成人自拍| 免费一级片91| 中文在线日韩| 亚洲一区网站| 国产日韩欧美| 久久精品二区亚洲w码| 福利一区二区三区视频在线观看| 91亚洲成人| 91高清一区| 中文无码日韩欧| 亚洲69av| 美腿丝袜亚洲三区| 午夜影院一区| 9色精品在线| 欧美日本不卡高清| 欧美国产小视频| 欧美aa在线观看| 亚洲作爱视频| 日韩国产欧美一区二区三区| 国产精品久久久久久久久久齐齐 | 视频一区二区三区中文字幕| 亚洲精品在线国产| 美女视频黄久久| 久久美女性网| 蜜桃视频在线观看一区二区| 日韩av成人高清| 国产麻豆一区二区三区| 国产第一亚洲| 日韩一级精品| 91精品国产自产观看在线| 美女视频免费精品| 成人精品中文字幕| 日韩精品视频一区二区三区| 精品免费av| 欧美日韩在线网站| 国产亚洲激情| 精品视频一区二区三区在线观看 | 伊人精品在线| 久久精品av麻豆的观看方式| 国产精品不卡| 亚洲欧美日韩综合国产aⅴ| 日本v片在线高清不卡在线观看| 麻豆视频久久| 精品免费av在线| 亚洲精品极品| 日韩不卡免费高清视频| 日本va欧美va欧美va精品| 欧美xxxx中国| 亚洲自拍另类| 国产在线不卡一区二区三区| 最新国产拍偷乱拍精品| 国产精品sss在线观看av| 美女网站一区| 国产精品羞羞答答在线观看| 欧美亚洲精品在线| 牛牛精品成人免费视频| 欧美午夜不卡| 美女视频网站久久| 99亚洲视频| 国产传媒在线观看| 日韩在线麻豆| 欧美亚洲精品在线| 久久精品午夜| 999在线观看精品免费不卡网站| 国产精品男女| 久久不射网站| 亚洲伦乱视频| 免费观看亚洲天堂| 国产女优一区| 岛国av在线网站| 欧美a一区二区| 日本v片在线高清不卡在线观看| 欧美/亚洲一区| jizzjizz中国精品麻豆| 欧美日韩精品一区二区三区视频| 在线亚洲一区| 久久男女视频| 国产成人免费| 日本在线不卡视频| 亚洲激情中文| bbw在线视频| 国产精品久久久久久久久久久久久久久| 影音先锋国产精品| 日韩一区二区中文| 欧美激情91| 欧美有码在线| 午夜亚洲一区| 亚洲福利国产| 天堂8中文在线最新版在线| 国产精品日本一区二区三区在线| 亚洲精品女人| 亚洲综合精品四区| 日韩成人亚洲| 精品一区二区三区中文字幕视频 | 天海翼精品一区二区三区| 久久九九精品| 不卡一二三区| 国产在线看片免费视频在线观看| 国产欧美日本| 欧美在线首页| 日韩精品一区二区三区中文在线| 亚洲激情另类| 狠狠色狠狠色综合日日tαg| 国产一区2区| 国产精品资源| 夜夜精品视频| 欧美aa在线观看| 久久xxx视频| 国产精品色婷婷在线观看| 日本三级亚洲精品| 日韩黄色在线观看| 日日摸夜夜添夜夜添国产精品| 日韩中文字幕一区二区三区| 香蕉国产精品| 亚洲精品一区二区妖精| 99久久精品国产亚洲精品| 肉色欧美久久久久久久免费看| 精品一区91| 国产一区二区三区四区五区 | 日韩欧美2区| 人人精品人人爱| 水蜜桃久久夜色精品一区的特点| 欧美日韩国产亚洲一区| 女同性一区二区三区人了人一| 亚洲天堂久久| 亚洲激情av| 中文不卡在线| 亚洲日产国产精品| 亚洲婷婷丁香| 最近国产精品视频| 日韩av中文字幕一区二区三区| 亚洲制服少妇| 亚洲久久在线| 91精品国产自产精品男人的天堂| 日本aⅴ亚洲精品中文乱码 | 玖玖精品视频| 日韩国产在线不卡视频| 日韩高清三区| 麻豆精品在线观看| 国产精品专区免费| 亚洲特色特黄| 免费在线观看不卡| 国产日韩一区二区三区在线播放| 欧美日韩a区| 精品欧美视频| 亚洲91精品| 日韩中文字幕1| 日韩国产欧美在线播放| 美女视频黄久久| 色老板在线视频一区二区| 婷婷亚洲五月| 日韩成人在线看| 国产在线一区不卡| 国产精品99一区二区| 日韩中文欧美在线| 国产亚洲欧美日韩精品一区二区三区 | 精品视频在线观看网站| av亚洲一区二区三区| 六月丁香综合| 美女免费视频一区| 婷婷综合在线| 国产亚洲一卡2卡3卡4卡新区| 97精品国产| 亚洲伊人影院| 国产成人精选| 中文精品在线| 久久99精品久久久久久园产越南 | 国产一区二区三区四区大秀|