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

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

Introduction

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

Prototype

public boolean logout() throws IOException 

Source Link

Document

Logout of the FTP server by sending the QUIT command.

Usage

From source file:fr.acxio.tools.agia.ftp.FTPDownloadTasklet.java

@Override
public RepeatStatus execute(StepContribution sContribution, ChunkContext sChunkContext) throws Exception {
    FTPClient aClient = ftpClientFactory.getFtpClient();

    RegexFilenameFilter aFilter = new RegexFilenameFilter();
    aFilter.setRegex(regexFilename);/*from   w  ww . jav a2s  .  c  o  m*/
    try {
        URI aRemoteBaseURI = new URI(remoteBaseDir);
        URI aRemoteBasePath = new URI(aRemoteBaseURI.toASCIIString() + SEPARATOR);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("Listing : [{}] {} ({})", aClient.getRemoteAddress().toString(),
                    aRemoteBaseURI.toASCIIString(), regexFilename);
        }

        FTPFile[] aRemoteFiles = aClient.listFiles(aRemoteBaseURI.toASCIIString(), aFilter);

        if (LOGGER.isInfoEnabled()) {
            LOGGER.info("  {} file(s) found", aRemoteFiles.length);
        }

        for (FTPFile aRemoteFile : aRemoteFiles) {

            if (sContribution != null) {
                sContribution.incrementReadCount();
            }

            File aLocalFile = new File(localBaseDir, aRemoteFile.getName());
            URI aRemoteTFile = aRemoteBasePath.resolve(aRemoteFile.getName());

            FileOutputStream aOutputStream = new FileOutputStream(aLocalFile);
            try {

                if (LOGGER.isInfoEnabled()) {
                    LOGGER.info(" Downloading : {} => {}", aRemoteTFile.toASCIIString(),
                            aLocalFile.getAbsolutePath());
                }

                aClient.retrieveFile(aRemoteTFile.toASCIIString(), aOutputStream);
                if (removeRemote) {

                    if (LOGGER.isInfoEnabled()) {
                        LOGGER.info(" Deleting : {}", aRemoteTFile.toASCIIString());
                    }

                    aClient.deleteFile(aRemoteTFile.toASCIIString());
                }

                if (sContribution != null) {
                    sContribution.incrementWriteCount(1);
                }

            } finally {
                aOutputStream.close();
            }
        }
    } finally {
        aClient.logout();
        aClient.disconnect();
    }

    return RepeatStatus.FINISHED;
}

From source file:com.rvl.android.getnzb.LocalNZB.java

public void uploadLocalFileFTP(String filename) {
    UPLOADFILENAME = filename;/*from   www .j a v  a2 s  .  c  om*/

    UPLOADDIALOG = ProgressDialog.show(this, "Please wait...", "Uploading '" + filename + "' to FTP server.");

    SharedPreferences prefs = GetNZB.preferences;
    if (prefs.getString("FTPHostname", "") == "") {
        uploadDialogHandler.sendEmptyMessage(0);
        Toast.makeText(this, "Upload to FTP server not possible. Please check FTP preferences.",
                Toast.LENGTH_LONG).show();
        return;
    }

    new Thread() {

        public void run() {
            SharedPreferences prefs = GetNZB.preferences;
            FTPClient ftp = new FTPClient();

            String replycode = "";
            String FTPHostname = prefs.getString("FTPHostname", "");
            String FTPUsername = prefs.getString("FTPUsername", "anonymous");
            String FTPPassword = prefs.getString("FTPPassword", "my@email.address");
            String FTPPort = prefs.getString("FTPPort", "21");
            String FTPUploadPath = prefs.getString("FTPUploadPath", "~/");
            if (!FTPUploadPath.matches("$/")) {
                Log.d(Tags.LOG, "Adding trailing slash");
                FTPUploadPath += "/";
            }
            String targetFile = FTPUploadPath + UPLOADFILENAME;

            try {

                ftp.connect(FTPHostname, Integer.parseInt(FTPPort));
                if (ftp.login(FTPUsername, FTPPassword)) {

                    ftp.setFileType(FTP.BINARY_FILE_TYPE);
                    ftp.enterLocalPassiveMode();
                    File file = new File(getFilesDir() + "/" + UPLOADFILENAME);
                    BufferedInputStream buffIn = new BufferedInputStream(new FileInputStream(file));
                    Log.d(Tags.LOG, "Saving file to:" + targetFile);
                    if (ftp.storeFile(targetFile, buffIn)) {
                        Log.d(Tags.LOG, "FTP: File should be uploaded. Replycode: "
                                + Integer.toString(ftp.getReplyCode()));

                        isUploadedFTP = true;
                    } else {
                        Log.d(Tags.LOG, "FTP: Could not upload file  Replycode: "
                                + Integer.toString(ftp.getReplyCode()));
                        FTPErrorCode = Integer.toString(ftp.getReplyCode());
                        isUploadedFTP = false;
                    }

                    buffIn.close();
                    ftp.logout();
                    ftp.disconnect();

                } else {
                    Log.d(Tags.LOG, "No ftp login");
                }
            } catch (SocketException e) {
                Log.d(Tags.LOG, "ftp(): " + e.getMessage());
                return;
            } catch (IOException e) {
                Log.d(Tags.LOG, "ftp(): " + e.getMessage());
                return;
            }
            if (isUploadedFTP) {
                removeLocalNZBFile(UPLOADFILENAME);
            }

            UPLOADFILENAME = "";
            uploadDialogHandlerFTP.sendEmptyMessage(0);
        }

    }.start();
}

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;/*from   w  ww  .j  a  v a  2s .  c  om*/
    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:com.bdaum.zoom.net.core.ftp.FtpAccount.java

