FTP 上传 下载


复制代码
package android.ftps;
 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 {
 // 上传例子
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();
 }
 // 下载例子
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();
 }
 FTPClient ftpClient = null;
 public String rootPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";
 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();
   }
 }
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值