java 实现ftp上传如何创建文件夹

package com.sinosoft.util;

 

 

import java.io.IOException;

 

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

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

 

 

public class Test {

    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 dirname) {

        try {

            ftp.makeDirectory(dirname);

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

        } catch (Exception ex) {

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

        }

    }

 

    // 断开host连接

    public void disconnect() {

        try {

            ftp.disconnect();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }

 

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

    public static void main(String[] args) {

        Test ftpupload = new Test();

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

            ftpupload.createDir("/testUploads/aafe");

            ftpupload.disconnect();

        }

    }

}