Corrupted image using UploadedFile of primefaces with FTPClient

0

Hello everyone, I have a problem with the "File Upload" component of Primefaces. When trying to save the file via "FtpClient" the file gets corrupted.

Form configuration

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

The FileUpload part of the web.xml file

<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>

Follow 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;
    }
}

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

Primefaces 6.1

    
asked by anonymous 11.07.2017 / 21:54

1 answer

0

I tested it today and it worked I think it was cache problem so it did not work before, it's solved.

  

fTPClient.setFileType (FTP.BINARY_FILE_TYPE);

    
13.07.2017 / 14:41