zoukankan      html  css  js  c++  java
  • edtftpj让Java上传FTP文件支持断点续传

    在用Java实现FTP上传文件功能时,特别是上传大文件的时候,可以需要这样的功能:程序在上传的过程中意外终止了,文件传了一大半,想从断掉了地方继续传;或者想做类似迅雷下载类似的功能,文件太大,今天传一半,睡一觉去先,明天继续传。

    Java上传FTP文件,用的比较多的工具是apache的commons-net。如果想用commons-net实现FTP上传的断点续传还是有点麻烦。

    除了commons-net之外,还有很多非常优秀的FTP工具,这里使用edtftpj这个工具来实现断点续传。

    下载:http://www.enterprisedt.com/


    这里下载免费版,相对收费的版本少一些功能,但是足够使用了。

    下载完成,解压后,在lib文件夹下,可以看到edtftpj.jar文件。放到项目中去。

    下面是FTP断点续传上传文件的代码:

    import java.io.File;  
    import java.io.IOException;  
      
    import com.enterprisedt.net.ftp.FTPException;  
    import com.enterprisedt.net.ftp.FTPTransferType;  
    import com.enterprisedt.net.ftp.FileTransferClient;  
    import com.enterprisedt.net.ftp.WriteMode;  
      
    public class FtpTool  
    {  
        /** 
         * edtptpj的上传工具 
         */  
        private FileTransferClient ftp;  
      
        /** 
         * FTP IP 
         */  
        private String ip;  
      
        /** 
         * FTP端口号 
         */  
        private int port;  
      
        /** 
         * FTP用户名 
         */  
        private String username;  
      
        /** 
         * FTP密码 
         */  
        private String password;  
      
        /** 
         *  
         * 构造方法,初始化FTP IP、FTP端口、FTP用户名、FTP密码 
         *  
         * @param ip 
         *            FTP IP 
         * @param port 
         *            FTP端口 
         * @param username 
         *            FTP用户名 
         * @param password 
         *            FTP密码 
         */  
        public FtpTool(String ip, int port, String username, String password)  
        {  
            this.ip = ip;  
            this.port = port;  
            this.username = username;  
            this.password = password;  
        }  
      
        /** 
         *  
         * 连接FTP 
         *  
         * @throws FTPException 
         *             FTPException 
         * @throws IOException 
         *             IOException 
         * @author XXG 
         */  
        public void connect() throws FTPException, IOException  
        {  
            ftp = new FileTransferClient();  
            ftp.setRemoteHost(ip);  
            ftp.setRemotePort(port);  
            ftp.setUserName(username);  
            ftp.setPassword(password);  
      
            //设置二进制方式上传  
            ftp.setContentType(FTPTransferType.BINARY);  
            ftp.connect();  
        }  
      
        /** 
         *  
         * 上传本地文件到FTP服务器上,文件名与原文件名相同 
         *  
         * @param localFile 
         *            本地文件路径 
         * @param remoteFilePath 
         *            上传到FTP服务器所在目录(该目录必须已经存在) 
         * @throws IOException 
         *             IOException 
         * @throws FTPException 
         *             FTPException 
         * @author XXG 
         *  
         */  
        public void resumeUpload(String localFile, String remoteFilePath)  
                throws FTPException, IOException  
        {  
            File local = new File(localFile);  
              
            //FTP文件完整路径  
            String remoteFileFullPath = remoteFilePath + local.getName();  
              
            //上传:WriteMode.RESUME表示断点续传  
            ftp.uploadFile(localFile, remoteFileFullPath, WriteMode.RESUME);  
        }  
      
        /** 
         *  
         * 关闭FTP连接 
         *  
         * @throws IOException 
         *             IOException 
         * @throws FTPException 
         *             FTPException 
         * @author XXG 
         */  
        public void close() throws FTPException, IOException  
        {  
            ftp.disconnect();  
        }  
    }  

    下面是main方法测试断点续传功能(测试的时候可以在上传的中途关闭程序,再开始上传,看是否在续传):

    public static void main(String[] args)  
    {  
        FtpTool ftpTool = new FtpTool("192.168.7.49", 21, "editor", "tvm_editor");  
        try  
        {  
            ftpTool.connect();  
            try  
            {  
                System.out.println("开始上传文件...");  
                ftpTool.resumeUpload("E:\软件\eclipse-jee-indigo-SR2-win32.zip", "wucao/aa/");  
                System.out.println("完成上传文件。");  
            }  
            catch (Exception e)   
            {  
                e.printStackTrace();  
            }  
            finally  
            {  
                ftpTool.close();  
            }  
        }  
        catch (Exception e)  
        {  
            e.printStackTrace();  
        }  
    }  

    在上面的代码中,ftp.uploadFile(localFile,remoteFileFullPath, WriteMode.RESUME) 这句代码中的第三个参数WriteMode.RESUME即表示断点续传。

    除了WriteMode.RESUME方式上传,还有其他两种方式:

    WriteMode.OVERWRITE:覆盖上传,就是上次如果没传完,这次删掉上次的重新传。

    WriteMode.APPEND:续写,就是如果FTP服务器上存在同名的文件,就接着它后面续加。

    注意续写和断点续传区分:比如有个100M的文件上传,上次传了80M,那么用RESUME方式的话,传完剩下的20M就完成了,但对于APPEND,它会接在80M后面重新传,最后成功的时候FTP上的文件大小是180M。

    最后要注意的是:并不是所有FTP服务器都支持断点续传,有断点续传的服务器也可以关闭断点续传的功能,不支持断点续传的服务器使用RESUME会抛出异常。所以,在使用FTP断点续传之前,首先要确认一下FTP服务器本身是否支持断点续传。

    本文转自:http://blog.csdn.net/kazeik/article/details/8260067

  • 相关阅读:
    yolo_to_onnx ValueError: need more tan 1 value to unpack
    yolo_to_onnx killed
    C++ 实现二维矩阵的加减乘等运算
    Leetcode 1013. Partition Array Into Three Parts With Equal Sum
    Leetcode 1014. Best Sightseeing Pair
    Leetcode 121. Best Time to Buy and Sell Stock
    Leetcode 219. Contains Duplicate II
    Leetcode 890. Find and Replace Pattern
    Leetcode 965. Univalued Binary Tree
    Leetcode 700. Search in a Binary Search Tree
  • 原文地址:https://www.cnblogs.com/dreammyle/p/4257005.html
Copyright © 2011-2022 走看看