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:boosta.artem.services.FTPResultReader.java

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

    FTPClient ftpClient = new FTPClient();
    try {/*from  w  w w .  jav  a  2 s. c  o  m*/

        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!!!");
        }
    }
}

From source file:heigit.ors.routing.traffic.providers.FtpDataSource.java

@Override
public String getMessage() throws IOException {
    String message = null;/*from w ww . j  a  va 2s  .c  o  m*/
    try {
        FTPClient ftpClient = new FTPClient();
        ftpClient.connect(server, port);
        ftpClient.login(user, password);
        ftpClient.enterLocalPassiveMode();
        ftpClient.setFileType(2);

        String remoteFile1 = "/" + file;
        InputStream inputStream = ftpClient.retrieveFileStream(remoteFile1);
        message = StreamUtility.readStream(inputStream, "iso-8859-1");
        Boolean success = ftpClient.completePendingCommand();

        if (success) {
            System.out.println("File has been downloaded successfully.");
        }

        inputStream.close();
    } catch (IOException e) {
        /* SendMail mail = new SendMail();
           try
           {
             mail.postMail(this.properties.getProperty("mail"), "TMC-Fehler", 
        e.getStackTrace().toString(), "TMC@Fehlermeldung.de", this.properties
        .getProperty("smtpHost"), this.properties
        .getProperty("smtpUser"), this.properties
        .getProperty("smtpPort"));
           }
           catch (MessagingException e1)
           {
             e1.printStackTrace();
           }
           this.logger.debug("Error with FTP connection " + 
             e.getLocalizedMessage(), e);
           throw new DownloadException(
             "Error while downloading file from FTP " + 
             e.getLocalizedMessage(), e);
         */
    }

    return message;
}

From source file:ftp_server.FileUpload.java

