欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-12-19 11:58:40
本文实例为大家分享了android九宫格图片展示的具体代码,供大家参考,具体内容如下 此篇博客为整理文章,供大家学习。 1.首先下载commons-net ...

本文实例为大家分享了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.outputstream; 
import java.io.randomaccessfile; 
import org.apache.commons.net.ftp.ftp; 
import org.apache.commons.net.ftp.ftpclient; 
import org.apache.commons.net.ftp.ftpfile; 
import org.apache.commons.net.ftp.ftpreply; 
import android.os.environment; 
 
public class ftpmanager { 
 ftpclient ftpclient = null; 
 
 public ftpmanager() { 
  ftpclient = new ftpclient(); 
 } 
 
 // 连接到ftp服务器 
 public synchronized boolean connect() throws exception { 
  boolean bool = false; 
  if (ftpclient.isconnected()) {//判断是否已登陆 
   ftpclient.disconnect(); 
  } 
  ftpclient.setdatatimeout(20000);//设置连接超时时间 
  ftpclient.setcontrolencoding("utf-8"); 
  ftpclient.connect("ip地址", 端口); 
  if (ftpreply.ispositivecompletion(ftpclient.getreplycode())) { 
   if (ftpclient.login("用户名", "密码")) { 
    bool = true; 
    system.out.println("ftp连接成功"); 
   } 
  } 
  return bool; 
 } 
 
 // 创建文件夹 
 public boolean createdirectory(string path) throws exception { 
  boolean bool = false; 
  string directory = path.substring(0, path.lastindexof("/") + 1); 
  int start = 0; 
  int end = 0; 
  if (directory.startswith("/")) { 
   start = 1; 
  } 
  end = directory.indexof("/", start); 
  while (true) { 
   string subdirectory = directory.substring(start, end); 
   if (!ftpclient.changeworkingdirectory(subdirectory)) { 
    ftpclient.makedirectory(subdirectory); 
    ftpclient.changeworkingdirectory(subdirectory); 
    bool = true; 
   } 
   start = end + 1; 
   end = directory.indexof("/", start); 
   if (end == -1) { 
    break; 
   } 
  } 
  return bool; 
 } 
 
