Example usage for org.apache.commons.net.ftp FTPClient makeDirectory

List of usage examples for org.apache.commons.net.ftp FTPClient makeDirectory

Introduction

In this page you can find the example usage for org.apache.commons.net.ftp FTPClient makeDirectory.

Prototype

public boolean makeDirectory(String pathname) throws IOException 

Source Link

Document

Creates a new subdirectory on the FTP server in the current directory (if a relative pathname is given) or where specified (if an absolute pathname is given).

Usage

From source file:de.jwi.ftp.FTPUploader.java

private static String uploadFiles(FTPClient ftp, String ftpServerPath, List files) throws IOException {
    boolean rc = false;
    String rcs = "error";
    List l = null;/* www.  j a v a  2s.c  o  m*/

    rc = ftp.makeDirectory(ftpServerPath);
    boolean rc1 = ftp.changeWorkingDirectory(ftpServerPath);
    System.out.println("cd: " + ftpServerPath);

    if (rc1) {
        Iterator it = files.iterator();
        while (it.hasNext()) {
            File f = (File) it.next();

            if (f.isDirectory()) {
                String name = f.getName();

                String newPath = ftpServerPath + "/" + name;

                l = Arrays.asList(f.listFiles());

                rcs = uploadFiles(ftp, newPath, l);

                rc1 = ftp.changeWorkingDirectory(ftpServerPath);

                if (!rc1) {
                    return "failed to chdir to " + ftpServerPath;
                }
            } else {
                rcs = uploadFile(ftp, ftpServerPath, f);
            }
        }
    } else {
        rcs = "failed to chdir to " + ftpServerPath;
    }
    return rcs;
}

From source file:com.hdfstoftp.main.HdfsToFtp.java

/**
 * ?/*from   w  ww.j a v  a 2s .c  o  m*/
 * 
 * @param srcFS
 *            
 * @param src
 *            ?
 * @param dst
 *            
 * @param queryStr
 *            
 * @param deleteSource
 *            ??
 * @param overwrite
 *            ????
 * @return boolean
 * @throws Exception
 */
private static boolean copyFromHDFSToFTP(Config config) throws Exception {
    // ?hdfs
    Configuration conf = new Configuration();
    FileSystem srcFS = FileSystem.get(conf);
    long start = System.currentTimeMillis();
    boolean isRename = config.isRenameUploaded();
    int retryTimes = config.getRetryTimes();
    // ?
    String dstPath = config.getDestDir();
    Path src = new Path(config.getSouceDir());
    FileStatus fileStatus = srcFS.getFileStatus(src);
    String subDir = null;
    if (fileStatus.isDirectory()) {// 
        if (isRename) {// ??rename
            subDir = Config.RENAME_DIR;
            srcFS.mkdirs(new Path(fileStatus.getPath(), subDir));
        }
        int threadNum = config.getThreadNum();
        // 
        ExecutorService threadPool = Executors.newFixedThreadPool(threadNum);
        // ?ftp
        FTPClientPool ftpPool = new FTPClientPool(threadNum, new FtpClientFactory(config.getFTPClientConfig()));
        FTPClient ftpClient = ftpPool.borrowObject();
        // ?
        ftpClient.makeDirectory(dstPath);
        ftpPool.returnObject(ftpClient);
        // ??
        FileStatus contents[] = srcFS.listStatus(src);
        long beginFilter = 0;
        long endFileter = 0;

        if (config.getCommandLine().hasOption("d") || config.getCommandLine().hasOption("h")
                || config.getCommandLine().hasOption("t")) {// ?"["
            beginFilter = System.currentTimeMillis();
            Long[] timeRange = parseTimeRange(config.getCommandLine());
            contents = getNewContents(timeRange, contents);
            endFileter = System.currentTimeMillis();
        }
        // ?
        if (config.getCommandLine().hasOption("r")) {// "["??
            beginFilter = System.currentTimeMillis();
            contents = getFilterContents(config.getCommandLine().getOptionValue("r").trim(), contents);
            endFileter = System.currentTimeMillis();
        }
        logger.info("total file count:" + contents.length);
        Map<String, String> fileNameMap = null;
        long beginSkip = 0;
        long endSkip = 0;
        boolean overwrite = true;
        if (config.getCommandLine().hasOption("o")) {
            overwrite = "true".equals(config.getCommandLine().getOptionValue("o").trim());
        }
        if (!overwrite) {// ?????
            beginSkip = System.currentTimeMillis();
            fileNameMap = getFileNameMap(dstPath, ftpPool);
            endSkip = System.currentTimeMillis();
        }
        int skiped = 0;

        List<Future<?>> futureList = new ArrayList<Future<?>>();
        for (int i = 0; i < contents.length; i++) {
            if (!overwrite && fileNameMap.containsKey(contents[i].getPath().getName())) {
                // 
                skiped++;
                Log.info("skiped filename:" + contents[i].getPath().getName());
                continue;
            }
            if (contents[i].isDirectory()) {
                continue;
            }
            // ???
            Future<?> future = threadPool.submit(new UploadFileTask(srcFS, contents[i].getPath(),
                    new Path(dstPath, contents[i].getPath().getName()), ftpPool, false, isRename, subDir,
                    retryTimes));
            futureList.add(future);
        }
        int transfered = 0;
        int failed = 0;
        for (Future<?> future : futureList) {
            Boolean computeResult = (Boolean) future.get();
            if (computeResult) {
                transfered++;
                if (transfered % 50 == 0 || transfered == contents.length) {
                    logger.info("have transfered:" + transfered + " files");
                }
            } else {
                failed++;
                logger.error("failed transter:" + failed + " files");
            }
        }
        // 
        threadPool.shutdown();
        // FTPCient
        ftpPool.close();
        // ****************
        logger.info("filter time:" + (endFileter - beginFilter) + " ms");
        if (!overwrite) {
            logger.info("skip time:" + (endSkip - beginSkip) + " ms");
        }
        logger.info("total file count:" + contents.length);
        logger.info("total transtered: " + transfered + ",total failed:" + failed + ",total skiped:" + skiped);

    } else {// 

        BufferedReader reader = null;
        FtpClientFactory facotry = new FtpClientFactory(config.getFTPClientConfig());
        FTPClient ftpClient = null;
        InputStream in = null;
        try {
            Path path = fileStatus.getPath();
            if (!path.getName().contains("log")) {

            }
            reader = new BufferedReader(new FileReader(new File(path.toUri().getPath())));
            String str = null;

            ftpClient = facotry.makeObject();

            while ((str = reader.readLine()) != null) {
                String[] feilds = str.split("&");
                Path filePath = null;
                if (feilds.length == 2 && feilds[1] != "") {
                    filePath = new Path(feilds[1]);
                    in = srcFS.open(filePath);
                    boolean result = ftpClient.storeFile(dstPath, in);
                    System.out.println(ftpClient.getReplyCode());
                    if (result) {
                        logger.info(filePath.toString());
                    } else {
                        logger_failed.info(filePath.toString());
                    }
                } else {
                    continue;
                }

            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            in.close();
            reader.close();
            facotry.destroyObject(ftpClient);
        }

    }
    long end = System.currentTimeMillis();
    logger.info("finished transfer,total time:" + (end - start) / 1000 + "s");
    return true;
}

From source file:com.claim.support.FtpUtil.java

public static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {

    boolean dirExists = true;

    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {
        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }//from  ww  w. j ava 2 s.co  m
            if (!dirExists) {
                if (!client.makeDirectory(dir)) {
                    throw new IOException("Unable to create remote directory '" + dir + "'.  error='"
                            + client.getReplyString() + "'");
                }
                if (!client.changeWorkingDirectory(dir)) {
                    throw new IOException("Unable to change into newly created remote directory '" + dir
                            + "'.  error='" + client.getReplyString() + "'");
                }
            }
        }
    }
}

