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

java基于Apache FTP点断续传的文件上传和下载

程序员文章站 2024-03-12 17:34:26
基于apache ftp实现文件上传下载工具 ,上传文件时需要考虑以下问题(实例是续传功能): (1)、 ftp服务器是否存在改目录,如果不存在目录则需要创建目录。 (...

基于apache ftp实现文件上传下载工具 ,上传文件时需要考虑以下问题(实例是续传功能):

(1)、 ftp服务器是否存在改目录,如果不存在目录则需要创建目录。

(2)、判断上传文件是否已经存在,如果存在是需要删除后再上传还是续传。

1、上传或下载状态的枚举类:

package com.scengine.wtms.utils.ftp; 
 
public enum uploadstatus 
{ 
  file_exits(0), create_directory_success(1), create_directory_fail(2), upload_from_break_success(3), upload_from_break_faild(4), download_from_break_success(5), download_from_break_faild(6), upload_new_file_success(7), upload_new_file_failed(8), delete_remote_success(9), delete_remote_faild(10),remote_bigger_local(11),remote_smaller_locall(12); 
 
  private int status; 
 
  public int getstatus() 
  { 
    return status; 
  } 
 
  public void setstatus(int status) 
  { 
    this.status = status; 
  } 
 
  uploadstatus(int status) 
  { 
    this.status = status; 
  } 
} 

2、工具类代码:

package com.scengine.wtms.utils.ftp; 
 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.fileoutputstream; 
import java.io.ioexception; 
import java.io.inputstream; 
import java.io.outputstream; 
import java.io.printwriter; 
import org.apache.commons.net.printcommandlistener; 
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; 
 
public class continueftp 
{ 
 
  private ftpclient ftpclient = new ftpclient(); 
 
  /** 
   * 对象构造 设置将过程中使用到的命令输出到控制台 
   */ 
  public continueftp() 
  { 
    this.ftpclient.addprotocolcommandlistener(new printcommandlistener(new printwriter(system.out))); 
  } 
 
  /** 
   * 
   * java编程中用于连接到ftp服务器 
   * 
   * @param hostname 
   *      主机名 
   * 
   * @param port 
   *      端口 
   * 
   * @param username 
   *      用户名 
   * 
   * @param password 
   *      密码 
   * 
   * @return 是否连接成功 
   * 
   * @throws ioexception 
   */ 
 
  public boolean connect(string hostname, int port, string username, string password) throws ioexception 
  { 
 
    ftpclient.connect(hostname, port); 
 
    if (ftpreply.ispositivecompletion(ftpclient.getreplycode())) 
    { 
 
      if (ftpclient.login(username, password)) 
      { 
        return true; 
      } 
    } 
    disconnect(); 
    return false; 
 
  } 
 
  /** 
   * 
   * 从ftp服务器上下载文件 
   * 
   * @param remote 
   *      远程文件路径 
   * 
   * @param local 
   *      本地文件路径 
   * 
   * @return 是否成功 
   * 
   * @throws ioexception 
   */ 
 
  @suppresswarnings("resource") 
  public boolean download(string remote, string local) throws ioexception 
  { 
 
    ftpclient.enterlocalpassivemode(); 
 
    ftpclient.setfiletype(ftp.binary_file_type); 
 
    boolean result; 
 
    file f = new file(local); 
 
    ftpfile[] files = ftpclient.listfiles(remote); 
 
    if (files.length != 1) 
    { 
      system.out.println("远程文件不唯一"); 
      return false; 
    } 
 
    long lremotesize = files[0].getsize(); 
 
    if (f.exists()) 
    { 
      outputstream out = new fileoutputstream(f, true); 
      system.out.println("本地文件大小为:" + f.length()); 
 
      if (f.length() >= lremotesize) 
      { 
 
        system.out.println("本地文件大小大于远程文件大小,下载中止"); 
 
        return false; 
 
      } 
 
      ftpclient.setrestartoffset(f.length()); 
 
      result = ftpclient.retrievefile(remote, out); 
 
      out.close(); 
 
    } else 
    { 
 
      outputstream out = new fileoutputstream(f); 
      result = ftpclient.retrievefile(remote, out); 
      out.close(); 
    } 
 
    return result; 
 
  } 
 
  /** 
   * 
   * 上传文件到ftp服务器,支持断点续传 
   * 
   * @param local 
   *      本地文件名称,绝对路径 
   * 
   * @param remote 
   *      远程文件路径,使用/home/directory1/subdirectory/file.ext 
   *      按照linux上的路径指定方式,支持多级目录嵌套,支持递归创建不存在的目录结构 
   * 
   * @return 上传结果 
   * 
   * @throws ioexception 
   */ 
 
  @suppresswarnings("resource") 
  public uploadstatus upload(string local, string remote) throws ioexception 
  { 
 
    // 设置passivemode传输 
 
    ftpclient.enterlocalpassivemode(); 
 
    // 设置以二进制流的方式传输 
 
    ftpclient.setfiletype(ftp.binary_file_type); 
 
    uploadstatus result; 
 
    // 对远程目录的处理 
 
    string remotefilename = remote; 
 
    if (remote.contains("/")) 
    { 
 
      remotefilename = remote.substring(remote.lastindexof("/") + 1); 
 
      string directory = remote.substring(0, remote.lastindexof("/") + 1); 
 
      if (!directory.equalsignorecase("/") && !ftpclient.changeworkingdirectory(directory)) 
      { 
 
        // 如果远程目录不存在,则递归创建远程服务器目录 
 
        int start = 0; 
 
        int end = 0; 
 
        if (directory.startswith("/")) 
        { 
 
          start = 1; 
 
        } else 
        { 
 
          start = 0; 
 
        } 
 
        end = directory.indexof("/", start); 
 
        while (true) 
        { 
 
          string subdirectory = remote.substring(start, end); 
 
          if (!ftpclient.changeworkingdirectory(subdirectory)) 
          { 
 
            if (ftpclient.makedirectory(subdirectory)) 
            { 
 
              ftpclient.changeworkingdirectory(subdirectory); 
 
            } else 
            { 
 
              system.out.println("创建目录失败"); 
 
              return uploadstatus.create_directory_fail; 
 
            } 
 
          } 
 
          start = end + 1; 
 
          end = directory.indexof("/", start); 
 
          // 检查所有目录是否创建完毕 
 
          if (end <= start) 
          { 
 
            break; 
 
          } 
 
        } 
 
      } 
 
    } 
 
    // 检查远程是否存在文件 
 
    ftpfile[] files = ftpclient.listfiles(remotefilename); 
 
    if (files.length == 1) 
    { 
 
      long remotesize = files[0].getsize(); 
 
      file f = new file(local); 
 
      long localsize = f.length(); 
 
      if (remotesize == localsize) 
      { 
 
        return uploadstatus.file_exits; 
 
      } else if (remotesize > localsize) 
      { 
 
        return uploadstatus.remote_bigger_local; 
 
      } 
 
      // 尝试移动文件内读取指针,实现断点续传 
 
      inputstream is = new fileinputstream(f); 
 
      if (is.skip(remotesize) == remotesize) 
      { 
 
        ftpclient.setrestartoffset(remotesize); 
 
        if (ftpclient.storefile(remote, is)) 
        { 
 
          return uploadstatus.upload_from_break_success; 
 
        } 
 
      } 
 
      // 如果断点续传没有成功,则删除服务器上文件,重新上传 
 
      if (!ftpclient.deletefile(remotefilename)) 
      { 
 
        return uploadstatus.delete_remote_faild; 
 
      } 
 
      is = new fileinputstream(f); 
 
      if (ftpclient.storefile(remote, is)) 
      { 
 
        result = uploadstatus.upload_new_file_success; 
 
      } else 
      { 
 
        result = uploadstatus.upload_new_file_failed; 
 
      } 
 
      is.close(); 
 
    } else 
    { 
 
      inputstream is = new fileinputstream(local); 
 
      if (ftpclient.storefile(remotefilename, is)) 
      { 
 
        result = uploadstatus.upload_new_file_success; 
 
      } else 
      { 
 
        result = uploadstatus.upload_new_file_failed; 
 
      } 
 
      is.close(); 
    } 
 
    return result; 
 
  } 
 
  /** 
   * 
   * 断开与远程服务器的连接 
   * 
   * @throws ioexception 
   */ 
 
  public void disconnect() throws ioexception 
  { 
 
    if (ftpclient.isconnected()) 
    { 
      ftpclient.disconnect(); 
    } 
 
  } 
 
  public static void main(string[] args) 
  { 
    continueftp myftp = new continueftp(); 
    try 
    { 
 
      myftp.connect("192.168.1.200", 21, "duser", "htpduserxp32"); 
 
      system.out.println(myftp.upload("c:\\users\\administrator\\desktop\\swing.drawer.jar", "/jars/swing.drawer.jar")); 
 
      myftp.disconnect(); 
 
    } catch (ioexception e) 
    { 
 
      system.out.println("连接ftp出错:" + e.getmessage()); 
 
    } 
 
  } 
 
} 

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