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

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

Introduction

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

Prototype

int ASCII_FILE_TYPE

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

Click Source Link

Document

A constant used to indicate the file(s) being transfered should be treated as ASCII.

Usage

From source file:s32a.FTPTest.java

public FTPTest(boolean login) {

    FTPClient client = new FTPClient();
    FileInputStream fis = null;/*from   w  w  w  .  j  a v  a 2  s  .c om*/
    FileOutputStream fos = null;

    try {
        client.connect("s32a.Airhockey.org");
        client.login("testey", "test");
        client.enterLocalPassiveMode();
        client.setFileType(FTP.ASCII_FILE_TYPE);

        client.makeDirectory("/testey");

        //        String filename = "testey.txt";
        //        File file = new File(filename);
        //        file.createNewFile();
    } catch (IOException ex) {
        Logger.getLogger(FTPTest.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:s32a.FTPTest.java

public FTPTest() {
    FTPClient client = new FTPSClient(false);
    FileInputStream fis = null;/*from  w  w w .j  a v  a 2 s  .c  om*/
    FileOutputStream fos = null;

    try {
        System.out.println("connecting");
        client.connect("athena.fhict.nl");
        boolean login = client.login("i293443", "ifvr2edfh101");
        System.out.println("login: " + login);
        client.enterLocalPassiveMode();
        System.out.println("connected: " + client.isConnected() + ", available: " + client.isAvailable());

        client.setFileType(FTP.ASCII_FILE_TYPE);
        //
        // Create an InputStream of the file to be uploaded
        //
        String filename = ".gitattributes";
        File file = new File(filename);
        file.createNewFile();
        System.out.println(file.length());
        fis = new FileInputStream(file.getAbsolutePath());
        client.makeDirectory("/Airhockey/Codebase/test");
        client.makeDirectory("\\Airhockey\\Codebase\\testey");

        //
        // Store file to server
        //
        String desiredName = "s32a\\Server\\.gitattributes";
        System.out.println("storefile: " + file.getAbsolutePath() + " - "
                + client.storeFile("/Airhockey/" + desiredName, fis));
        System.out.println("file stored");

        //            File output = new File("colors.json");
        //            fos = new FileOutputStream(output.getAbsolutePath());
        //            client.retrieveFile("/colors.json", fos);
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception ex) {
        System.out.println("exception caught: " + ex.getMessage());
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            if (fos != null) {
                fos.close();
            }
            client.disconnect();
            System.exit(0);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

From source file:smilehouse.opensyncro.defaultcomponents.ftp.FTPDestination.java

public void take(String data, DestinationInfo info, MessageLogger logger)
        throws FailTransferException, AbortTransferException {

    FTPClient ftp = new FTPClient();

    try {//from  w ww.  j a  va 2  s.  co  m
        // -----------------
        // Try to connect...
        // -----------------
        String host = this.data.getAttribute(HOST_ATTR);
        int port = -1;
        String portStr = this.data.getAttribute(PORT_ATTR);
        if (portStr != null && portStr.length() > 0) {
            try {
                port = Integer.parseInt(portStr);
            } catch (NumberFormatException nfe) {
                logger.logMessage("Invalid value '" + portStr + "' for port.", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }
        }
        int reply;

        try {
            if (port != -1) {
                ftp.connect(host, port);
            } else {
                ftp.connect(host);
            }
        } catch (SocketException e) {
            logger.logMessage("SocketException while connecting to host " + host + ", aborting", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        } catch (IOException e) {
            logger.logMessage("IOException while connecting to host " + host + ", aborting", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                /**
                 * ftp.disconnect() is called only as additional clean-up here, so we choose to
                 * ignore possible exceptions
                 */
            }
            logger.logMessage("Couldn't connect to the FTP server: " + ftp.getReplyString(), this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // -----------
        // Then log in
        // -----------

        try {
            if (!ftp.login(this.data.getAttribute(USER_ATTR), this.data.getAttribute(PASSWORD_ATTR))) {
                logger.logMessage("Could not log in, check your username and password settings.", this,
                        MessageLogger.ERROR);
                ftp.logout();

                PipeComponentUtils.failTransfer();
            }
        } catch (IOException e) {
            logger.logMessage("IOException while logging in to FTP server", this, MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // Use passive mode
        ftp.enterLocalPassiveMode();

        // -----------------
        // ASCII or binary ?
        // -----------------
        boolean fileTypeSetOk = false;

        try {
            switch (getFileType()) {
            case FILE_TYPE_ASCII:
                fileTypeSetOk = ftp.setFileType(FTP.ASCII_FILE_TYPE);
                break;
            case FILE_TYPE_BINARY:
                fileTypeSetOk = ftp.setFileType(FTP.BINARY_FILE_TYPE);
            }
            if (!fileTypeSetOk) {
                logger.logMessage("Could not set file type: " + ftp.getReplyString(), this,
                        MessageLogger.WARNING);
            }
        } catch (IOException e) {
            logger.logMessage("IOException while setting file transfer type parameter", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // -------------
        // Send the data
        // -------------
        String fileName = getFileName();
        logger.logMessage("Storing file: " + fileName, this, MessageLogger.DEBUG);
        String charSet = this.data.getAttribute(CHARSET_ATTR);
        if (charSet == null || charSet.length() == 0)
            charSet = DEFAULT_CHARSET;
        InputStream dataStream = null;
        try {
            dataStream = new ByteArrayInputStream(data.getBytes(charSet));
        } catch (UnsupportedEncodingException e1) {

            logger.logMessage(charSet + " charset not supported", this, MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }
        try {
            if (!ftp.storeFile(fileName, dataStream)) {
                logger.logMessage("Could not store file '" + fileName + "': " + ftp.getReplyString(), this,
                        MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }
        } catch (IOException e) {
            logger.logMessage("IOException while uploading the file to FTP server", this, MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }
}

From source file:smilehouse.opensyncro.defaultcomponents.ftp.FTPSource.java

public String[] give(SourceInfo info, MessageLogger logger)
        throws FailTransferException, AbortTransferException {

    // This component does not support iteration, so we output all our data
    // once (and only once)
    if (this.allDataOutput == true)
        return null;
    else//  www .  j  a  v a  2s .c  o  m
        this.allDataOutput = true;

    String[] dataArray = new String[1];
    FTPClient ftp = new FTPClient();

    try {
        // -----------------
        // Try to connect...
        // -----------------
        String host = this.data.getAttribute(HOST_ATTR);
        int port = -1;
        String portStr = this.data.getAttribute(PORT_ATTR);
        if (portStr != null && portStr.length() > 0) {
            try {
                port = Integer.parseInt(portStr);
            } catch (NumberFormatException nfe) {
                logger.logMessage("Invalid value '" + portStr + "' for port.", this, MessageLogger.ERROR);
                PipeComponentUtils.failTransfer();
            }
        }
        int reply;

        try {

            if (port != -1) {
                ftp.connect(host, port);
            } else {
                ftp.connect(host);
            }

        } catch (SocketException e) {
            logger.logMessage("SocketException while connecting to host " + host + ", aborting", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        } catch (IOException e) {
            logger.logMessage("IOException while connecting to host " + host + ", aborting", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // After connection attempt, you should check the reply code to verify
        // success.
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
            try {
                ftp.disconnect();
            } catch (IOException e) {
                /**
                 * ftp.disconnect() is called only as additional clean-up here, so we choose to
                 * ignore possible exceptions
                 */
            }
            logger.logMessage("Couldn't connect to the FTP server: " + ftp.getReplyString(), this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // -----------
        // Then log in
        // -----------
        try {
            if (!ftp.login(this.data.getAttribute(USER_ATTR), this.data.getAttribute(PASSWORD_ATTR))) {
                logger.logMessage("Could not log in, check your username and password settings.", this,
                        MessageLogger.ERROR);
                ftp.logout();

                PipeComponentUtils.failTransfer();
            }

        } catch (IOException e) {
            logger.logMessage("IOException while logging in to FTP server", this, MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // Use passive mode
        ftp.enterLocalPassiveMode();

        // -----------------
        // ASCII or binary ?
        // -----------------
        boolean fileTypeSetOk = false;
        try {
            switch (getFileType()) {
            case FILE_TYPE_ASCII:
                fileTypeSetOk = ftp.setFileType(FTP.ASCII_FILE_TYPE);
                break;
            case FILE_TYPE_BINARY:
                fileTypeSetOk = ftp.setFileType(FTP.BINARY_FILE_TYPE);
            }
            if (!fileTypeSetOk) {
                logger.logMessage("Could not set file type: " + ftp.getReplyString(), this,
                        MessageLogger.WARNING);
            }
        } catch (IOException e) {
            logger.logMessage("IOException while setting file transfer type parameter", this,
                    MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }

        // -----------------
        // Retrieve the data
        // -----------------
        String fileName = getFileName(logger);
        logger.logMessage("Retrieving file: " + fileName, this, MessageLogger.DEBUG);
        OutputStream dataStream = new ByteArrayOutputStream();
        try {
            if (!ftp.retrieveFile(fileName, dataStream)) {
                logger.logMessage("Could not retrieve file '" + fileName + "': " + ftp.getReplyString(), this,
                        MessageLogger.WARNING);
                PipeComponentUtils.abortTransfer();
            }
            String charSet = this.data.getAttribute(CHARSET_ATTR);
            if (charSet == null || charSet.length() == 0)
                charSet = DEFAULT_CHARSET;
            dataArray[0] = ((ByteArrayOutputStream) dataStream).toString(charSet);
        } catch (IOException e) {
            logger.logMessage("IOException while downloading file from FTP server", this, MessageLogger.ERROR);
            PipeComponentUtils.failTransfer();
        }
    } finally {
        if (ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException f) {
                // do nothing
            }
        }
    }
    return dataArray;
}

From source file:uk.ac.manchester.cs.datadesc.validator.utils.UrlReader.java

private InputStream getInputStream(FTPClient ftp) throws VoidValidatorException {
    try {/*from  w  w w . ja  va  2  s.c o m*/
        // in theory this should not be necessary as servers should default to ASCII
        // but they don't all do so - see NET-500
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
        ftp.enterLocalPassiveMode();
        ftp.setUseEPSVwithIPv4(useEpsvWithIPv4);
        String path = uri.getPath();
        return ftp.retrieveFileStream(path);
    } catch (FTPConnectionClosedException ex) {
        disconnect(ftp);
        throw new VoidValidatorException("Server closed connection.", ex);
    } catch (IOException ex) {
        disconnect(ftp);
        throw new VoidValidatorException("IOEXception with Server ", ex);
    }
}

From source file:uk.co.marcoratto.net.ClientFTP.java

/** Use ASCII mode for file transfers */
public boolean setFileTypeASCII() throws ClientFTPException {
    boolean reply = false;
    try {// w  w w. ja va  2  s .c om
        reply = ftp.setFileType(FTP.ASCII_FILE_TYPE);
        this.printReplyStrings();
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
        throw new ClientFTPException(e);
    }
    return reply;
}

From source file:uk.trainwatch.io.ftp.DefaultFTPClient.java

@Override
public void login() throws IOException {
    if (loggedIn) {
        return;/* w w w .  j  ava 2  s .  co m*/
    }

    if (!login.test(this)) {
        throw new ConnectException("Login failed");
    }

    String systemType = ftp.getSystemType();
    debug(() -> "Remote system is " + systemType);

    if (binaryTransfer) {
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
    } else {
        // in theory this should not be necessary as servers should default to ASCII but they don't all do so - see NET-500
        ftp.setFileType(FTP.ASCII_FILE_TYPE);
    }

    // Use passive mode as default because most of us are
    // behind firewalls these days.
    if (localActive) {
        ftp.enterLocalActiveMode();
    } else {
        ftp.enterLocalPassiveMode();
    }

    ftp.setUseEPSVwithIPv4(useEpsvWithIPv4);

    loggedIn = true;
}

From source file:wjhk.jupload2.upload.FileUploadThreadFTP.java

/**
 * Will set the binary/ascii value based on the parameters to the applet.
 * This could be done by file extension too but it is not implemented.
 * /* www .  ja v  a  2s .co m*/
 * @param index The index of the file that we want to upload, in the array
 *            of files to upload.
 * @throws IOException if an error occurs while setting mode data
 */
private void setTransferType(@SuppressWarnings("unused") int index) throws IOException {
    // FileData file
    try {
        String binVal = this.uploadPolicy.getString("binary");

        // read the value given from the user
        if (Boolean.getBoolean(binVal))
            this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
        else
            this.ftp.setFileType(FTP.ASCII_FILE_TYPE);

    } catch (MissingResourceException e) {
        // should set based on extension (not implemented)
        this.ftp.setFileType(FTP.BINARY_FILE_TYPE);
    }

    // now do the same for the passive/active parameter
    try {
        String pasVal = this.uploadPolicy.getString("passive");

        if (Boolean.getBoolean(pasVal)) {
            this.ftp.enterRemotePassiveMode();
            this.ftp.enterLocalPassiveMode();
        } else {
            this.ftp.enterLocalActiveMode();
            this.ftp.enterRemoteActiveMode(InetAddress.getByName(this.host), Integer.parseInt(this.port));
        }
    } catch (MissingResourceException e) {
        this.ftp.enterRemotePassiveMode();
        this.ftp.enterLocalPassiveMode();
    }
}