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

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

Introduction

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

Prototype

public boolean setFileType(int fileType) throws IOException 

Source Link

Document

Sets the file type to be transferred.

Usage

From source file:org.ramadda.repository.type.FtpTypeHandler.java

/**
 * _more_//from  ww w. j  a v a  2 s.c  om
 *
 * @param parentEntry _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
private FTPClient getFtpClient(Entry parentEntry) throws Exception {
    Object[] values = parentEntry.getValues();
    if (values == null) {
        return null;
    }
    String server = (String) values[COL_SERVER];
    String baseDir = (String) values[COL_BASEDIR];
    String user = (String) values[COL_USER];
    String password = (String) values[COL_PASSWORD];
    if (password != null) {
        password = getRepository().getPageHandler().processTemplate(password, false);
    } else {
        password = "";
    }
    FTPClient ftpClient = new FTPClient();
    try {
        ftpClient.connect(server);
        if (user != null) {
            ftpClient.login(user, password);
        }
        int reply = ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftpClient.disconnect();
            System.err.println("FTP server refused connection.");

            return null;
        }
        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
        ftpClient.enterLocalPassiveMode();

        return ftpClient;
    } catch (Exception exc) {
        System.err.println("Could not connect to ftp server:" + server + "\nError:" + exc);

        return null;
    }
}

From source file:org.ramadda.util.Utils.java

/**
 * _more_//from  w  w  w. j a  v  a 2s  .c om
 *
 * @param url _more_
 *
 * @return _more_
 *
 * @throws Exception _more_
 */
public static FTPClient makeFTPClient(URL url) throws Exception {
    FTPClient ftpClient = new FTPClient();
    ftpClient.connect(url.getHost());
    ftpClient.login("anonymous", "");
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftpClient.disconnect();
        System.err.println("FTP server refused connection.");

        return null;
    }
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();

    return ftpClient;
}

From source file:org.shept.util.FtpFileCopy.java

/**
 * @param//from ww  w  .j a  v  a2s .  c  o m
 * @return
 *
 * @param config
 * @param ftpC
 * @throws SocketException
 * @throws IOException
 */
private boolean configSetup(FtpConfig config, FTPClient ftpC) throws IOException {
    boolean rc;
    ftpC.connect(config.getHostAddress(), config.getPort());
    rc = ftpC.login(config.getUserName(), config.getPassword());
    if (!rc) {
        logger.error("Ftp could not log to remote server with " + config.getUserName());
        return false;
    }
    if (StringUtils.hasText(config.getServerPath())) {
        int cwdCode = ftpC.cwd(config.getServerPath());
        if (!FTPReply.isPositiveCompletion(cwdCode)) {
            logger.error("Ftp could not change working dir to " + config.getServerPath()
                    + " - Server returned Code " + cwdCode);
            return false;
        }
    }
    rc = ftpC.setFileType(config.getFileType());
    if (!rc) {
        logger.error("Ftp could not change FileType for transmission");
        return false;
    }
    return true;
}

From source file:org.spka.cursus.publish.website.ftp.DownloadDataFiles.java

public void from(FTPClient ftp) throws IOException {
    if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
        throw new IllegalStateException("Unable to set mode to binary");
    }/*from  w  w w . j a va2  s.  c  om*/

    for (String fileName : files.keySet()) {
        if (fileName.startsWith(Constants.RESULTS_DIR + "/__") && fileName.endsWith(".xml")) {
            if (files.get(fileName) == null) {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                if (!ftp.retrieveFile(fileName, buf)) {
                    throw new IllegalStateException("Unable to retrieve " + fileName);
                }
                buf.close();
                files.put(fileName, ByteSource.wrap(buf.toByteArray()));
            }
        }
    }
}

From source file:org.spka.cursus.publish.website.ftp.Uploader.java

public void to(FTPClient ftp, Map<String, ByteSource> newFiles) throws IOException {
    if (!ftp.setFileType(FTP.BINARY_FILE_TYPE)) {
        throw new IllegalStateException("Unable to set mode to binary");
    }/*from   www.j  a v a 2  s .  com*/

    for (Map.Entry<String, ByteSource> file : newFiles.entrySet()) {
        ByteSource other = null;

        if (existingFiles.containsKey(file.getKey())) {
            other = existingFiles.get(file.getKey());

            if (other == null) {
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                if (!ftp.retrieveFile(file.getKey(), buf)) {
                    throw new IllegalStateException("Unable to retrieve " + file.getKey());
                }
                buf.close();
                other = ByteSource.wrap(buf.toByteArray());
                existingFiles.put(file.getKey(), other);
            }
        }

        if (other == null || !file.getValue().contentEquals(other)) {
            InputStream in = file.getValue().openStream();
            if (!ftp.storeFile(file.getKey(), in)) {
                throw new IllegalStateException("Unable to store " + file.getKey());
            }
            in.close();

            existingFiles.put(file.getKey(), file.getValue());
        }
    }
}

