Android使用ftp方式实现文件上传和下载功能
时间:2022-07-26
本文章向大家介绍Android使用ftp方式实现文件上传和下载功能,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
近期在工作上一直再维护平台OTA在线升级项目,其中关于这个升级文件主要是存放于ftp服务器上的,然后客户端通过走ftp协议方式下载至本地Android机进行一个系统升级操作。那么今天将对ftp实现文件上传和下载进行一个使用总结,关于ftp这方面的理论知识如果不是太了解的各位道友,那么请移步HTTP和FTP的区别的一些理论知识 作个具体的了解或者查阅相关资料。那么先看看个人工作项目这个OTA升级效果图吧。如下:
下面是具体的接口实现:
那么相关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文件上传下载内容请搜索ZaLou.Cn以前的文章或继续浏览下面的相关文章希望大家以后多多支持ZaLou.Cn!
- django之对FileField字段的upload_to的设定
- JAVA_HOME环境变量失效的解决办法
- JBOSS EAP 6.0+ Standalone模式安装成Windows服务
- Django 设置media static
- Django---Ajax
- 利用Spring MVC搭建REST Service
- ehcache2.8.3入门示例:hello world
- day4、Linux基础题目
- 命令行执行Django脚本的方法
- Spring Security笔记:Hello World
- day5、文件乱码怎么解决
- javascript计算对象的长度
- Spring 4.0.2 学习笔记(2) - 自动注入及properties文件的使用
- day6、Linux下如何找出7天以前的文件删除
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 用了这个jupyter插件,我已经半个月没打开过excel了
- Webpack学习笔记
- 正则表达式学习笔记
- R 可视化 | 华夫饼图
- 绝了!Python定时爬取微博热搜+pyecharts动态图展示
- 实战 | Python爬取B站柯南弹幕+Gephi梳理主线剧情
- 别再问我 Python 怎么识别数字验证码了!
- Python自动化办公 | 同事要我帮忙补写178份Word日报!别闹!
- Excel多区间判断,其实很简单
- 外观模式
- cp命令
- java基本数据类型及相互间的转换(转)
- 【java设计模式系列】1. 工厂方法模式(Factory Method)
- 为什么 Java 中 1000==1000 为 false ?
- 【java设计模式系列】2. 单例模式(Singleton)