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

9 篇文章 0 订阅

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();

        }

    }

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值