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

JAVA实现FTP断点上传的方法

程序员文章站 2024-03-05 11:10:42
本文实例讲述了java实现ftp断点上传的方法。分享给大家供大家参考。具体分析如下: 这里主要使用apache中的net包来实现。网址http://commons.apa...

本文实例讲述了java实现ftp断点上传的方法。分享给大家供大家参考。具体分析如下:

这里主要使用apache中的net包来实现。网址http://commons.apache.org/net/。具体包的下载和api文档请看官网。 断点上传就是在上传的过程中设置传输的起始位置。并设置二进制传输。

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 
   */ 
   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 
   */ 
   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.21.171", 21, "test", "test"); 
       system.out.println(myftp.upload("e:\\vp6.flv", "/mis/video/vp6.flv")); 
       myftp.disconnect(); 
     } catch (ioexception e) { 
       system.out.println("连接ftp出错:"+e.getmessage()); 
     } 
   } 
}

希望本文所述对大家的java程序设计有所帮助。