 // 实现上传文件的功能 
 public synchronized boolean uploadfile(string localpath, string serverpath) 
   throws exception { 
  // 上传文件之前,先判断本地文件是否存在 
  file localfile = new file(localpath); 
  if (!localfile.exists()) { 
   system.out.println("本地文件不存在"); 
   return false; 
  } 
  system.out.println("本地文件存在,名称为:" + localfile.getname()); 
  createdirectory(serverpath); // 如果文件夹不存在,创建文件夹 
  system.out.println("服务器文件存放路径:" + serverpath + localfile.getname()); 
  string filename = localfile.getname(); 
  // 如果本地文件存在,服务器文件也在,上传文件,这个方法中也包括了断点上传 
  long localsize = localfile.length(); // 本地文件的长度 
  ftpfile[] files = ftpclient.listfiles(filename); 
  long serversize = 0; 
  if (files.length == 0) { 
   system.out.println("服务器文件不存在"); 
   serversize = 0; 
  } else { 
   serversize = files[0].getsize(); // 服务器文件的长度 
  } 
  if (localsize <= serversize) { 
   if (ftpclient.deletefile(filename)) { 
    system.out.println("服务器文件存在,删除文件,开始重新上传"); 
    serversize = 0; 
   } 
  } 
  randomaccessfile raf = new randomaccessfile(localfile, "r"); 
  // 进度 
  long step = localsize / 100; 
  long process = 0; 
  long currentsize = 0; 
  // 好了,正式开始上传文件 
  ftpclient.enterlocalpassivemode(); 
  ftpclient.setfiletype(ftp.binary_file_type); 
  ftpclient.setrestartoffset(serversize); 
  raf.seek(serversize); 
  outputstream output = ftpclient.appendfilestream(filename); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = raf.read(b)) != -1) { 
   output.write(b, 0, length); 
   currentsize = currentsize + length; 
   if (currentsize / step != process) { 
    process = currentsize / step; 
    if (process % 10 == 0) { 
     system.out.println("上传进度:" + process); 
    } 
   } 
  } 
  output.flush(); 
  output.close(); 
  raf.close(); 
  if (ftpclient.completependingcommand()) { 
   system.out.println("文件上传成功"); 
   return true; 
  } else { 
   system.out.println("文件上传失败"); 
   return false; 
  } 
 } 
 
 // 实现下载文件功能,可实现断点下载 
 public synchronized boolean downloadfile(string localpath, string serverpath) 
   throws exception { 
  // 先判断服务器文件是否存在 
  ftpfile[] files = ftpclient.listfiles(serverpath); 
  if (files.length == 0) { 
   system.out.println("服务器文件不存在"); 
   return false; 
  } 
  system.out.println("远程文件存在,名字为:" + files[0].getname()); 
  localpath = localpath + files[0].getname(); 
  // 接着判断下载的文件是否能断点下载 
  long serversize = files[0].getsize(); // 获取远程文件的长度 
  file localfile = new file(localpath); 
  long localsize = 0; 
  if (localfile.exists()) { 
   localsize = localfile.length(); // 如果本地文件存在,获取本地文件的长度 
   if (localsize >= serversize) { 
    system.out.println("文件已经下载完了"); 
    file file = new file(localpath); 
    file.delete(); 
    system.out.println("本地文件存在,删除成功,开始重新下载"); 
    return false; 
   } 
  } 
  // 进度 
  long step = serversize / 100; 
  long process = 0; 
  long currentsize = 0; 
  // 开始准备下载文件 
  ftpclient.enterlocalactivemode(); 
  ftpclient.setfiletype(ftp.binary_file_type); 
  outputstream out = new fileoutputstream(localfile, true); 
  ftpclient.setrestartoffset(localsize); 
  inputstream input = ftpclient.retrievefilestream(serverpath); 
  byte[] b = new byte[1024]; 
  int length = 0; 
  while ((length = input.read(b)) != -1) { 
   out.write(b, 0, length); 
   currentsize = currentsize + length; 
   if (currentsize / step != process) { 
    process = currentsize / step; 
    if (process % 10 == 0) { 
     system.out.println("下载进度:" + process); 
    } 
   } 
  } 
  out.flush(); 
  out.close(); 
  input.close(); 
  // 此方法是来确保流处理完毕,如果没有此方法,可能会造成现程序死掉 
  if (ftpclient.completependingcommand()) { 
   system.out.println("文件下载成功"); 
   return true; 
  } else { 
   system.out.println("文件下载失败"); 
   return false; 
  } 
 } 
 
 // 如果ftp上传打开,就关闭掉 
 public void closeftp() throws exception { 
  if (ftpclient.isconnected()) { 
   ftpclient.disconnect(); 
  } 
 } 
} 

具体实现看代码注释写的很详细。

一.android中ftp文件上传代码:

// 上传例子 
private void ftpupload() { 
 new thread() { 
 public void run() { 
  try { 
  system.out.println("正在连接ftp服务器...."); 
  ftpmanager ftpmanager = new ftpmanager(); 
  if (ftpmanager.connect()) { 
  if (ftpmanager.uploadfile(ftpmanager.rootpath + "updatexzmarketplatform.apk", "mnt/sdcard/")) { 
  ftpmanager.closeftp(); 
  } 
  } 
  } catch (exception e) { 
  // todo: handle exception 
  // system.out.println(e.getmessage()); 
  } 
 } 
 }.start(); 
 } 

二.android中ftp文件下载代码:

