热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

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

这篇文章主要介绍了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 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 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新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • Monkey《大话移动——Android与iOS应用测试指南》的预购信息发布啦!
    Monkey《大话移动——Android与iOS应用测试指南》的预购信息已经发布,可以在京东和当当网进行预购。感谢几位大牛给出的书评,并呼吁大家的支持。明天京东的链接也将发布。 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • 本文介绍了使用kotlin实现动画效果的方法,包括上下移动、放大缩小、旋转等功能。通过代码示例演示了如何使用ObjectAnimator和AnimatorSet来实现动画效果,并提供了实现抖动效果的代码。同时还介绍了如何使用translationY和translationX来实现上下和左右移动的效果。最后还提供了一个anim_small.xml文件的代码示例,可以用来实现放大缩小的效果。 ... [详细]
  • 基于layUI的图片上传前预览功能的2种实现方式
    本文介绍了基于layUI的图片上传前预览功能的两种实现方式:一种是使用blob+FileReader,另一种是使用layUI自带的参数。通过选择文件后点击文件名,在页面中间弹窗内预览图片。其中,layUI自带的参数实现了图片预览功能。该功能依赖于layUI的上传模块,并使用了blob和FileReader来读取本地文件并获取图像的base64编码。点击文件名时会执行See()函数。摘要长度为169字。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • HDU 2372 El Dorado(DP)的最长上升子序列长度求解方法
    本文介绍了解决HDU 2372 El Dorado问题的一种动态规划方法,通过循环k的方式求解最长上升子序列的长度。具体实现过程包括初始化dp数组、读取数列、计算最长上升子序列长度等步骤。 ... [详细]
  • Android中高级面试必知必会,积累总结
    本文介绍了Android中高级面试的必知必会内容,并总结了相关经验。文章指出,如今的Android市场对开发人员的要求更高,需要更专业的人才。同时,文章还给出了针对Android岗位的职责和要求,并提供了简历突出的建议。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文讲述了如何通过代码在Android中更改Recycler视图项的背景颜色。通过在onBindViewHolder方法中设置条件判断,可以实现根据条件改变背景颜色的效果。同时,还介绍了如何修改底部边框颜色以及提供了RecyclerView Fragment layout.xml和项目布局文件的示例代码。 ... [详细]
  • 本文介绍了C#中数据集DataSet对象的使用及相关方法详解,包括DataSet对象的概述、与数据关系对象的互联、Rows集合和Columns集合的组成,以及DataSet对象常用的方法之一——Merge方法的使用。通过本文的阅读,读者可以了解到DataSet对象在C#中的重要性和使用方法。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
author-avatar
扈菁雄佳纯
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有