From source file:com.geotrackin.gpslogger.senders.ftp.Ftp.java

private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {

    boolean dirExists = true;

    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {
        if (dir.length() > 0) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
                showServerReply(client);
            }//from   www  . j  a  va  2  s  . c o m
            if (!dirExists) {
                client.makeDirectory(dir);
                showServerReply(client);
                client.changeWorkingDirectory(dir);
                showServerReply(client);
            }
        }
    }
}

From source file:cycronix.ctlib.CTftp.java

/**
* utility to create an arbitrary directory hierarchy on the remote ftp server 
* @param client//w ww.ja  va  2 s. c  o  m
* @param dirTree  the directory tree only delimited with / chars.  No file name!
* @throws Exception
*/
private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {
    boolean dirExists = true;
    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {

        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }

            if (!dirExists) {
                try {
                    if (!client.makeDirectory(dir)) {
                        throw new IOException("Unable to create remote directory: " + dir + ", error="
                                + client.getReplyString());
                    }
                    if (!client.changeWorkingDirectory(dir)) {
                        throw new IOException("Unable to change into newly created remote directory: " + dir
                                + ", error=" + client.getReplyString());
                    }
                } catch (IOException ioe) {
                    //                  System.err.println("ftpCreateDir exception on dirTree: "+dirTree+", dir: "+dir+", error: "+ioe.getMessage());
                    throw ioe;
                }
            }
        }
    }
}

From source file:erigo.filepump.FilePumpWorker.java

/**
* utility to create an arbitrary directory hierarchy on the remote ftp server 
* @param client/*from w  ww .  j ava  2  s.c  o  m*/
* @param dirTree  the directory tree only delimited with / chars.  No file name!
* @throws Exception
*/
private static void ftpCreateDirectoryTree(FTPClient client, String dirTree) throws IOException {
    boolean dirExists = true;
    //tokenize the string and attempt to change into each directory level.  If you cannot, then start creating.
    String[] directories = dirTree.split("/");
    for (String dir : directories) {
        if (!dir.isEmpty()) {
            if (dirExists)
                dirExists = client.changeWorkingDirectory(dir);
            if (!dirExists) {
                if (!client.makeDirectory(dir))
                    throw new IOException("Unable to create remote directory '" + dir + "'.  error='"
                            + client.getReplyString() + "'");
                if (!client.changeWorkingDirectory(dir))
                    throw new IOException("Unable to change into newly created remote directory '" + dir
                            + "'. error='" + client.getReplyString() + "'");
            }
        }
    }
}

From source file:com.ephesoft.dcma.util.FTPUtil.java

/**
 * API to create an arbitrary directory hierarchy on the remote ftp server
 * //from w ww.j  av  a  2  s .c o  m
 * @param client {@link FTPClient} the ftp client instance.
 * @param dirTree the directory tree only delimited with / chars.
 * @throws IOException if a [problem occurs while making the directory or traversing down the directory structure.
 */
private static void createFtpDirectoryTree(final FTPClient client, final String dirTree) throws IOException {

    boolean dirExists = true;

    /*
     * tokenize the string and attempt to change into each directory level. If you cannot, then start creating. Needs to be updated
     * with a proper regex for being the system independent working. NOTE: Cannot use File.seperator here as for windows its value
     * is "\", which is a special character in terms of regex.
     */
    String[] directories = dirTree.split(DIRECTORY_SEPARATOR);
    for (String dir : directories) {
        if (!dir.isEmpty()) {
            if (dirExists) {
                dirExists = client.changeWorkingDirectory(dir);
            }
            if (!dirExists) {
                if (!client.makeDirectory(dir)) {
                    throw new IOException(EphesoftStringUtil.concatenate("Unable to create remote directory :",
                            dir, "\t error :", client.getReplyString()));
                }
                if (!client.changeWorkingDirectory(dir)) {
                    throw new IOException(EphesoftStringUtil.concatenate(
                            "Unable to change into newly created remote directory :", dir, "\t  error :",
                            client.getReplyString()));
                }
            }
        }
    }
}

From source file:com.mendhak.gpslogger.senders.ftp.Ftp.java

public static boolean Upload(String server, String username, String password, int port, boolean useFtps,
        String protocol, boolean implicit, InputStream inputStream, String fileName) {
    FTPClient client = null;

    try {/*from  ww w  .  j  a v a 2 s  .  c om*/
        if (useFtps) {
            client = new FTPSClient(protocol, implicit);

            KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmf.init(null, null);
            KeyManager km = kmf.getKeyManagers()[0];
            ((FTPSClient) client).setKeyManager(km);
        } else {
            client = new FTPClient();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        Utilities.LogDebug("Connecting to FTP");
        client.connect(server, port);

        Utilities.LogDebug("Logging in to FTP server");
        if (client.login(username, password)) {
            client.enterLocalPassiveMode();

            Utilities.LogDebug("Uploading file to FTP server");

            FTPFile[] existingDirectory = client.listFiles("GPSLogger");

            if (existingDirectory.length <= 0) {
                client.makeDirectory("GPSLogger");
            }

            client.changeWorkingDirectory("GPSLogger");
            boolean result = client.storeFile(fileName, inputStream);
            inputStream.close();
            if (result) {
                Utilities.LogDebug("Successfully FTPd file");
            } else {
                Utilities.LogDebug("Failed to FTP file");
            }

        } else {
            Utilities.LogDebug("Could not log in to FTP server");
            return false;
        }

    } catch (Exception e) {
        Utilities.LogError("Could not connect or upload to FTP server.", e);
    } finally {
        try {
            Utilities.LogDebug("Logging out of FTP server");
            client.logout();

            Utilities.LogDebug("Disconnecting from FTP server");
            client.disconnect();
        } catch (Exception e) {
            Utilities.LogError("Could not logout or disconnect", e);
        }
    }

    return true;
}

From source file:it.zero11.acme.example.FTPChallengeListener.java

private boolean createChallengeFiles(String token, String challengeBody) {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {//  w ww  .  ja va  2s .c  o  m
        ftp.connect(host);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
        }

        ftp.login(username, password);
        ftp.changeWorkingDirectory(webroot);
        ftp.makeDirectory(".well-known");
        ftp.changeWorkingDirectory(".well-known");
        ftp.makeDirectory("acme-challenge");
        ftp.changeWorkingDirectory("acme-challenge");
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE, FTPClient.BINARY_FILE_TYPE);
        ftp.setFileTransferMode(FTPClient.BINARY_FILE_TYPE);
        success = ftp.storeFile(token, new ByteArrayInputStream(challengeBody.getBytes()));
        if (!success)
            System.err.println("FTP error uploading file: " + ftp.getReplyCode() + ": " + ftp.getReplyString());
        ftp.logout();
    } catch (IOException e) {
        throw new AcmeException(e);
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
            }
        }
    }

    return success;
}

From source file:com.globalsight.smartbox.util.FtpHelper.java

public boolean ftpCreateDir(String p_path) {
    FTPClient ftpClient = getFtpClient();
    if (ftpClient != null && ftpClient.isConnected()) {
        try {//from w w w  .  j ava2  s.  com
            return ftpClient.makeDirectory(p_path);
        } catch (IOException e) {
            LogUtil.fail("Create Directory error by path:" + p_path, e);
        } finally {
            closeFtpClient(ftpClient);
        }
    }

    return false;
}