Corrupted image using Primefaces UploadedFile with FTPClient

Hello guys I am having problem in the Primefaces component "File Upload", when trying to save the file via" FtpClient " the file gets corrupted.

Form configuration

<h:form id="formNavegacao" enctype="multipart/form-data">

The part of the FileUpload in the web file.xml

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>commons</param-value>
</context-param>
<filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>Faces Servlet</servlet-name>
    <dispatcher>REQUEST</dispatcher>
    <!--Tags rewrites para upload do primefaces-->
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Follows my xhtml where I use the component.

                                <p:fileUpload id="navegacaoSuperiorMenuLogoTipo"
                                          styleClass="navegacaoSuperiorMenuLogoTipo waves-effect waves-light btn left-align"
                                          update="navegacaoComponente:formNavegacao:navegacao"
                                          auto="true" mode="advanced" skinSimple="true" 
                                          label="#{text['AdicionarLogotipo']}"
                                          allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                                          fileUploadListener="#{editorMB.salvaArquivoTmp}"/>

Method where I capture the image

    public void salvaArquivoTmp(FileUploadEvent fileUploadEvent) {
    try {
        UploadedFile uploadedFile = fileUploadEvent.getFile();
        if (uploadedFile != null) {
            sessionMB.getConexaoFtp().enviarArquivoFtp("logotipo-tmp.png", uploadedFile.getInputstream());
            layout.getTopo().setCaminhoLogotipo(sessionMB.getEmpresa().getDominio() + "/" + "logotipo-tmp.png");
        }
    } catch (IOException exception) {
        GerenciadorBugs.enviandoBug(TratamentoErro.setValoresException(exception));
    }
}

The class where I insert the file via FtpClient

package br.com.redew.util;

import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.net.ftp.FTPClient;

public class ConexaoFtp {

    private String host;
    private String usuario;
    private String senha;
    private String pastaInicial;
    private final FTPClient fTPClient;

    public ConexaoFtp() {
        fTPClient = new FTPClient();
    }

    private void conectandoFtp() throws IOException {
        fTPClient.connect(host);
        fTPClient.enterLocalPassiveMode();
        fTPClient.login(usuario, senha);
        fTPClient.changeWorkingDirectory(pastaInicial);
    }

    private void desconectandoFtp() throws IOException {
        fTPClient.logout();
        fTPClient.disconnect();
    }

    public void enviarArquivoFtp(String caminho, InputStream arquivo) {
        try {
            conectandoFtp();
            fTPClient.storeFile(caminho, arquivo);
            desconectandoFtp();
        } catch (IOException iOException) {
            GerenciadorBugs.enviandoBug(TratamentoErro.setValoresException(iOException));
        }
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getUsuario() {
        return usuario;
    }

    public void setUsuario(String usuario) {
        this.usuario = usuario;
    }

    public String getSenha() {
        return senha;
    }

    public void setSenha(String senha) {
        this.senha = senha;
    }

    public String getPastaInicial() {
        return pastaInicial;
    }

    public void setPastaInicial(String pastaInicial) {
        this.pastaInicial = pastaInicial;
    }
}

When uploading an "html" file via " FtpClient" it worked now when I try to upload an image the file gets corrupted.

Primefaces 6.1

Author: Catatau, 2017-07-11

1 answers

I tested today and it worked I think it was cache problem for not having worked before, is solved.

FTPClient.setFileType (FTP.BINARY_FILE_TYPE);

 0
Author: Catatau, 2017-07-13 12:41:28