推薦一個java操作ftp的工具類

寫在前面

作爲經常使用電腦整理文件的童鞋,應該都使用過從ftp服務器上傳下載文件,那麼今天就瞭解下如何通過java程序操作ftp服務的文件

首先你要知道ftp的ip,路徑,端口,有操作權限的賬號和密碼

1 導入jar包

 commons-net-3.6.jar

這個jar包用來設置編碼,經過測試,不加也可用

2 工具類中主要方法

2.1 登陸ftp

    /**
     * 驗證登錄
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @return
     */
    public boolean login(String ip,int port, String name, String pwd) {
        try {
            ftp = new FTPClient();
            ftp.connect(ip, port);
            System.out.println(ftp.login(name, pwd));
            if(!ftp.login(name, pwd)){
                return false;
            }
            ftp.setCharset(Charset.forName("UTF-8"));
            ftp.setControlEncoding("UTF-8");

        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
注意:獲取遠程文件目錄,上傳和下載方法基於登陸方法

2.2 獲取遠程文件目錄

    /**
     * 獲取ftp某一文件(路徑)下的文件名字,用於查看文件列表
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotedir 遠程地址目錄
     * @return
     */
    public boolean getFilesName(String ip,int port, String name, String pwd, String remotedir) {
        try {
            if(!login(ip, port, name, pwd)){
                return false;
            }
            //獲取ftp裏面,指定文件夾 裏面的文件名字,存入數組中
            FTPFile[] files = ftp.listFiles(remotedir);
            //打印出ftp裏面,指定文件夾 裏面的文件名字
            for (int i = 0; i < files.length; i++) {
                System.out.println(files[i].getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            this.close();
        }
        return true;
    }

2.3 上傳文件

    /**
     * 上傳文件 方法一
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 遠程地址文件路徑
     * @param localpath 本地文件路徑
     * @return
     */
    public boolean putFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
            if(!login(ip, port, name, pwd)){
                return false;
            }
            //將本地的 localpath 文件上傳到ftp的根目錄文件夾下面,並重命名爲 remotepath中的名字
             return ftp.storeFile(remotepath, new FileInputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            this.close();
        }
    }
    
    /**
     * 上傳文件的第二種方法,優化了傳輸速度
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 遠程地址文件路徑
     * @param localpath 本地文件路徑
     * @return
     */
    public boolean putFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
            if(!login(ip, port, name, pwd)){
                return false;
            }
            os = ftp.storeFileStream(remotepath);
            fis = new FileInputStream(new File(localpath));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = fis.read(b)) != -1) {
                os.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            this.close();
        }
        return true;
    }

2.4 下載文件

    /**
     * 下載文件 方法一
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 遠程地址文件路徑
     * @param localpath 本地文件路徑
     * @return
     */
    public boolean getFileOne(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
            if(!login(ip, port, name, pwd)){
                return false;
            }
            //將ftp資源中 remotepath 文件下載到本地目錄文件夾下面,並重命名爲 localpath 中的名字
            return ftp.retrieveFile(remotepath, new FileOutputStream(new File(localpath)));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally{
            this.close();
        }
    }
    
    /**
     * 下載文件的第二種方法,優化了傳輸速度
     * @param ip
     * @param port
     * @param name
     * @param pwd
     * @param remotepath 遠程地址文件路徑
     * @param localpath  本地文件路徑
     * @return
     */
    public boolean getFileTwo(String ip,int port, String name, String pwd,String remotepath,String localpath) {
        try {
            if(!login(ip, port, name, pwd)){
                return false;
            }
            is = ftp.retrieveFileStream(remotepath);
            fos = new FileOutputStream(new File(localpath));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = is.read(b)) != -1) {
                fos.write(b,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }finally {
            this.close();
        }
        return true;
    }

3 源碼

當然上面代碼只是重要的部分,如果有問題可去github自行下載 charmsongo

如果有什麼更好的方法歡迎留言
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章