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

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

Introduction

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

Prototype

public int mkd(String pathname) throws IOException 

Source Link

Document

A convenience method to send the FTP MKD command to the server, receive the reply, and return the reply code.

Usage

From source file:com.clican.pluto.cms.core.service.impl.IssueServiceImpl.java

private boolean issue(ISite site, ITemplate template, IDataModel dataModel, String relativePath,
        Map<ISite, FTPClient> ftpMap) {
    Template t = null;/*w ww.ja va2  s  . com*/
    Writer w = null;
    VelocityContext velocityContext = new VelocityContext();
    velocityContext.put("this", dataModel);
    FTPClient client = null;
    try {
        SimpleNode node = RuntimeSingleton.getRuntimeServices().parse(template.getContent(),
                template.getName());
        t = new Template();
        t.setName(template.getName());
        t.setRuntimeServices(RuntimeSingleton.getRuntimeServices());
        t.setData(node);
        t.initDocument();
        OutputStream os = null;
        if (site.getUrl().startsWith("ftp://")) {
            client = null;
            if (ftpMap != null) {
                client = ftpMap.get(site);
            }
            if (client == null) {
                client = siteService.getFTPClient(site);
            }
            if (client == null) {
                log.error("This site[" + site.getName() + "] is unavailable");
                return false;
            }
            if (!relativePath.endsWith("/")) {
                relativePath = relativePath + "/";
            }
            client.mkd(relativePath);
            os = client.appendFileStream(relativePath + dataModel.getName() + "." + template.getSuffix());
        } else if (site.getUrl().startsWith("file://")) {
            String filePath = site.getUrl().substring(7);
            File file = new File(filePath + relativePath);
            if (file.exists()) {
                file.mkdirs();
            }
            os = new FileOutputStream(new File(file, dataModel.getName() + "." + template.getSuffix()));
        } else {
            throw new UnknownHostException(site.getUrl());
        }
        w = new OutputStreamWriter(os, "utf-8");
        t.merge(velocityContext, w);
        w.flush();
        return true;
    } catch (Exception e) {
        log.error("", e);
        return false;
    } finally {
        try {
            if (w != null) {
                w.close();
            }
        } catch (Exception e) {
            log.error("", e);
        }
        try {
            if (ftpMap == null && client != null) {
                client.logout();
                client.disconnect();
            }
        } catch (Exception e) {
            log.error("", e);
        }
    }
}

From source file:org.covito.kit.file.support.FtpFileServiceImpl.java

/** 
 * ?/* w w  w  . ja va2 s. co m*/
 * <p>??</p>
 *
 * @author  covito
 * @param ftp
 * @param path
 * @param mkdir true?
 * @return
 */
protected boolean dealDocPath(FTPClient ftp, String path, boolean mkdir) {
    String doc = path.substring(0, path.indexOf("/"));
    try {
        boolean success = ftp.changeWorkingDirectory(rootWorkingDirectory + "/" + doc);
        if (!success) {
            if (mkdir) {
                ftp.mkd(rootWorkingDirectory + "/" + doc);
                return ftp.changeWorkingDirectory(rootWorkingDirectory + "/" + doc);
            }
            return false;
        }
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:org.kuali.ole.module.purap.transmission.service.impl.TransmissionServiceImpl.java

/**
 * This method is to perform file upload
 *
 * @param ftpHostname//from w ww .j  a va 2 s. co m
 * @param ftpUsername
 * @param ftpPassword
 * @param file
 * @param fileName
 */
@Override
public void doFTPUpload(String ftpHostname, String ftpUsername, String ftpPassword, String file,
        String fileName) {
    LOG.trace("************************************doFTPUpload() started************************************");
    FTPClient ftpClient = new FTPClient();
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        ftpClient.connect(ftpHostname);
        ftpClient.login(ftpUsername, ftpPassword);
        ftpClient.enterLocalPassiveMode();
        int reply = ftpClient.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            LOG.debug("Connected to FTP server.");
        } else {
            LOG.debug("FTP server refused connection.");
        }

        // upload
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        String fileLocation = getFileLocation();
        if (LOG.isDebugEnabled()) {
            LOG.debug("File Location in FTP Server================>" + fileLocation);
            LOG.debug("File source=================================>" + file);
            LOG.debug("FileName====================================>" + fileName);
        }
        ftpClient.mkd(fileLocation);
        ftpClient.cwd(fileLocation);
        inputStream = new FileInputStream(file);
        ftpClient.storeFile(fileName, inputStream);

        ftpClient.logout();
        inputStream.close();
    } catch (Exception e) {
        LOG.error("Exception performing SFTP upload of " + file + " to " + ftpHostname, e);
        throw new RuntimeException(e);
    }
    LOG.trace(
            "************************************doFTPUpload() completed************************************");
}

From source file:org.limy.common.util.FtpUtils.java

/**
 * ???????//from w  w w  .  j a  va 2 s. c om
 * @param client FTP
 * @param remotePath ??
 * @throws IOException I/O
 */
private static void mkdirs(FTPClient client, String remotePath) throws IOException {
    if (client.cwd(remotePath) == FTPReply.FILE_UNAVAILABLE) {
        LOG.debug("Mkdir(ftp) : " + remotePath);

        StringTokenizer tokenizer = new StringTokenizer(remotePath, "/");
        StringBuilder subDir = new StringBuilder();
        while (tokenizer.hasMoreTokens()) {
            String token = tokenizer.nextToken();
            subDir.append(token);
            client.mkd(subDir.toString());
            subDir.append("/");
        }
    }
}

From source file:org.limy.common.util.FtpUtils.java

/**
 * ???????/*from  www .  j  a v  a 2s . co m*/
 * @param client FTP
 * @param absolutePath 
 * @throws IOException I/O
 */
private static void mkdirsAbsolute(FTPClient client, String absolutePath) throws IOException {
    String baseDir = absolutePath;
    while (client.cwd(baseDir) == FTPReply.FILE_UNAVAILABLE) {
        if (baseDir.lastIndexOf('/') < 0) {
            break;
        }
        baseDir = baseDir.substring(0, baseDir.lastIndexOf('/'));
    }
    if (baseDir.length() == absolutePath.length()) {
        return;
    }
    int pos = absolutePath.indexOf('/', baseDir.length() + 1);
    client.mkd(absolutePath.substring(0, pos));
    mkdirsAbsolute(client, absolutePath);
}

From source file:org.soitoolkit.commons.mule.ftp.FtpUtil.java

/**
 * Create a directory and all missing parent-directories.
 * /*  w w  w  . j ava 2  s. co m*/
 * @param ftpClient
 * @param path
 * @throws IOException
 */
static public void recursiveCreateDirectory(FTPClient ftpClient, String path) throws IOException {

    logger.info("Create Directory: {}", path);
    int createDirectoryStatus = ftpClient.mkd(path); // makeDirectory...
    logger.debug("Create Directory Status: {}", createDirectoryStatus);

    if (createDirectoryStatus == FTP_FILE_NOT_FOUND) {
        int sepIdx = path.lastIndexOf('/');
        if (sepIdx > -1) {
            String parentPath = path.substring(0, sepIdx);
            recursiveCreateDirectory(ftpClient, parentPath);

            logger.debug("2'nd CreateD irectory: {}", path);
            createDirectoryStatus = ftpClient.mkd(path); // makeDirectory...
            logger.debug("2'nd Create Directory Status: {}", createDirectoryStatus);
        }
    }
}