/**
 * Transfers files to the FTP account//from w  w w .java2s  .c o  m
 *
 * @param files
 *            - files to be transferred
 * @param monitor
 *            - progress monitor
 * @param adaptable
 *            - adaptable instance. Must at least provide a Shell instance
 * @param deleteTransferred
 *            - true if transferred files shall be deleted on the client
 *            site
 * @return number of transferred files or -1 in case of abort
 * @throws IOException
 */
public int transferFiles(File[] files, IProgressMonitor monitor, IAdaptable adaptable,
        boolean deleteTransferred) throws IOException {
    filecount = countFiles(files);
    monitor.beginTask(Messages.FtpAccount_uploading_files, filecount + 1);
    FTPClient ftp = null;
    try {
        ftp = login();
        String dir = stripSlashes(getDirectory());
        int n = 0;
        boolean result = ftp.changeWorkingDirectory(Core.encodeUrlSegment(dir));
        if (!result) {
            if (monitor.isCanceled())
                return -1;
            boolean createDir = true;
            IDbErrorHandler errorHandler = Core.getCore().getErrorHandler();
            if (errorHandler != null)
                createDir = errorHandler.question(Messages.FtpAccount_directory_does_not_exist,
                        Messages.FtpAccount_specified_dir_does_not_exist, adaptable);
            if (createDir) {
                StringTokenizer st = new StringTokenizer(dir, "/"); //$NON-NLS-1$
                while (st.hasMoreTokens()) {
                    String token = st.nextToken();
                    ftp.makeDirectory(token);
                    result = ftp.changeWorkingDirectory(Core.encodeUrlSegment(token));
                    if (!result)
                        throw new IOException(Messages.FtpAccount_directory_creation_failed);
                }

            } else
                throw new IOException(Messages.FtpAccount_target_dir_does_not_exist);
        }
        if (files != null) {
            monitor.worked(1);
            skipAll = false;
            replaceAll = false;
            if (monitor.isCanceled())
                return -1;
            n = transferFiles(ftp, files, monitor, adaptable, deleteTransferred);
        }

        // transfer files
        if (isPassiveMode())
            ftp.enterLocalActiveMode();
        ftp.logout();
        return n;
    } finally {
        monitor.done();
        if (ftp != null && ftp.isConnected()) {
            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                // do nothing
            }
        }
    }
}

From source file:edu.cmu.cs.in.hoop.hoops.load.HoopFTPReader.java

/**
 * //  w w w .jav a2 s .  c  o  m
 */
