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

寫在前面

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

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

1 導入jar包

commons-net-3.6.jar

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

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;
	}

注意:獲取遠程文件目錄,上傳和下載方法基於登錄方法github

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數組

若是有什麼更好的方法歡迎留言服務器

相關文章
相關標籤/搜索