Android使⽤ftp⽅式实现⽂件上传和下载功能
近期在⼯作上⼀直再维护平台OTA在线升级项⽬,其中关于这个升级⽂件主要是存放于ftp服务器上的,然后客户端通过⾛ftp 协议⽅式下载⾄本地Android机进⾏⼀个系统升级操作。那么今天将对ftp实现⽂件上传和下载进⾏⼀个使⽤总结,关于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() {
DatabaseOldDate();
}
public String getOldVersion() {
DatabaseOldVersion();
}
public void checkVer(String serverRoot) {
// TODO Auto-generated method stub
mDownLoad = Instance();
mDownLoad.setServeRoot(serverRoot);
mDownLoad.setFtpInfo(mApp.mFtpInfo);
mDownLoad.checkUpgrade();
}
FTPToolkit.java
package a.ftp;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;
import java.io.File;
import java.util.List;
import a.clinet.PathToolkit;
import a.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 {
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 (!ists())
localFile.mkdirs();
localfilepath = PathToolkit.formatPath4File(localPath
+ File.separator + new File(remoteFileName).getName());
}
if (x == FTPFile.TYPE_FILE) {
connect下载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 (!ists())
throw new Exception("the upload FTP file"
+ Path() + "not exist!");
if (!localfile.isFile())
throw new Exception("the upload FTP file"
+ Path() + "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 (!ists())
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 (!ists())
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路径是否存在,如果存在返回类型(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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论