private String retrieveFTP(String aURL) {
    debug("retrieveFTP (" + aURL + ")");

    URL urlObject = null;

    try {
        urlObject = new URL(aURL);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String downloadPath = this.projectToFullPath("<PROJECTPATH>/tmp/download/");
    HoopLink.fManager.createDirectory(downloadPath);

    File translator = new File(urlObject.getFile());

    String localFileName = "<PROJECTPATH>/tmp/download/" + translator.getName();

    OutputStream fileStream = null;

    if (HoopLink.fManager.openStreamBinary(this.projectToFullPath(localFileName)) == false) {
        this.setErrorString("Error opening temporary output file");
        return (null);
    }

    fileStream = HoopLink.fManager.getOutputStreamBinary();

    if (fileStream == null) {
        this.setErrorString("Error opening temporary output file");
        return (null);
    }

    debug("Starting FTP client ...");

    FTPClient ftp = new FTPClient();

    try {
        int reply;

        debug("Connecting ...");

        ftp.connect(urlObject.getHost());

        debug("Connected to " + urlObject.getHost() + ".");
        debug(ftp.getReplyString());

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

        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp.disconnect();
            debug("FTP server refused connection.");
            return (null);
        } else {
            ftp.login("anonymous", "hoop-dev@gmail.com");

            reply = ftp.getReplyCode();

            if (!FTPReply.isPositiveCompletion(reply)) {
                ftp.disconnect();
                debug("Unable to login to FTP server");
                return (null);
            }

            debug("Logged in");

            boolean rep = true;

            String pathFixed = translator.getParent().replace("\\", "/");

            rep = ftp.changeWorkingDirectory(pathFixed);
            if (rep == false) {
                debug("Unable to change working directory to: " + pathFixed);
                return (null);
            } else {
                debug("Current working directory: " + pathFixed);

                debug("Retrieving file " + urlObject.getFile() + " ...");

                try {
                    rep = ftp.retrieveFile(urlObject.getFile(), fileStream);
                } catch (FTPConnectionClosedException connEx) {
                    debug("Caught: FTPConnectionClosedException");
                    connEx.printStackTrace();
                    return (null);
                } catch (CopyStreamException strEx) {
                    debug("Caught: CopyStreamException");
                    strEx.printStackTrace();
                    return (null);
                } catch (IOException ioEx) {
                    debug("Caught: IOException");
                    ioEx.printStackTrace();
                    return (null);
                }

                debug("File retrieved");
            }

            ftp.logout();
        }
    } catch (IOException e) {
        debug("Error retrieving FTP file");
        e.printStackTrace();
        return (null);
    } finally {
        if (ftp.isConnected()) {
            debug("Closing ftp connection ...");

            try {
                ftp.disconnect();
            } catch (IOException ioe) {
                debug("Exception closing ftp connection");
            }
        }
    }

    debug("Closing local file stream ...");

    try {
        fileStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    String result = HoopLink.fManager.loadContents(this.projectToFullPath(localFileName));

    return (result);
}

From source file:edu.lternet.pasta.dml.download.DownloadHandler.java

/**
 * Gets content from given source and writes it to DataStorageInterface 
 * to store them. This method will be called by run()
 * /*from w w w.ja  v a  2s .c o  m*/
 * @param resourceName  the URL to the source data to be retrieved
 */
protected boolean getContentFromSource(String resourceName) {
    boolean successFlag = false;
    QualityCheck onlineURLsQualityCheck = null;
    boolean onlineURLsException = false; // used to determine status of onlineURLs quality check

    if (resourceName != null) {
        resourceName = resourceName.trim();
    }

    if (resourceName != null && (resourceName.startsWith("http://") || resourceName.startsWith("https://")
            || resourceName.startsWith("file://") || resourceName.startsWith("ftp://"))) {
        // get the data from a URL
        int responseCode = 0;
        String responseMessage = null;

        try {
            URL url = new URL(resourceName);
            boolean isFTP = false;

            if (entity != null) {
                String contentType = null;

                // Find the right MIME type and set it as content type
                if (resourceName.startsWith("http")) {
                    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                    httpURLConnection.setRequestMethod("HEAD");
                    httpURLConnection.connect();
                    contentType = httpURLConnection.getContentType();
                    responseCode = httpURLConnection.getResponseCode();
                    responseMessage = httpURLConnection.getResponseMessage();
                } else if (resourceName.startsWith("file")) {
                    URLConnection urlConnection = url.openConnection();
                    urlConnection.connect();
                    contentType = urlConnection.getContentType();
                } else { // FTP
                    isFTP = true;
                    contentType = "application/octet-stream";
                }

                entity.setUrlContentType(contentType);
            }

            if (!isFTP) { // HTTP(S) or FILE
                InputStream filestream = url.openStream();

                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    filestream.close();
                }
            } else { // FTP
                String[] urlParts = resourceName.split("/");
                String address = urlParts[2];
                String dir = "/";
                for (int i = 3; i < urlParts.length - 1; i++) {
                    dir += urlParts[i] + "/";
                }
                String fileName = urlParts[urlParts.length - 1];
                FTPClient ftpClient = new FTPClient();
                ftpClient.connect(address);
                ftpClient.login(ANONYMOUS, anonymousFtpPasswd);
                ftpClient.changeWorkingDirectory(dir);
                ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
                ftpClient.enterLocalPassiveMode(); // necessary to avoid firewall blocking
                InputStream filestream = ftpClient.retrieveFileStream(fileName);
                try {
                    successFlag = this.writeRemoteInputStreamIntoDataStorage(filestream);
                } catch (IOException e) {
                    exception = e;
                    String errorMessage = e.getMessage();
                    if (errorMessage.startsWith(ONLINE_URLS_EXCEPTION_MESSAGE)) {
                        onlineURLsException = true;
                    }
                } finally {
                    try {
                        filestream.close();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(String
                                .format("Error closing local file '%s': %s", resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }

                // logout and disconnect if FTP session
                if (resourceName.startsWith("ftp") && ftpClient != null) {
                    try {
                        ftpClient.enterLocalActiveMode();
                        ftpClient.logout();
                        ftpClient.disconnect();
                    } catch (IOException e) {
                        exception = new DataSourceNotFoundException(
                                String.format("Error disconnecting from FTP with resource '%s': %s",
                                        resourceName, e.getMessage()));
                        onlineURLsException = true;
                    }
                }
            }
        } catch (MalformedURLException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            onlineURLsException = true;
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is a malformed URL: %s", resourceName, eMessage));
        } catch (IOException e) {
            String eClassName = e.getClass().getName();
            String eMessage = String.format("%s: %s", eClassName, e.getMessage());
            if (responseCode > 0) {
                eMessage = String.format("Response Code: %d %s; %s", responseCode, responseMessage, eMessage);
            }
            onlineURLsException = true;
            exception = new DataSourceNotFoundException(
                    String.format("The URL '%s' is not reachable: %s", resourceName, eMessage));
        }

        // Initialize the "Online URLs are live" quality check
        String qualityCheckIdentifier = "onlineURLs";
        QualityCheck qualityCheckTemplate = QualityReport.getQualityCheckTemplate(qualityCheckIdentifier);
        onlineURLsQualityCheck = new QualityCheck(qualityCheckIdentifier, qualityCheckTemplate);

        if (QualityCheck.shouldRunQualityCheck(entity, onlineURLsQualityCheck)) {
            String resourceNameEscaped = embedInCDATA(resourceName);

            if (!onlineURLsException) {
                onlineURLsQualityCheck.setStatus(Status.valid);
                onlineURLsQualityCheck.setFound("true");
                onlineURLsQualityCheck.setExplanation("Succeeded in accessing URL: " + resourceNameEscaped);
            } else {
                onlineURLsQualityCheck.setFailedStatus();
                onlineURLsQualityCheck.setFound("false");
                String explanation = "Failed to access URL: " + resourceNameEscaped;
                explanation = explanation + "; " + embedInCDATA(exception.getMessage());
                onlineURLsQualityCheck.setExplanation(explanation);
            }

            entity.addQualityCheck(onlineURLsQualityCheck);
        }

        return successFlag;
    } else if (resourceName != null && resourceName.startsWith("ecogrid://")) {
        // get the docid from url
        int start = resourceName.indexOf("/", 11) + 1;
        //log.debug("start: " + start);
        int end = resourceName.indexOf("/", start);

        if (end == -1) {
            end = resourceName.length();
        }

        //log.debug("end: " + end);
        String ecogridIdentifier = resourceName.substring(start, end);
        // pass this docid and get data item
        //System.out.println("the endpoint is "+ECOGRIDENDPOINT);
        //System.out.println("The identifier is "+ecogridIdentifier);
        //return false;
        return getContentFromEcoGridSource(ecogridEndPoint, ecogridIdentifier);
    } else if (resourceName != null && resourceName.startsWith("srb://")) {
        // get srb docid from the url
        String srbIdentifier = transformSRBurlToDocid(resourceName);
        // reset endpoint for srb (This is hack we need to figure ou
        // elegent way to do this
        //mEndPoint = Config.getValue("//ecogridService/srb/endPoint");
        // pass this docid and get data item
        //log.debug("before get srb data");
        return getContentFromEcoGridSource(SRBENDPOINT, srbIdentifier);
    } else {
        successFlag = false;
        return successFlag;
    }
}

From source file:it.baywaylabs.jumpersumo.twitter.TwitterListener.java

/**
 * @param host FTP Host name./*w  w w  . j a v a2  s.  c o m*/
 * @param port FTP port.
 * @param user FTP User.
 * @param pswd FTP Password.
 * @param c    Context
 * @return Downloaded name file or blank list if something was going wrong.
 */
private String FTPDownloadFile(String host, Integer port, String user, String pswd, Context c) {
    String result = "";
    FTPClient mFTPClient = null;

    try {
        mFTPClient = new FTPClient();
        // connecting to the host
        mFTPClient.connect(host, port);

        // Now check the reply code, if positive mean connection success
        if (FTPReply.isPositiveCompletion(mFTPClient.getReplyCode())) {

            // Login using username & password
            boolean status = mFTPClient.login(user, pswd);
            mFTPClient.setFileType(FTP.BINARY_FILE_TYPE);
            mFTPClient.enterLocalPassiveMode();

            mFTPClient.changeWorkingDirectory(Constants.DIR_ROBOT_MEDIA);
            FTPFile[] fileList = mFTPClient.listFiles();
            long timestamp = 0l;
            String nameFile = "";
            for (int i = 0; i < fileList.length; i++) {
                if (fileList[i].isFile() && fileList[i].getTimestamp().getTimeInMillis() > timestamp) {
                    timestamp = fileList[i].getTimestamp().getTimeInMillis();
                    nameFile = fileList[i].getName();
                }
            }
            Log.d(TAG, "File da scaricare: " + nameFile);

            mFTPClient.enterLocalActiveMode();
            File folder = new File(Constants.DIR_ROBOT_IMG);
            OutputStream outputStream = null;
            boolean success = true;
            if (!folder.exists()) {
                success = folder.mkdir();
            }

            try {
                outputStream = new FileOutputStream(folder.getAbsolutePath() + "/" + nameFile);
                success = mFTPClient.retrieveFile(nameFile, outputStream);
            } catch (Exception e) {
                return e.getMessage();
            } finally {
                if (outputStream != null) {
                    outputStream.close();
                }
            }
            if (success) {
                result = nameFile;
                mFTPClient.deleteFile(nameFile);
            }
        }
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
    } finally {
        if (mFTPClient != null) {
            try {
                mFTPClient.logout();
                mFTPClient.disconnect();
            } catch (IOException e) {
                Log.e(TAG, e.getMessage());
            }
        }
    }

    return result;
}

From source file:com.maxl.java.aips2sqlite.AllDown.java

public void downIBSA() {
    String fl = "";
    String fp = "";
    String fs = "";
    try {//from   ww  w .  ja  va  2  s  .  c om
        FileInputStream glnCodesCsv = new FileInputStream(Constants.DIR_IBSA + "/access.ami.csv");
        BufferedReader br = new BufferedReader(new InputStreamReader(glnCodesCsv, "UTF-8"));
        String line;
        while ((line = br.readLine()) != null) {
            // Semicolon is used as a separator
            String[] gln = line.split(";");
            if (gln.length > 2) {
                if (gln[0].equals("IbsaAmiko")) {
                    fl = gln[0];
                    fp = gln[1];
                    fs = gln[2];
                }
            }
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    FTPClient ftp_client = new FTPClient();
    try {
        ftp_client.connect(fs, 21);
        ftp_client.login(fl, fp);
        ftp_client.enterLocalPassiveMode();
        ftp_client.changeWorkingDirectory("data");
        ftp_client.setFileType(FTP.BINARY_FILE_TYPE);

        int reply = ftp_client.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            ftp_client.disconnect();
            System.err.println("FTP server refused connection.");
            return;
        }

        System.out.println("- Connected to server " + fs + "...");
        //get list of filenames
        FTPFile[] ftpFiles = ftp_client.listFiles();

        List<String> list_remote_files = Arrays.asList("Konditionen.csv", "Targeting_diff.csv", "Address.csv");
        List<String> list_local_files = Arrays.asList(Constants.FILE_CUST_IBSA, Constants.FILE_TARG_IBSA,
                Constants.FILE_MOOS_ADDR);

        if (ftpFiles != null && ftpFiles.length > 0) {
            int index = 0;
            for (String remote_file : list_remote_files) {
                OutputStream os = new FileOutputStream(Constants.DIR_IBSA + "/" + list_local_files.get(index));
                System.out.print("- Downloading " + remote_file + " from server " + fs + "... ");

                boolean done = ftp_client.retrieveFile(remote_file, os);
                if (done)
                    System.out.println("file downloaded successfully.");
                else
                    System.out.println("error.");
                os.close();
                index++;
            }
        }
    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftp_client.isConnected()) {
                ftp_client.logout();
                ftp_client.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:com.limegroup.gnutella.archive.ArchiveContribution.java

/**
 * /* w ww . j  a v a 2s .c om*/
 * @throws UnknownHostException
 *         If the hostname cannot be resolved.
 *         
 * @throws SocketException
 *         If the socket timeout could not be set.
 *         
 * @throws FTPConnectionClosedException
 *         If the connection is closed by the server.
 *         
 * @throws LoginFailedException
 *         If the login fails.
 *         
 * @throws DirectoryChangeFailedException
 *         If changing to the directory provided by the internet
 *         archive fails.
 *         
 * @throws CopyStreamException
 *         If an I/O error occurs while in the middle of
 *         transferring a file.
 *         
 * @throws IOException
 *         If an I/O error occurs while sending a command or
 *         receiving a reply from the server
 *         
 * @throws IllegalStateException
 *          If the contribution object is not ready to upload
 *          (no username, password, server, etc. set)
 *          or if java's xml parser is configured badly
 */

public void upload()
        throws UnknownHostException, SocketException, FTPConnectionClosedException, LoginFailedException,
        DirectoryChangeFailedException, CopyStreamException, RefusedConnectionException, IOException {

    final int NUM_XML_FILES = 2;
    final String META_XML_SUFFIX = "_meta.xml";
    final String FILES_XML_SUFFIX = "_files.xml";

    final String username = getUsername();
    final String password = getPassword();

    if (getFtpServer() == null) {
        throw new IllegalStateException("ftp server not set");
    }
    if (getFtpPath() == null) {
        throw new IllegalStateException("ftp path not set");
    }
    if (username == null) {
        throw new IllegalStateException("username not set");
    }
    if (password == null) {
        throw new IllegalStateException("password not set");
    }

    // calculate total number of files and bytes

    final String metaXmlString = serializeDocument(getMetaDocument());
    final String filesXmlString = serializeDocument(getFilesDocument());

    final byte[] metaXmlBytes = metaXmlString.getBytes();
    final byte[] filesXmlBytes = filesXmlString.getBytes();

    final int metaXmlLength = metaXmlBytes.length;
    final int filesXmlLength = filesXmlBytes.length;

    final Collection files = getFiles();

    final int totalFiles = NUM_XML_FILES + files.size();

    final String[] fileNames = new String[totalFiles];
    final long[] fileSizes = new long[totalFiles];

    final String metaXmlName = getIdentifier() + META_XML_SUFFIX;
    fileNames[0] = metaXmlName;
    fileSizes[0] = metaXmlLength;

    final String filesXmlName = getIdentifier() + FILES_XML_SUFFIX;
    fileNames[1] = filesXmlName;
    fileSizes[1] = filesXmlLength;

    int j = 2;
    for (Iterator i = files.iterator(); i.hasNext();) {
        final File f = (File) i.next();
        fileNames[j] = f.getRemoteFileName();
        fileSizes[j] = f.getFileSize();
        j++;
    }

    // init the progress mapping
    for (int i = 0; i < fileSizes.length; i++) {
        _fileNames2Progress.put(fileNames[i], new UploadFileProgress(fileSizes[i]));
        _totalUploadSize += fileSizes[i];
    }

    FTPClient ftp = new FTPClient();

    try {
        // first connect

        if (isCancelled()) {
            return;
        }
        ftp.enterLocalPassiveMode();

        if (isCancelled()) {
            return;
        }
        ftp.connect(getFtpServer());

        final int reply = ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            throw new RefusedConnectionException(getFtpServer() + "refused FTP connection");
        }
        // now login
        if (isCancelled()) {
            return;
        }
        if (!ftp.login(username, password)) {
            throw new LoginFailedException();
        }

        try {

            // try to change the directory
            if (!ftp.changeWorkingDirectory(getFtpPath())) {
                // if changing fails, make the directory
                if (!isFtpDirPreMade() && !ftp.makeDirectory(getFtpPath())) {
                    throw new DirectoryChangeFailedException();
                }

                // now change directory, if it fails again bail
                if (isCancelled()) {
                    return;
                }
                if (!ftp.changeWorkingDirectory(getFtpPath())) {
                    throw new DirectoryChangeFailedException();
                }
            }

            if (isCancelled()) {
                return;
            }
            connected();

            // upload xml files
            uploadFile(metaXmlName, new ByteArrayInputStream(metaXmlBytes), ftp);

            uploadFile(filesXmlName, new ByteArrayInputStream(filesXmlBytes), ftp);

            // now switch to binary mode
            if (isCancelled()) {
                return;
            }
            ftp.setFileType(FTP.BINARY_FILE_TYPE);

            // upload contributed files
            for (final Iterator i = files.iterator(); i.hasNext();) {
                final File f = (File) i.next();

                uploadFile(f.getRemoteFileName(), new FileInputStream(f.getIOFile()), ftp);
            }
        } catch (InterruptedIOException ioe) {
            // we've been requested to cancel
            return;
        } finally {
            ftp.logout(); // we don't care if logging out fails
        }
    } finally {
        try {
            ftp.disconnect();
        } catch (IOException e) {
        } // don't care if disconnecting fails
    }

    // now tell the Internet Archive that we're done
    if (isCancelled()) {
        return;
    }
    checkinStarted();

    if (isCancelled()) {
        return;
    }
    checkin();

    if (isCancelled()) {
        return;
    }
    checkinCompleted();
}

From source file:dk.dma.dmiweather.service.FTPLoader.java

/**
 * Check for files every 10 minutes. New files are given to the gridWeatherService
 *//*  w ww . j  a va2s . c o m*/
@Scheduled(initialDelay = 1000, fixedDelay = 10 * 60 * 1000)
public void checkFiles() {
    log.info("Checking FTP files at DMI.");
    FTPClient client = new FTPClient();
    try {
        client.setDataTimeout(20 * 1000);
        client.setBufferSize(1024 * 1024);
        client.connect(hostname);
        if (client.login("anonymous", "")) {
            try {
                client.enterLocalPassiveMode();
                client.setFileType(FTP.BINARY_FILE_TYPE);
                for (ForecastConfiguration configuration : configurations) {
                    // DMI creates a Newest link once all files have been created
                    if (client.changeWorkingDirectory(configuration.getFolder() + "/Newest")) {
                        if (client.getReplyCode() != 250) {
                            log.error("Did not get reply 250 as expected, got {} ", client.getReplyCode());
                        }
                        String workingDirectory = new File(client.printWorkingDirectory()).getName();
                        String previousNewest = newestDirectories.get(configuration);
                        if (!workingDirectory.equals(previousNewest)) {
                            // a new directory for this configuration is available on the server
                            FTPFile[] listFiles = client.listFiles();
                            List<FTPFile> files = Arrays.stream(listFiles)
                                    .filter(f -> configuration.getFilePattern().matcher(f.getName()).matches())
                                    .collect(Collectors.toList());

                            try {
                                Map<File, Instant> localFiles = transferFilesIfNeeded(client, workingDirectory,
                                        files);
                                gridWeatherService.newFiles(localFiles, configuration);
                            } catch (IOException e) {
                                log.warn("Unable to get new weather files from DMI", e);
                            }

                            if (previousNewest != null) {
                                File previous = new File(tempDirLocation, previousNewest);
                                deleteRecursively(previous);
                            }
                            newestDirectories.put(configuration, workingDirectory);
                        }

                    } else {
                        gridWeatherService.setErrorMessage(ErrorMessage.FTP_PROBLEM);
                        log.error("Unable to change ftp directory to {}", configuration.getFolder());
                    }
                }
            } finally {
                try {
                    client.logout();
                } catch (IOException e) {
                    log.info("Failed to logout", e);
                }
            }
        } else {
            gridWeatherService.setErrorMessage(ErrorMessage.FTP_PROBLEM);
            log.error("Unable to login to {}", hostname);
        }

    } catch (IOException e) {
        gridWeatherService.setErrorMessage(ErrorMessage.FTP_PROBLEM);
        log.error("Unable to update weather files from DMI", e);
    } finally {
        try {
            client.disconnect();
        } catch (IOException e) {
            log.info("Failed to disconnect", e);
        }
    }
    log.info("Check completed.");
}