ftp创建多级文件夹(可用)

package com.sinosoft.util;

 

 

import java.io.IOException;

import java.io.UnsupportedEncodingException;

 

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

import org.apache.commons.net.ftp.FTPReply;

 

 

public class Test {

    static FTPClient ftp;

 

    public boolean connect(String path, String addr, int port, String username, String password) {

        try {

            // FTPClient ftp = new FTPHTTPClient(addr, port, username, password);

            ftp = new FTPClient();

            int reply;

            ftp.connect(addr);

            System.out.println("连接到:" + addr + ":" + port);

            System.out.print(ftp.getReplyString());

            reply = ftp.getReplyCode();

 

            if (!FTPReply.isPositiveCompletion(reply)) {

                ftp.disconnect();

                System.err.println("FTP目标服务器积极拒绝.");

                System.exit(1);

                return false;

            } else {

                ftp.login(username, password);

                ftp.enterLocalPassiveMode();

                ftp.setFileType(FTPClient.BINARY_FILE_TYPE);

                ftp.changeWorkingDirectory(path);

                System.out.println("已连接:" + addr + ":" + port);

                return true;

            }

        } catch (Exception ex) {

            ex.printStackTrace();

            System.out.println(ex.getMessage());

            return false;

        }

    }

 

    // 然后再利用ftpclient的makeDirectory方法创建文件夹

    public void createDir(String remote, FTPClient ftpClient)

        throws UnsupportedEncodingException, IOException {

        // try {

        // ftp.makeDirectory(dirname);

        // System.out.println("在目标服务器上成功建立了文件夹: " + dirname);

        // } catch (Exception ex) {

        // System.out.println(ex.getMessage());

        // }

 

        String directory = remote.substring(0, remote.lastIndexOf("/") + 1);

        if (!directory.equalsIgnoreCase("/") && !ftpClient.changeWorkingDirectory(

            new String(directory.getBytes("GBK"), "iso-8859-1"))) {

            // 如果远程目录不存在,则递归创建远程服务器目录

            int start = 0;

            int end = 0;

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

                start = 1;

            } else {

                start = 0;

            }

            end = directory.indexOf("/", start);

            while (true) {

                String subDirectory = new String(remote.substring(start, end).getBytes("GBK"),

                    "iso-8859-1");

                if (!ftpClient.changeWorkingDirectory(subDirectory)) {

                    if (ftpClient.makeDirectory(subDirectory)) {

                        ftpClient.changeWorkingDirectory(subDirectory);

                    } else {

                        System.out.println("创建目录失败");

                    }

                }

 

                start = end + 1;

                end = directory.indexOf("/", start);

 

                // 检查所有目录是否创建完毕

                if (end <= start) {

                    break;

                }

            }

        }

    }

 

    // 断开host连接

    public void disconnect() {

        try {

            ftp.disconnect();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

    // 最后是程序的调用方法

    public static void main(String[] args)

        throws IOException {

        Test ftpupload = new Test();

        if (ftpupload.connect("", "218.60.42.207", 21, "dawei", "gjjl302")) {

            ftpupload.createDir("/Zdy/upload22/bbs/gggk", ftp);

            ftpupload.disconnect();

        }

    }

 

}

你可能感兴趣的:(Java,ftp)