Example usage for org.apache.commons.net.ftp FTP STREAM_TRANSFER_MODE

List of usage examples for org.apache.commons.net.ftp FTP STREAM_TRANSFER_MODE

Introduction

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

Prototype

int STREAM_TRANSFER_MODE

To view the source code for org.apache.commons.net.ftp FTP STREAM_TRANSFER_MODE.

Click Source Link

Document

A constant used to indicate a file is to be transfered as a stream of bytes.

Usage

From source file:net.shopxx.plugin.ftpStorage.FtpStoragePlugin.java

@Override
public void upload(String path, File file, String contentType) {
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {//from  w w w  .j  av  a  2 s . c  om
            inputStream = new BufferedInputStream(new FileInputStream(file));
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (SocketException e) {
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage(), e);
        } finally {
            IOUtils.closeQuietly(inputStream);
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.disconnect();
                }
            } catch (IOException e) {
            }
        }
    }
}

From source file:com.dp2345.plugin.ftp.FtpPlugin.java

@Override
public void upload(String path, File file, String contentType) {
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {/* ww w .ja  v  a 2s .  com*/
            inputStream = new FileInputStream(file);
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java

@Override
public void upload(String path, File file, String contentType) {
    Map<String, String> ftpInfo = getFtpInfo(contentType);
    if (!"".equals(ftpInfo.get("host")) && !"".equals(ftpInfo.get("username"))
            && !"".equals(ftpInfo.get("password"))) {
        FTPClient ftpClient = new FTPClient();
        InputStream inputStream = null;
        try {// w  ww  . j  a  va2 s.c  o  m
            inputStream = new FileInputStream(file);
            ftpClient.connect(ftpInfo.get("host"), 21);
            ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password"));
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            path = ftpInfo.get("path") + path;
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                String directory = StringUtils.substringBeforeLast(path, "/");
                String filename = StringUtils.substringAfterLast(path, "/");
                if (!ftpClient.changeWorkingDirectory(directory)) {
                    String[] paths = StringUtils.split(directory, "/");
                    String p = "/";
                    ftpClient.changeWorkingDirectory(p);
                    for (String s : paths) {
                        p += s + "/";
                        if (!ftpClient.changeWorkingDirectory(p)) {
                            ftpClient.makeDirectory(s);
                            ftpClient.changeWorkingDirectory(p);
                        }
                    }
                }
                ftpClient.storeFile(filename, inputStream);
                ftpClient.logout();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
}

From source file:com.hibo.bas.fileplugin.file.FtpPlugin.java

@Override
public List<FileInfo> browser(String path) {
    List<FileInfo> fileInfos = new ArrayList<FileInfo>();
    // PluginConfig pluginConfig = getPluginConfig();
    // if (pluginConfig != null) {
    Map<String, String> ftpInfo = getFtpInfo("all");
    String urlPrefix = DataConfig.getConfig("IMGFTPROOT");
    FTPClient ftpClient = new FTPClient();
    try {//from  w w  w.j a v  a2s  . co  m
        ftpClient.connect(ftpInfo.get("host"), 21);
        ftpClient.login(ftpInfo.get("username"), ftpInfo.get("password"));
        ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();
        if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode()) && ftpClient.changeWorkingDirectory(path)) {
            for (FTPFile ftpFile : ftpClient.listFiles()) {
                FileInfo fileInfo = new FileInfo();
                fileInfo.setName(ftpFile.getName());
                fileInfo.setUrl(urlPrefix + path + ftpFile.getName());
                fileInfo.setIsDirectory(ftpFile.isDirectory());
                fileInfo.setSize(ftpFile.getSize());
                fileInfo.setLastModified(ftpFile.getTimestamp().getTime());
                fileInfos.add(fileInfo);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftpClient.isConnected()) {
            try {
                ftpClient.disconnect();
            } catch (IOException e) {
            }
        }
    }
    // }
    return fileInfos;
}

From source file:com.dp2345.plugin.ftp.FtpPlugin.java

@Override
public List<FileInfo> browser(String path) {
    List<FileInfo> fileInfos = new ArrayList<FileInfo>();
    PluginConfig pluginConfig = getPluginConfig();
    if (pluginConfig != null) {
        String host = pluginConfig.getAttribute("host");
        Integer port = Integer.valueOf(pluginConfig.getAttribute("port"));
        String username = pluginConfig.getAttribute("username");
        String password = pluginConfig.getAttribute("password");
        String urlPrefix = pluginConfig.getAttribute("urlPrefix");
        FTPClient ftpClient = new FTPClient();
        try {//w w  w  .  j a v a 2 s  .c o m
            ftpClient.connect(host, port);
            ftpClient.login(username, password);
            ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();
            if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())
                    && ftpClient.changeWorkingDirectory(path)) {
                for (FTPFile ftpFile : ftpClient.listFiles()) {
                    FileInfo fileInfo = new FileInfo();
                    fileInfo.setName(ftpFile.getName());
                    fileInfo.setUrl(urlPrefix + path + ftpFile.getName());
                    fileInfo.setIsDirectory(ftpFile.isDirectory());
                    fileInfo.setSize(ftpFile.getSize());
                    fileInfo.setLastModified(ftpFile.getTimestamp().getTime());
                    fileInfos.add(fileInfo);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ftpClient.isConnected()) {
                try {
                    ftpClient.disconnect();
                } catch (IOException e) {
                }
            }
        }
    }
    return fileInfos;
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * ?//from   w ww . j a va  2  s .  co  m
 * 
 * @param remoteFile
 *            FTP"/"
 * @param input
 *            ?
 */
public static boolean upload(String remoteFile, byte[] bytes) {
    if (!remoteFile.startsWith("/")) {
        throw new RuntimeException(" path must start with '/'");
    }
    ByteArrayInputStream bais = null;
    BufferedInputStream bis = null;
    try {

        mkdirs(remoteFile.substring(0, remoteFile.lastIndexOf("/")));

        if (remoteFile.startsWith("/") && remoteFile.indexOf("/") > 0) {
            changeToDirectory(remoteFile);
            remoteFile = remoteFile.substring(remoteFile.lastIndexOf("/") + 1, remoteFile.length());
        }

        getFtpClient().setFileType(FTP.BINARY_FILE_TYPE);
        getFtpClient().enterLocalPassiveMode();
        getFtpClient().setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        bais = new ByteArrayInputStream(bytes);
        bis = new BufferedInputStream(bais);
        boolean flag = getFtpClient().storeFile(remoteFile, bis);
        if (flag) {
            logger.info("upload success");
        } else {
            logger.info("upload failure");
        }
        return flag;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("upload error", ex);
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(bis);
        IOUtils.closeStream(bais);
    }
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * ?/*from www .  j av  a  2 s.co  m*/
 * 
 * @param remoteFile
 *            FTP"/"
 * @param input
 *            ?
 */
public static boolean upload(String remoteFile, InputStream input) {
    if (!remoteFile.startsWith("/")) {
        throw new RuntimeException(" path must start with '/'");
    }
    try {

        mkdirs(remoteFile.substring(0, remoteFile.lastIndexOf("/")));

        if (remoteFile.startsWith("/") && remoteFile.indexOf("/") > 0) {
            changeToDirectory(remoteFile);
            remoteFile = remoteFile.substring(remoteFile.lastIndexOf("/") + 1, remoteFile.length());
        }
        getFtpClient().setFileType(FTP.BINARY_FILE_TYPE);
        getFtpClient().enterLocalPassiveMode();
        getFtpClient().setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        boolean flag = getFtpClient().storeFile(remoteFile, input);
        if (flag) {
            logger.info("upload success");
        } else {
            logger.info("upload failure");
        }
        return flag;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("upload error", ex);
        throw new RuntimeException(ex);
    }
}

From source file:com.glaf.core.util.FtpUtils.java

/**
 * ?/*from w  w w  . ja va 2 s.co m*/
 * 
 * @param remoteFile
 *            FTP"/"
 * @param localFile
 *            
 */
public static boolean upload(String remoteFile, String localFile) {
    if (!remoteFile.startsWith("/")) {
        throw new RuntimeException(" path must start with '/'");
    }
    InputStream input = null;
    try {

        mkdirs(remoteFile.substring(0, remoteFile.lastIndexOf("/")));

        if (remoteFile.startsWith("/") && remoteFile.indexOf("/") > 0) {
            changeToDirectory(remoteFile);
            remoteFile = remoteFile.substring(remoteFile.lastIndexOf("/") + 1, remoteFile.length());
        }

        getFtpClient().setFileType(FTP.BINARY_FILE_TYPE);
        getFtpClient().enterLocalPassiveMode();
        getFtpClient().setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        File file = new File(localFile);
        input = new FileInputStream(file);
        boolean flag = getFtpClient().storeFile(remoteFile, input);
        if (flag) {
            logger.info("upload success");
        } else {
            logger.info("upload failure");
        }
        return flag;
    } catch (IOException ex) {
        ex.printStackTrace();
        logger.error("upload error", ex);
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(input);
    }
}

From source file:org.mockftpserver.stub.StubFtpServerIntegrationTest.java

/**
 * Test Mode (MODE)/*w ww  .java  2 s .c  o  m*/
*/
public void testMode() throws Exception {
    ftpClientConnect();

    // MODE
    boolean success = ftpClient.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
    assertTrue("Unable to MODE", success);
    verifyReplyCode("setFileTransferMode", 200);
}

From source file:tufts.oki.remoteFiling.RemoteByteStore.java

/**
 *  Get the current length of thsi byte store.
 *
 *  @author Mark Norton//from  w ww.j  a  va  2 s.  c  om
 *
 *  @return The current length of this byte store.
 */
public long length() throws osid.filing.FilingException {
    long length = 0;
    try {
        FTPClient client = rc.getClient();
        client.setFileTransferMode(FTP.STREAM_TRANSFER_MODE);
        //  The file to open consists of the root base plus, path to current directory plus name.
        //String fn = rc.getRootBase() + ((RemoteCabinet)getParent()).separator() + getDisplayName();
        //String fn = rc.getRootBase() + "/" + getDisplayName();
        String fn = getFullName();
        //System.out.println("length - file name to open: " + fn);
        FTPFile[] replies = client.listFiles(__fileListParser, fn);
        System.out.println("File Name = " + fn + " replies =" + replies + "Client:" + client.getStatus());
        if (replies == null) {
            System.out.println(client.getReplyCode());
            throw new osid.filing.FilingException(
                    "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
        }
        //System.out.println(client.getReplyCode());
        length = replies[0].getSize();
    } catch (IOException e) {
        throw new osid.filing.FilingException(
                "RemoteByteStore.length: " + osid.filing.FilingException.IO_ERROR);
    }
    return length;
}