boosta.artem.services.FTPResultReader.java Source code

Java tutorial

Introduction

Here is the source code for boosta.artem.services.FTPResultReader.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boosta.artem.services;

import boosta.artem.Main;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

/**
 *
 * @author artem
 */
public class FTPResultReader {

    public void readResult() {
        String server = "62.210.82.210";
        int port = 55021;
        String user = "misha_p";
        String pass = "GfhjkM1983";

        FTPClient ftpClient = new FTPClient();
        try {

            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.ASCII_FILE_TYPE);

            // APPROACH #1: using retrieveFile(String, OutputStream)
            String remoteFile = Main.remote_out_path;
            System.out.println(remoteFile);
            //String remoteFile = "/results/ttgenerate.txt";
            String downlFile = Main.file_name;
            System.out.println(downlFile);
            File downloadFile = new File(downlFile);
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(downloadFile));

            boolean success = ftpClient.retrieveFile(remoteFile, outputStream);

            outputStream.close();

            if (success) {
                System.out.println(" ?   FTP!!!");
            } else {
                System.out.println("  ?   TFP!!!");
            }

        } catch (IOException ex) {
            System.out.println("   ? FTP  !!!");
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                System.out.println(" ? ?? ? FTP!!!");
            }
        }
    }
}