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

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

Introduction

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

Prototype

int BINARY_FILE_TYPE

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

Click Source Link

Document

A constant used to indicate the file(s) being transfered should be treated as a binary image, i.e., no translations should be performed.

Usage

From source file:uk.nhs.cfh.dsp.srth.distribution.FileDownloader.java

public void getFileFromTRUDArchive(String trudArchiveName, String fileName, String outputURL) {

    logger.info("Now getting file... ");
    if (ftpClient.isConnected()) {
        logger.info("ftp client is connected... ");
        // now get file specified by inputURL
        try {// ww  w  .j  a  va 2s .  c  o  m
            ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
            logger.info("Sending NOOP command... ");
            // send NOOP command to see if connection is still active
            if (ftpClient.sendNoOp()) {
                logger.info("ftpClient.getReplyString() = " + ftpClient.getReplyString());
                FTPFile[] files = ftpClient.listFiles(getPackPath());
                for (FTPFile file : files) {
                    logger.info("file.getName() = " + file.getName());
                    logger.info("file.getSize() = " + file.getSize());
                    if (file.getName().equals(trudArchiveName) && file.getSize() > 0) {
                        InputStream is = ftpClient.retrieveFileStream(getPackPath() + trudArchiveName);

                        if (is != null) {
                            ZipArchiveInputStream zipArchiveInputStream = new ZipArchiveInputStream(is);
                            ZipArchiveEntry zipArchiveEntry = zipArchiveInputStream.getNextZipEntry();
                            while (zipArchiveEntry != null) {
                                String zippedArchiveEntryName = zipArchiveEntry.getName();
                                logger.info("zippedArchiveEntryName = " + zippedArchiveEntryName);
                                logger.info("fileName = " + fileName);
                                if (zippedArchiveEntryName.equals(fileName)) {
                                    logger.info("Extracting: " + zippedArchiveEntryName);
                                    File outputLocation = new File(outputURL);
                                    boolean canWrite = outputLocation.exists();
                                    logger.info("canWrite = " + canWrite);
                                    if (!canWrite) {
                                        canWrite = outputLocation.mkdirs();
                                        logger.info("canWrite after mkdirs = " + canWrite);
                                    }

                                    if (canWrite && outputLocation.canWrite()) {
                                        logger.info("outputLocation.getPath() = " + outputLocation.getPath());
                                        File destinationFile = new File(outputLocation.getAbsolutePath(),
                                                zippedArchiveEntryName);
                                        OutputStream out = new FileOutputStream(destinationFile);
                                        IOUtils.copy(zipArchiveInputStream, out);
                                        out.close();

                                        if (zippedArchiveEntryName.indexOf(".zip") > -1) {
                                            // unpackZip file
                                            extractZipFileContents(destinationFile);
                                        }
                                    }
                                } else {
                                    logger.info("Resetting zipArchiveEntry");
                                    zipArchiveEntry = zipArchiveInputStream.getNextZipEntry();
                                    if (zipArchiveEntry != null) {
                                        logger.info("zipArchiveEntry.getName() = " + zipArchiveEntry.getName());
                                    }
                                }

                            }

                            zipArchiveInputStream.close();
                            is.close();
                        }
                        break;
                    }
                }

                // complete pending commands; needed after opening and closing streams
                ftpClient.completePendingCommand();
            } else {
                logger.warning("FTP connection might have timed out.");
                ftpClient.disconnect();
            }
        } catch (IOException e) {
            logger.warning("FTP connection might have timed out. " + ERR_MESSAGE + e.fillInStackTrace());
        }
    } else {
        logger.warning("No connection to TRUD is available. Ensure FTP connection is open");
    }
}

From source file:uk.sipperfly.utils.FTPUtil.java

public static void reconnect() throws SocketException, IOException {
    FTPClient ftpClient = new FTPClient();
    ftpClient.setControlEncoding("UTF-8");
    ftpClient.connect(host, port);/* w ww . j  a  v  a 2  s  .c om*/
    ftpClient.login(username, password);
    if (mode.equalsIgnoreCase("passive")) {
        ftpClient.enterLocalPassiveMode();
    } else if (mode.equalsIgnoreCase("active")) {
        ftpClient.enterLocalActiveMode();
    }
    int reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        Logger.getLogger(GACOM).log(Level.INFO, "FTP Login: ".concat(ftpClient.getReplyString()));
        ftpClient.disconnect();
    }
    ftpClient.setKeepAlive(true);
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE, FTP.BINARY_FILE_TYPE);
    ftpClient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
    ftpClient.setControlKeepAliveTimeout(300);
    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLKSIZE=27000");
    //      ftpClient.sendSiteCommand("CY");
    //      ftpClient.sendSiteCommand("PRI= 50");
    //      ftpClient.sendSiteCommand("SEC=25");

    //      ftpClient.sendSiteCommand("RECFM=FB");
    //      ftpClient.sendSiteCommand("LRECL=2000");
    //      ftpClient.sendSiteCommand("BLOCKSIZE=27000");
    //      ftpClient.sendSiteCommand("SPACE=(CYL,(30,300),RLSE)"); 
    //      ftpClient.sendSiteCommand("TR");
    //      ftpClient.sendSiteCommand("PRI=450");
    //      ftpClient.sendSiteCommand("SEC=4500");

    Logger.getLogger(GACOM).log(Level.INFO, "Reconnected FTP");
    System.out.println("reconnected");
}

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

@Override
public void login() throws IOException {
    if (loggedIn) {
        return;// ww w.  j a  v a  2s . c o 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:vn.edu.vttu.data.UploadFile.java

public UploadFile(String host, String user, String pwd) throws Exception {
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
    int reply;//from ww  w  . j  a  v a2s.c  o  m
    ftp.connect(host);
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        throw new Exception("Exception in connecting to FTP Server");
    }
    ftp.login(user, pwd);
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    ftp.enterLocalPassiveMode();

}

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.
 * //from   w w  w.  ja  va 2  s .  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();
    }
}