// 下载例子 
private void ftpdownload() { 
 new thread() { 
 public void run() { 
  try { 
  system.out.println("正在连接ftp服务器...."); 
  ftpmanager ftpmanager = new ftpmanager(); 
  if (ftpmanager.connect()) { 
  if (ftpmanager.downloadfile(ftpmanager.rootpath, "20120723_xfq07_xzmarketplatform.db")) { 
  ftpmanager.closeftp(); 
  } 
  } 
  } catch (exception e) { 
  // todo: handle exception 
  // system.out.println(e.getmessage()); 
  } 
 } 
 }.start(); 
 } 

自己之前做项目的时候写过的ftp上传代码:

package com.kandao.yunbell.videocall; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.filenotfoundexception; 
import java.io.ioexception; 
import java.io.unsupportedencodingexception; 
import java.net.socketexception; 
 
import org.apache.commons.net.ftp.ftp; 
import org.apache.commons.net.ftp.ftpclient; 
import org.apache.commons.net.ftp.ftpreply; 
 
import com.kandao.yunbell.common.sysapplication; 
 
import android.content.context; 
import android.util.log; 
 
public class myuploadthread extends thread { 
 private string filename;// 文件名字 
 private string filepath;// 文件本地路径 
 private string filestoragepath;// 文件服务器存储路径 
 private string serveraddress;// 服务器地址 
 private string ftpusername;// ftp账号 
 private string ftppassword;// ftp密码 
 private context mcontext; 
 public myuploadthread() { 
  super(); 
  // todo auto-generated constructor stub 
 } 
 
 public myuploadthread(context mcontext,string filename, string filepath, 
    string filestoragepath,string serveraddress,string ftpusername,string ftppassword) { 
  super(); 
  this.filename = filename; 
  this.filepath = filepath; 
  this.filestoragepath = filestoragepath; 
  this.serveraddress = serveraddress; 
  this.ftpusername = ftpusername; 
  this.ftppassword = ftppassword; 
  this.mcontext=mcontext; 
 } 
 
 @override 
 public void run() { 
  super.run(); 
  try { 
   fileinputstream fis=null; 
   ftpclient ftpclient = new ftpclient(); 
   string[] idport = serveraddress.split(":"); 
   ftpclient.connect(idport[0], integer.parseint(idport[1])); 
   int returncode = ftpclient.getreplycode(); 
   log.i("caohai", "returncode,upload:"+returncode); 
   boolean loginresult = ftpclient.login(ftpusername, ftppassword); 
   log.i("caohai", "loginresult:"+loginresult); 
   if (loginresult && ftpreply.ispositivecompletion(returncode)) {// 如果登录成功 
     
    // 设置上传目录 
     
    if (((sysapplication) mcontext).getisvideo()) { 
     ((sysapplication) mcontext).setisvideo(false); 
     boolean ff=ftpclient.changeworkingdirectory(filestoragepath + "/video/"); 
     log.i("caohai", "ff:"+ff); 
    }else{ 
    boolean ee=ftpclient.changeworkingdirectory(filestoragepath + "/photo/"); 
    log.i("caohai", "ee:"+ee); 
    } 
    ftpclient.setbuffersize(1024); 
    // ftpclient.setcontrolencoding("iso-8859-1"); 
    // ftpclient.enterlocalpassivemode(); 
    ftpclient.setfiletype(ftp.binary_file_type); 
     fis = new fileinputstream(filepath + "/" 
      + filename); 
     log.i("caohai", "filestoragepath00000:"+filestoragepath); 
    string[] path = filestoragepath.split("visitorrecord"); 
     
    boolean fs = ftpclient.storefile(new string((path[1] 
      + "/photo/" + filename).getbytes(), "iso-8859-1"), fis); 
    log.i("caohai", "shifoushangchuanchenggong:"+fs); 
    fis.close(); 
    ftpclient.logout(); 
    //ftpclient.disconnect(); 
   } else {// 如果登录失败 
    ftpclient.disconnect(); 
   } 
  } catch (numberformatexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } catch (socketexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } catch (filenotfoundexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } catch (unsupportedencodingexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } catch (ioexception e) { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:

下一篇: