FTP 断点续传工具

本文档介绍了一个用于文件断点续传的FTP工具类,适用于在上传或下载大文件时,因网络问题中断后能从上次停止的位置继续操作。
摘要由CSDN通过智能技术生成

**
* 类描述: 文件断点续传工具类
* @author:赵博
* @since:2017-6-14 下午13:41
* @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
*
*
*/
public class ContinueFTP2 {

 public FTPClient ftpClient = new FTPClient(); 
 private String ftpURL,username,pwd,ftpport,file1,file2; 


 /******
  * 
  * @param _ftpURL     文件服务器长传地址
  * @param _username   用户名
  * @param _pwd        密码
  * @param _ftpport    端口
  * @param _file1            要传输的文件
  * @param _file2            目的文件夹
  */
 public ContinueFTP2(String _ftpURL,String _username,String _pwd,String _ftpport,String _file1,String _file2 ){   
        //设置将过程中使用到的命令输出到控制台   
     ftpURL = _ftpURL; 
     username = _username; 
     pwd = _pwd; 
     ftpport = _ftpport; 
     file1 = _file1; 
     file2 = _file2; 
     ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));   
    } 

     /** 
     * 连接到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);   
        ftpClient.setControlEncoding("GBK");   
        if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){   
            if(ftpClient.login(username, password)){   
                return true;   
            }   
        }   
        disconnect();   
        return false;   
    }  


    /** *//**  
     * 断开与远程服务器的连接  
     * @throws IOException  
     */  
    public void disconnect() throws IOException{   
        if(ftpClient.isConnected()){   
            ftpClient.disconnect();   
        }   
    } 


    /** *//**  
     * 递归创建远程服务器目录  
     * @param remote 远程服务器文件绝对路径  
     * @param ftpClient FTPClient 对象  
     * @return 目录创建是否成功  
     * @throws IOException  
     */  
    public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{   
        UploadStatus status = UploadStatus.Create_Directory_Success;   
        String directory = remote.substring(0,remote.lastIndexOf("/")+1);   
        if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){   
            //如果远程目录不存在,则递归创建远程服务器目录   
            int start=0;   
            int end = 0;   
            if(directory.startsWith("/")){   
                start = 1;   
            }else{   
                start = 0;   
            }   
            end = directory.indexOf("/",start);   
            while(true){   
                String subDirectory = new String(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");   
             
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吟风笛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值