FTP實用工具類


import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import java.io.*;


public class FTPUtil implements AutoCloseable {

    private FTPClient ftpClient;

    public FTPUtil(String serverIP, int port, String userName, String password) throws IOException {
        ftpClient = new FTPClient();
        ftpClient.connect(serverIP, port);
        ftpClient.login(userName, password);
        ftpClient.setBufferSize(1024);//設置上傳緩存大小
        ftpClient.setControlEncoding("UTF-8");//設置編碼
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);//設置文件類型
        ftpClient.setConnectTimeout(15000);//連接超時時間
        ftpClient.setSoTimeout(60000);//讀取操作時間
    }

    /**
     * 下載ftp文件到本地
     *
     * @param remoteFileName 遠程文件名稱
     * @param localFile      本地文件[包含路徑]
     * @return true/false
     * @throws IOException 異常
     */
    public boolean downloadFile(String remoteFileName, String localFile) throws IOException {
        boolean isSucc;
        File outFileName = new File(localFile);
        if (ftpClient == null)
            throw new IOException("ftp server not login");
        try (OutputStream outputStream = new FileOutputStream(outFileName)) {
            isSucc = ftpClient.retrieveFile(remoteFileName, outputStream);
        }
        return isSucc;
    }

    /**
     * 上傳文件制定目錄
     *
     * @param remoteFileName 遠程文件名
     * @param localFile      本地文件[必須帶路徑]
     * @return true/false
     * @throws IOException 異常
     */
    public boolean uploadFile(String remoteFileName, String localFile) throws IOException {
        boolean isSucc;
        try (InputStream inputStream = new FileInputStream(localFile)) {
            if (ftpClient == null)
                throw new IOException("ftp server not login");
            isSucc = ftpClient.storeFile(remoteFileName, inputStream);
        }
        return isSucc;
    }

    /**
     	* 切換目錄
     *
     * @param path 創建目錄
     * @return 創建標誌
     * @throws IOException 異常
     */
    public boolean changeDirectory(String path) throws IOException {
        return ftpClient.changeWorkingDirectory(path);
    }

    /**
     * 創建目錄
     *
     * @param path 創建目錄
     * @return 創建標誌
     * @throws IOException 異常
     */
    public boolean createDirectory(String path) throws IOException {
        return ftpClient.makeDirectory(path);
    }

    /***
  	    * 判斷文件是否存在
     * @param ftpPath
     * @return
     */
    public boolean isExsits(String ftpPath, String fileName) throws IOException {
        if (changeDirectory(ftpPath)) {
            FTPFile[] files = ftpClient.listFiles(fileName);
            if (files != null && files.length > 0) {
                return true;
            }
        }
        return false;
    }

    /**
         * 自動關閉資源
     */
    @Override
    public void close() throws Exception {
        if (ftpClient != null && ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章