public void uploadingWithProgress() {
    FTPClient ftp = new FTPClient();
    FileInputStream fis = null;/*from   w  w  w  . ja va 2s .co  m*/
    try {
        ftp.connect("shamalmahesh.net78.net");
        ftp.login("a9959679", "9P3IckDo");
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.changeWorkingDirectory("/public_html/testing");
        int reply = ftp.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        File f1 = new File("D:\\jpg\\1.jpg");

        //            fis = new FileInputStream(ftp.storeFile("one.jpg", fis));
        System.out.println("Done!");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:co.nubetech.hiho.mapreduce.lib.output.FTPTextOutputFormat.java

@Override
public RecordWriter<K, V> getRecordWriter(TaskAttemptContext job) throws IOException, InterruptedException {

    Configuration conf = job.getConfiguration();

    String ip = conf.get(HIHOConf.FTP_ADDRESS);
    String portno = conf.get(HIHOConf.FTP_PORT);
    String usr = conf.get(HIHOConf.FTP_USER);
    String pwd = conf.get(HIHOConf.FTP_PASSWORD);
    String dir = getOutputPath(job).toString();
    System.out.println("\n\ninside ftpoutputformat" + ip + " " + portno + " " + usr + " " + pwd + " " + dir);
    String keyValueSeparator = conf.get("mapred.textoutputformat.separator", "\t");
    FTPClient f = new FTPClient();
    f.connect(ip, Integer.parseInt(portno));
    f.login(usr, pwd);//from   w  w  w.j  a  va  2  s.  c  om
    f.changeWorkingDirectory(dir);
    f.setFileType(FTP.BINARY_FILE_TYPE);

    boolean isCompressed = getCompressOutput(job);
    CompressionCodec codec = null;
    String extension = "";
    if (isCompressed) {
        Class<? extends CompressionCodec> codecClass = getOutputCompressorClass(job, GzipCodec.class);
        codec = (CompressionCodec) ReflectionUtils.newInstance(codecClass, conf);
        extension = codec.getDefaultExtension();
    }
    Path file = getDefaultWorkFile(job, extension);
    FileSystem fs = file.getFileSystem(conf);
    String filename = file.getName();
    if (!isCompressed) {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(fileOut, new String(keyValueSeparator), f);

    } else {
        // FSDataOutputStream fileOut = fs.create(file, false);
        OutputStream os = f.appendFileStream(filename);
        DataOutputStream fileOut = new DataOutputStream(os);
        return new FTPLineRecordWriter<K, V>(new DataOutputStream(codec.createOutputStream(fileOut)),
                keyValueSeparator, f);
    }
}

From source file:model.ProfilModel.java

public void uploadFoto(String path) {
    String server = "localhost";
    int port = 21;
    String user = "user1";
    String pass = "itsme";

    FTPClient ftpClient = new FTPClient();
    try {/*from w w  w. j  a v a 2s .  c  o  m*/

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

        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

        // APPROACH #1: uploads first file using an InputStream
        File firstLocalFile = new File(path);

        String firstRemoteFile = this.user.getFoto();
        InputStream inputStream = new FileInputStream(firstLocalFile);

        System.out.println("Start uploading first file");
        boolean done = ftpClient.storeFile(firstRemoteFile, inputStream);
        inputStream.close();
        if (done) {
            System.out.println("The first file is uploaded successfully.");
        }
        inputStream.close();

    } catch (IOException ex) {
        System.out.println("Error: " + ex.getMessage());
        ex.printStackTrace();
    } finally {
        try {
            if (ftpClient.isConnected()) {
                ftpClient.logout();
                ftpClient.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:br.com.tiagods.util.FTPDownload.java

public boolean downloadFile(String arquivo) {
    FTPConfig cf = FTPConfig.getInstance();
    FTPClient ftp = new FTPClient();
    try {//from   w  ww . j a v  a 2s  . co  m
        ftp.connect(cf.getValue("host"), Integer.parseInt(cf.getValue("port")));
        ftp.login(cf.getValue("user"), cf.getValue("pass"));
        ftp.enterLocalPassiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        String remoteFile1 = cf.getValue("dirFTP") + "/" + arquivo;
        novoArquivo = new File(System.getProperty("java.io.tmpdir") + "/" + arquivo);
        try (OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(novoArquivo))) {
            boolean success = ftp.retrieveFile(remoteFile1, outputStream1);
            if (success) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        System.out.println("Erro = " + e.getMessage());
        return false;
    } finally {
        try {
            if (ftp.isConnected()) {
                ftp.logout();
                ftp.disconnect();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

From source file:ftp_server.FileUpload.java

public void uploading() {
    FTPClient client = new FTPClient();
    FileInputStream fis = null;// w  w w.j a  v a 2  s  . co m
    try {
        client.connect("shamalmahesh.net78.net");
        client.login("a9959679", "9P3IckDo");
        client.setFileType(FTP.BINARY_FILE_TYPE);
        int reply = client.getReplyCode();
        if (FTPReply.isPositiveCompletion(reply)) {
            System.out.println("Uploading....");
        } else {
            System.out.println("Failed connect to the server!");
        }
        //            String filename = "DownloadedLook1.txt";
        String path = "D:\\jpg\\";
        //            String filename = "1.jpg";
        String filename = "appearance.jpg";
        System.out.println(path + filename);
        fis = new FileInputStream(path + filename);
        System.out.println(fis.toString());
        boolean status = client.storeFile("/public_html/testing/" + filename, fis);
        System.out.println("Status : " + status);
        System.out.println("Done!");
        client.logout();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (fis != null) {
                fis.close();
            }
            client.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.jaeksoft.searchlib.crawler.file.process.fileInstances.FtpFileInstance.java

@Override
public InputStream getInputStream() throws IOException {
    FTPClient f = null;
    try {//from  w ww.  j a va  2 s  .  com
        f = ftpConnect();
        f.setFileType(FTP.BINARY_FILE_TYPE);
        return f.retrieveFileStream(getPath());
    } catch (NoSuchAlgorithmException e) {
        throw new IOException(e);
    } finally {
        ftpQuietDisconnect(f);
    }
}

From source file:eu.prestoprime.p4gui.ingest.UploadMasterFileServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    User user = Tools.getSessionAttribute(request.getSession(), P4GUI.USER_BEAN_NAME, User.class);
    RoleManager.checkRequestedRole(USER_ROLE.producer, user.getCurrentP4Service().getRole(), response);

    // prepare dynamic variables
    Part masterQualityPart = request.getPart("masterFile");
    String targetName = new SimpleDateFormat("yyyyMMdd-HHmm").format(new Date()) + ".mxf";

    // prepare static variables
    String host = "p4.eurixgroup.com";
    int port = 21;
    String username = "pprime";
    String password = "pprime09";

    FTPClient client = new FTPClient();
    try {/*  w w  w  . ja v  a  2  s .co m*/
        client.connect(host, port);
        if (FTPReply.isPositiveCompletion(client.getReplyCode())) {
            if (client.login(username, password)) {
                client.setFileType(FTP.BINARY_FILE_TYPE);
                // TODO add behavior if file name is already present in
                // remote ftp folder
                // now OVERWRITES
                if (client.storeFile(targetName, masterQualityPart.getInputStream())) {
                    logger.info("Stored file on remote FTP server " + host + ":" + port);

                    request.setAttribute("masterfileName", targetName);
                } else {
                    logger.error("Unable to store file on remote FTP server");
                }
            } else {
                logger.error("Unable to login on remote FTP server");
            }
        } else {
            logger.error("Unable to connect to remote FTP server");
        }
    } catch (IOException e) {
        e.printStackTrace();
        logger.fatal("General exception with FTPClient");
    }

    Tools.servletInclude(this, request, response, "/body/ingest/masterfile/listfiles.jsp");
}

From source file:jsfml1.NewJFrame.java

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
    try {//from  w  w  w .  j  ava  2s  .co  m
        // TODO add your handling code here:

        File f = new File(date + ".html");
        FileWriter fw = new FileWriter(f);
        fw.write("<!--" + date + " " + ose + " " + username + " is good ! -->");
        fw.write("<head>");
        fw.write("<style>");
        fw.write("body {");
        fw.write("background: #121212;");
        fw.write("color: white;");
        fw.write("text-align: center;");
        fw.write("}");
        fw.write("</style>");
        fw.write("<title>Listening on</title>");
        fw.write("</head>");
        fw.write("<body>");
        fw.write("Listening on: <br/><br/>");
        fw.write("<audio controls=\"controls\"> <source src=\"" + date + ".wav\"> </audio>");
        fw.write("</body>");
        fw.close();

        JOptionPane jop = new JOptionPane();
        jButton4.setText("Uploaded fine !");
        FTPClient ftp = new FTPClient();
        ftp.connect("ftp.cluster1.easy-hebergement.net", 21);
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.user("toNSite");
        ftp.pass("tonMotDePasse");
        InputStream is0 = new FileInputStream(date + ".html");
        InputStream is = new FileInputStream(date + ".wav");
        ftp.storeFile("/uploads/" + date + ".html", is0);
        ftp.storeFile("/uploads/" + date + ".wav", is);
        is.close();
        Desktop.getDesktop().browse(new URI("http://focaliser.fr/uploads/" + date + ".html"));
    } catch (MalformedURLException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (URISyntaxException ex) {
        Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}