From source file:org.spongepowered.repoindexer.FTPUtils.java

public static boolean uploadFTP(String user, String pass, String server, File local, String remoteLocation) {
    FTPClient ftpClient = new FTPClient();
    boolean done = false;
    try {/*from w w  w .  j  a  va2  s .com*/

        ftpClient.connect(server);
        ftpClient.login(user, pass);
        ftpClient.enterLocalPassiveMode();

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        InputStream inputStream = new FileInputStream(local);
        done = ftpClient.storeFile(remoteLocation, inputStream);
        inputStream.close();
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
        return false;
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return done;
    }
}

From source file:org.structr.files.ftp.FtpFilesTest.java

public void test00StoreFile() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "file1";
    final String name2 = "file2";

    try (final Tx tx = app.tx()) {

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);//from  w w  w.ja va2 s . c o m
        assertEquals(0, files.length);

        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.setAutodetectUTF8(true);

        // Store a file
        InputStream in = IOUtils.toInputStream("Test Content");
        ftp.storeFile(name1, in);

        in.close();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }

    String[] fileNames = null;

    try (final Tx tx = app.tx()) {

        fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(1, fileNames.length);
        assertEquals(name1, fileNames[0]);

        // Create second file in /
        createFTPFile(null, name2);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }

    try (final Tx tx = app.tx()) {

        fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(2, fileNames.length);
        assertEquals(name1, fileNames[0]);
        assertEquals(name2, fileNames[1]);

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        ex.printStackTrace();
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:org.structr.ftp.FtpFilesTest.java

public void test00StoreFile() {

    FTPClient ftp = setupFTPClient();
    final String name1 = "file1";
    final String name2 = "file2";

    try (final Tx tx = app.tx()) {

        FTPFile[] files = ftp.listFiles();

        assertNotNull(files);/*from   w w w. ja  v a2  s. com*/
        assertEquals(0, files.length);

        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.setAutodetectUTF8(true);

        // Store a file
        InputStream in = IOUtils.toInputStream("Test Content");
        ftp.storeFile(name1, in);

        in.close();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

    String[] fileNames = null;

    try (final Tx tx = app.tx()) {

        fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(1, fileNames.length);
        assertEquals(name1, fileNames[0]);

        // Create second file in /
        createFTPFile(null, name2);

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }

    try (final Tx tx = app.tx()) {

        fileNames = ftp.listNames();

        assertNotNull(fileNames);
        assertEquals(2, fileNames.length);
        assertEquals(name1, fileNames[0]);
        assertEquals(name2, fileNames[1]);

        ftp.disconnect();

        tx.success();

    } catch (IOException | FrameworkException ex) {
        logger.log(Level.WARNING, "", ex);
        fail("Unexpected exception: " + ex.getMessage());
    }
}

From source file:org.syncany.tests.connection.plugins.ftp.EmbeddedTestFtpServer.java

public static void mkdir(String path, String user) throws SocketException, IOException {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(3000);/*from   w  ww  .  j a v  a  2s .  c o m*/
    ftp.setDataTimeout(3000);
    ftp.setDefaultTimeout(3000);

    ftp.connect(HOST, PORT);
    ftp.login(user, PASSWORD1);
    ftp.enterLocalPassiveMode();
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Important !!!
    ftp.makeDirectory(path);

    ftp.disconnect();
    ftp = null;
}

From source file:org.syncany.tests.plugins.ftp.EmbeddedTestFtpServer.java

public static void createTestFile(String path, String user) throws SocketException, IOException {
    FTPClient ftp = new FTPClient();
    ftp.setConnectTimeout(3000);/*  w  w  w.j a  v a  2  s.c o m*/
    ftp.setDataTimeout(3000);
    ftp.setDefaultTimeout(3000);

    ftp.connect(HOST, PORT);
    ftp.login(user, PASSWORD1);
    ftp.enterLocalPassiveMode();
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // Important !!!
    ftp.storeFile(path, new ByteArrayInputStream(new byte[] { 0x01, 0x02 }));

    ftp.disconnect();
    ftp = null;
}