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文件上传下载内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Android FTP 多线程断点续传下载\上传的实例

    最近在给我的开源下载框架Aria增加FTP断点续传下载和上传功能,在此过程中,爬了FTP的不少坑,终于将功能实现了,在此把一些核心功能点记录下载. FTP下载原理 FTP单线程断点续传 FTP和传统的HTTP协议有所不同,由于FTP没有所谓的头文件,因此我们不能像HTTP那样通过设置header向服务器指定下载区间. 但是FTP协议提供了一个更好用的命令REST用于从指定位置恢复任务,同时FTP协议也提供了一个命令SIZE用于获取下载的文件大小,有了这两个命令,FTP断点续传也就没有什么问题.

  • Android中FTP上传、下载的功能实现(含进度)

    Android中使用的FTP上传.下载,含有进度. 代码部分主要分为三个文件:MainActivity,FTP,ProgressInputStream 1. MainActivity package com.ftp; import java.io.File; import java.io.IOException; import java.util.LinkedList; import com.ftp.FTP.DeleteFileProgressListener; import com.ftp.F

  • Android关于FTP文件上传和下载功能详解

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 此篇博客为整理文章,供大家学习. 1.首先下载commons-net  jar包,可以百度下载. FTP的文件上传和下载的工具类: package ryancheng.example.progressbar; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.Outpu

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

    近期在工作上一直再维护平台OTA在线升级项目,其中关于这个升级文件主要是存放于ftp服务器上的,然后客户端通过走ftp协议方式下载至本地Android机进行一个系统升级操作.那么今天将对ftp实现文件上传和下载进行一个使用总结,关于ftp这方面的理论知识如果不是太了解的各位道友,那么请移步HTTP和FTP的区别的一些理论知识 作个具体的了解或者查阅相关资料.那么先看看个人工作项目这个OTA升级效果图吧.如下: 下面是具体的接口实现: 那么相关ftp的操作,已经被封装到ota.ftp这个包下,各位

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

    近期在工作上一直再维护平台OTA在线升级项目,其中关于这个升级文件主要是存放于ftp服务器上的,然后客户端通过走ftp协议方式下载至本地Android机进行一个系统升级操作.那么今天将对ftp实现文件上传和下载进行一个使用总结,关于ftp这方面的理论知识如果不是太了解的各位道友,那么请移步HTTP和FTP的区别的一些理论知识 作个具体的了解或者查阅相关资料.那么先看看个人工作项目这个OTA升级效果图吧.如下: 下面是具体的接口实现: 那么相关ftp的操作,已经被封装到ota.ftp这个包下,各位

  • Retrofit+Rxjava实现文件上传和下载功能

    Retrofit简介: 在Android API4.4之后,Google官方使用了square公司推出的okHttp替换了HttpClient的请求方式.后来square公司又推出了基于okHttp的网络请求框架:Retrofit. 什么是 RxJava? RxJava 是一个响应式编程框架,采用观察者设计模式.所以自然少不了 Observable 和 Subscriber 这两个东东了. RxJava 是一个开源项目,地址:https://github.com/ReactiveX/RxJava

  • node.js express框架实现文件上传与下载功能实例详解

    本文实例讲述了node.js express框架实现文件上传与下载功能.分享给大家供大家参考,具体如下: 背景 昨天吉视传媒的客户对IPS信息发布系统又提了一个新需求,就是发布端发送消息时需要支持附件的上传,而接收端可以对发布端上传的附件进行下载:接收端回复消息时也需要支持上传附件,发布端可以对所有接收端上传的附件进行打包下载. 功能实现 前台部分 前台使用webUploader插件即可,这是百度开发的一款文件上传组件,具体使用查看它的API即可.这个项目之前开发的时候前台使用了angular.

  • 详解JavaWeb如何实现文件上传和下载功能

    目录 1. 文件传输原理及介绍 2. JavaWeb文件上传 2.1我们用一个新的方式创建项目 2.2 导包 2.3 实用类介绍 2.4 pom.xml导入需要的依赖 2.5 index.jsp 2.6 info.jsp 2.7 FileServlet 2.8 配置Servlet 2.9 测试结果 3. SpringMVC文件上传和下载 3.1 上传 3.2 下载 1. 文件传输原理及介绍 2. JavaWeb文件上传 2.1我们用一个新的方式创建项目 空项目会直接弹出框 把jdk版本设置好 点

  • SpringBoot+微信小程序实现文件上传与下载功能详解

    目录 1.文件上传 1.1 后端部分 1.2 小程序前端部分 1.3 实现效果 2.文件下载 2.1 后端部分 2.2 小程序前端部分 2.3 实现效果 1.文件上传 1.1 后端部分 1.1.1 引入Apache Commons FIleUpload组件依赖 <!--文件上传与下载相关的依赖--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fil

  • JavaWeb Servlet实现文件上传与下载功能实例

    目录 前言 项目准备 文件上传 前台页面 文件下载 资源准备 超链接下载 后台实现下载 总结 前言 在上网的时候我们常常遇到文件上传的情况,例如上传头像.上传资料等:当然除了上传,遇见下载的情况也很多,接下来看看我们 servlet 中怎么实现文件的上传和下载. 项目准备 idea:2020.1 jdk:1.8 tomcat:10 项目模板:java Enterprise–>Web Application 文件上传 文件上传涉及到前台页面的编写和后台服务器端代码的编写,前台发送文件,后台接收并保

  • SpringBoot实现文件上传与下载功能的示例代码

    目录 Spring Boot文件上传与下载 举例说明 1.引入Apache Commons FileUpload组件依赖 2.设置上传文件大小限制 3.创建选择文件视图页面 4.创建控制器 5.创建文件下载视图页面 6.运行 Spring Boot文件上传与下载 在实际的Web应用开发中,为了成功上传文件,必须将表单的method设置为post,并将enctype设置为multipart/form-data.只有这种设置,浏览器才能将所选文件的二进制数据发送给服务器. 从Servlet 3.0开

  • SpringBoot文件上传与下载功能实现详解

    目录 前言 1.引入Apache Commons FileUpload组件依赖 2.设置上传文件大小限制 3.创建选择文件视图页面 4.创建控制器 5.创建文件下载视图页面 前言 文件上传与下载是Web应用开发中常用的功能之一,在实际的Web应用开发中,为了成功上传文件,必须将表单的method设置为post,并将enctype设置为multipart/form-data 只有这样设置,浏览器才能将所选文件的二进制数据发送给服务器 从Servlet3.0开始,就提供了处理文件上传的方法,但这种文

  • MyBatis与SpringMVC相结合实现文件上传、下载功能

    环境:maven+SpringMVC + Spring + MyBatis + MySql 本文主要说明如何使用input上传文件到服务器指定目录,或保存到数据库中:如何从数据库下载文件,和显示图像文件并实现缩放. 将文件存储在数据库中,一般是存文件的byte数组,对应的数据库数据类型为blob. 首先要创建数据库,此处使用MySql数据库. 注意:文中给出的代码多为节选重要片段,并不齐全. 1. 前期准备 使用maven创建一个springMVC+spring+mybatis+mysql的项目

随机推荐