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

Android使用ftp方式实现文件上传和下载功能的更多相关文章

  1. html5 canvas合成海报所遇问题及解决方案总结

    这篇文章主要介绍了html5 canvas合成海报所遇问题及解决方案总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Html5 video标签视频的最佳实践

    这篇文章主要介绍了Html5 video标签视频的最佳实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  3. AngularJs上传前预览图片的实例代码

    使用AngularJs进行开发,在项目中,经常会遇到上传图片后,需在一旁预览图片内容,怎么实现这样的功能呢?今天小编给大家分享AugularJs上传前预览图片的实现代码,需要的朋友参考下吧

  4. HTML5在微信内置浏览器下右上角菜单的调整字体导致页面显示错乱的问题

    HTML5在微信内置浏览器下,在右上角菜单的调整字体导致页面显示错乱的问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

  5. ios – containerURLForSecurityApplicationGroupIdentifier:在iPhone和Watch模拟器上给出不同的结果

    我使用默认的XCode模板创建了一个WatchKit应用程序.我向iOSTarget,WatchkitAppTarget和WatchkitAppExtensionTarget添加了应用程序组权利.(这是应用程序组名称:group.com.lombax.fiveminutes)然后,我尝试使用iOSApp和WatchKitExtension访问共享文件夹URL:延期:iOS应用:但是,测试NSURL

  6. Ionic – Splash Screen适用于iOS,但不适用于Android

    我有一个离子应用程序,其中使用CLI命令离子资源生成的启动画面和图标iOS版本与正在渲染的启动画面完美配合,但在Android版本中,只有在加载应用程序时才会显示白屏.我检查了config.xml文件,所有路径看起来都是正确的,生成的图像出现在相应的文件夹中.(我使用了splash.psd模板来生成它们.我错过了什么?这是config.xml文件供参考,我觉得我在这里做错了–解决方法在config.xml中添加以下键:它对我有用!

  7. ios – 无法启动iPhone模拟器

    /Library/Developer/CoreSimulator/Devices/530A44CB-5978-4926-9E91-E9DBD5BFB105/data/Containers/Bundle/Application/07612A5C-659D-4C04-ACD3-D211D2830E17/ProductName.app/ProductName然后,如果您在Xcode构建设置中选择标准体系结构并再次构建和运行,则会产生以下结果:dyld:lazysymbolbindingFailed:Symbol

  8. Xamarin iOS图像在Grid内部重叠

    heyo,所以在Xamarin我有一个使用并在其中包含一对,所有这些都包含在内.这在Xamarin.Android中看起来完全没问题,但是在Xamarin.iOS中,图像与标签重叠.我不确定它的区别是什么–为什么它在Xamarin.Android中看起来不错但在iOS中它的全部都不稳定?

  9. 在iOS上向后播放HTML5视频

    我试图在iPad上反向播放HTML5视频.HTML5元素包括一个名为playbackRate的属性,它允许以更快或更慢的速率或相反的方式播放视频.根据Apple’sdocumentation,iOS不支持此属性.通过每秒多次设置currentTime属性,可以反复播放,而无需使用playbackRate.这种方法适用于桌面Safari,但似乎在iOS设备上的搜索限制为每秒1次更新–在我的情况下太慢了.有没有办法在iOS设备上向后播放HTML5视频?解决方法iOS6Safari现在支持playbackRat

  10. ios FTP使用NSURLSession上传

    我试图通过FTP将文件上传到服务器.根据NSK的NSURLSession类支持FTP操作.有一个着名的AppleDeveloperblog也支持.但还不清楚NSURLSessionAPI是否支持ftp上传?

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部