天天看點

FTP-使用ftp4j操作FTP_1

說明:jar包版本ftp4j-1.7.2.jar

一、建立項目

    項目名稱:ftpdemo

二、添加jar包

    1.在項目中建立lib目錄

        /lib

    2.在lib目錄下添加jar包

        ftp4j-1.7.2.jar

        junit-4.4.jar        

三、添加屬性檔案

    1.在項目中建立conf目錄

        /conf

    2.在conf目錄添加屬性檔案

        屬性檔案名稱:ftpinfo.properties

        屬性檔案内容:

        ftp_server_address=ftp://192.168.3.200:21

        ftp_server_username=teacher

        ftp_server_password=*******

四、建立工具類

    1.在src下建立包

        包名:cn.jbit

    2.在包下建立建立工具類

        1.字元串處理工具類

            類名:StringUtils.java

            類内容:

            public class StringUtils {

                public static boolean isEmpty(String dir) {

                    if (null != dir || !"".equals(dir)) {

                        return false;

                    }

                    return true;

                }

                public static boolean isEmpty(File[] files) {

                    if (null != files) {

                public static boolean isEmpty(String[] dirs) {

                    if (null != dirs) {

            }

        2.加載屬性檔案工具類

            類名:PropUtils.java

                /**

                 * 屬性檔案工具類

                 * @author dengdashuai

                 *

                 */

                public class PropUtils {

                    private static Properties properties = new Properties();

                    private PropUtils(){}

                    static{

                        try {

                            properties.load(PropUtils.class.getClassLoader().getResourceAsStream("ftpinfo.properties"));

                        } catch (IOException e) {

                            e.printStackTrace();

                        }

                    public static String getString(String key){

                        return properties.getProperty(key);

        3.操作FTP工具類

                public class FTPUtils {

                    private static FTPUtils ftp;

                    // FTP服務位址

                    private static String ADDRESS;

                    // FTP登入使用者名

                    private static String USERNAME;

                    // FTP登入密碼

                    private static String PASSWORD;

                    // 無參構造

                    private FTPUtils() {

                    /*

                     * 加載

                     *     ftp位址

                     *     ftp登入名

                     *     ftp登入密碼

                     */

                    static {

                        ADDRESS = PropUtils.getString("ftp_server_address");

                        USERNAME = PropUtils.getString("ftp_server_username");

                        PASSWORD = PropUtils.getString("ftp_server_password");

                    /**

                     * 執行個體化對象

                     * 

                     * @return FTPUtils

                    public static FTPUtils getInstance() {

                        if (ftp == null) {

                            ftp = new FTPUtils();

                        return ftp;

                     * 擷取FTP用戶端對象

                    private FTPClient getClient() throws Exception {

                        // 建立FTP用戶端對象

                        FTPClient client = new FTPClient();

                        // 設定編碼

                        client.setCharset("utf-8");

                        // 二進制

                        client.setType(FTPClient.TYPE_BINARY);

                        // 建立URL對象

                        URL url = new URL(FTPUtils.ADDRESS);

                        // 擷取端口

                        int port = url.getPort() < 1 ? 21 : url.getPort();

                        // 建立連接配接

                        client.connect(url.getHost(), port);

                        // 登入

                        client.login(FTPUtils.USERNAME, FTPUtils.PASSWORD);

                        // 用戶端對象

                        return client;

                     * 登出用戶端連接配接 

                     * client:FTP用戶端對象

                    private void logout(FTPClient client) throws Exception {

                        if (client != null) {

                            try {

                                // 有些FTP伺服器未實作此功能,若未實作則會出錯

                                client.logout(); // 登出

                            } catch (FTPException fe) {

                            } catch (Exception e) {

                                throw e;

                            } finally {

                                //判斷用戶端是否連接配接,如果已經連接配接服務傳回true,否則傳回false

                                if (client.isConnected()) { 

                                    // 斷開連接配接

                                    client.disconnect(true);

                                }

                            }

                     * 判斷目前為檔案還是目錄 

                     * client:FTP用戶端對象 dir:目錄

                    private int getFileType(FTPClient client, String dir) throws Exception {

                        FTPFile[] files = null;

                            files = client.list(dir);

                        } catch (Exception e) {

                            return -1;

                        if (files.length > 1) {

                            return FTPFile.TYPE_DIRECTORY;

                        } else if (files.length == 1) {

                            FTPFile f = files[0];

                            if (f.getType() == FTPFile.TYPE_DIRECTORY) {

                                return FTPFile.TYPE_DIRECTORY;

                            String path = dir + "/" + f.getName();

                                int len = client.list(path).length;

                                if (len == 1) {

                                    return FTPFile.TYPE_DIRECTORY;

                                } else {

                                    return FTPFile.TYPE_FILE;

                                return FTPFile.TYPE_FILE;

                        } else {

                                client.changeDirectory(dir);

                                client.changeDirectoryUp();

                                return -1;

                     * 判斷檔案或目錄是否存在 client:FTP用戶端對象 dir:目錄

                    private boolean exists(FTPClient client, String dir) throws Exception {

                        return getFileType(client, dir) != -1;

                     * 建立目錄 client:FTP用戶端對象 dir:目錄

                    private void mkdirs(FTPClient client, String dir) throws Exception {

                        if (dir == null) {

                            return;

                        dir = dir.replace("//", "/");

                        String[] dirs = dir.split("/");

                        for (int i = 0; i < dirs.length; i++) {

                            dir = dirs[i];

                            if (!StringUtils.isEmpty(dir)) {

                                if (!exists(client, dir)) {

                                    client.createDirectory(dir);

                     * 擷取FTP目錄 url:原FTP目錄 dir:目錄

                    private URL getURL(URL url, String dir) throws Exception {

                        String path = url.getPath();

                        if (!path.endsWith("/") && !path.endsWith("//")) {

                            path += "/";

                        if (dir.startsWith("/")) {

                            dir = dir.substring(1);

                        path += dir;

                        return new URL(url, path);

                     * 擷取FTP目錄

                     * dir:目錄

                    private URL getURL(String dir) throws Exception {

                        return getURL(new URL(FTPUtils.ADDRESS), dir);

                    本文轉自  素顔豬  51CTO部落格,原文連結:http://blog.51cto.com/suyanzhu/1566322