当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用ftp方式实现文件上传和下载功能

Android使用ftp方式实现文件上传和下载功能

2020年06月23日  | 移动技术网IT编程  | 我要评论

近期在工作上一直再维护平台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文件上传下载内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网