java ftp客戶端

jopen 9年前發布 | 2K 次閱讀 Java FTP

apache commons-net可以很方便的實現,但是這個第三方包中對文件夾的刪除與創建(級聯)操作并不是特別的方便。刪除文件夾必須保證該文件夾下沒有任何 文件,創建文件夾也必須要求父文件夾存在。為了方便以后使用方便,對其進行了簡單的封裝,主要針對刪除與文件夾創建。代碼如下:

    package cn.androiddevelop.io;

import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.net.SocketException;  

import org.apache.commons.net.ftp.FTP;  
import org.apache.commons.net.ftp.FTPClient;  
import org.apache.commons.net.ftp.FTPFile;  

/** 
 * ftp客戶端 
 *  
 * @author Yuedong Li 
 *  
 */  
public class FtpClient {  
    private FTPClient client;  

    public FtpClient(String host, String userName, String password)  
            throws SocketException, IOException {  
        initFtpClient(host, 21, userName, password);  
    }  

    public FtpClient(String host, int port, String userName, String password)  
            throws SocketException, IOException {  
        initFtpClient(host, port, userName, password);  
    }  

    /** 
     * 登錄 
     *  
     * @param host 
     * @param port 
     * @param userName 
     * @param password 
     * @throws SocketException 
     * @throws IOException 
     */  
    public void initFtpClient(String host, int port, String userName,  
            String password) throws SocketException, IOException {  
        client = new FTPClient();  
        client.connect(host, port);  
        client.login(userName, password);  
    }  

    /** 
     * 得到所有目錄 
     *  
     * @param remotePath 
     * @return 
     * @throws IOException 
     */  
    public FTPFile[] listFiles(String remotePath) throws IOException {  
        if (client == null)  
            return null;  
        client.changeWorkingDirectory(remotePath);  
        return client.listFiles();  
    }  

    /** 
     * 上傳 
     *  
     * @param localPath 
     *            本地路徑 
     * @param remotePath 
     *            ftp路徑 
     * @return 上傳是否成功 
     * @throws IOException 
     */  
    public boolean upload(String localPath, String remotePath)  
            throws IOException {  
        if (client == null)  
            return false;  
        boolean res = false;  
        FileInputStream fileInputStream = new FileInputStream(localPath);  
        int index = remotePath.lastIndexOf('/');  
        if (index != -1) {  
            client.setFileType(FTP.BINARY_FILE_TYPE);  
            client.changeWorkingDirectory(remotePath.substring(0, index));  
            res = client.storeFile(remotePath.substring(index + 1),  
                    fileInputStream);  
        }  
        fileInputStream.close();  
        return res;  
    }  

    /** 
     * 下載 
     *  
     * @param remotePath 
     *            ftp路徑 
     * @param localPath 
     *            本地路徑 
     * @return 下載是否成功 
     * @throws IOException 
     */  
    public boolean download(String remotePath, String localPath)  
            throws IOException {  
        if (client == null)  
            return false;  
        boolean res = false;  
        FileOutputStream fileOutputStream = new FileOutputStream(localPath);  
        res = client.retrieveFile(remotePath, fileOutputStream);  
        fileOutputStream.flush();  
        fileOutputStream.close();  
        return res;  

    }  

    /** 
     * 刪除文件 
     *  
     * @param remotePath ftp端路徑 
     * @return 
     * @throws IOException 
     */  
    public boolean delete(String remotePath) throws IOException {  
        if (client == null)  
            return false;  

        return client.deleteFile(remotePath) || deleteDirectory(remotePath);  
    }  

    /** 
     * 創建目錄 
     *  
     * @param remotePath 
     * @throws IOException 
     */  
    public boolean makeDirectory(String remotePath) throws IOException {  
        if (client == null)  
            return false;  

        String[] item = remotePath.split("/");  
        String currentPath = "";  
        for (int i = 0; i < item.length - 1; i++) {  
            currentPath = currentPath + "/" + item[i];  
            client.makeDirectory(currentPath);  
        }  

        return client.makeDirectory(remotePath);  
    }  


    /** 
     * 刪除文件 
     *  
     * @param remotePath ftp端路徑 
     * @return 
     * @throws IOException 
     */  
    private boolean deleteDirectory(String remotePath) throws IOException {  
        FTPFile[] files = listFiles(remotePath);  
        for (int i = 0; i < files.length; i++) {  
            if (files[i].isDirectory()) {  
                deleteDirectory(remotePath + "/" + files[i].getName());  
            } else {  
                client.deleteFile(remotePath + "/" + files[i].getName());  
            }  
        }  
        return client.removeDirectory(remotePath);  
    }  

    /** 
     * 重命名 
     *  
     * @param remoteOldPath  
     * @param remoteNewPath 
     * @return 
     * @throws IOException 
     */  
    public boolean rename(String remoteOldPath, String remoteNewPath)  
            throws IOException {  
        if (client == null)  
            return false;  
        return client.rename(remoteOldPath, remoteNewPath);  
    }  

    /** 
     * 退出登錄 
     *  
     * @throws IOException 
     */  
    public void close() throws IOException {  
        if (client != null)  
            client.logout();  
    }  
}  </pre> 


 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!