Image corrupted using Uploadedfile from Primefaces with Ftpclient

Asked

Viewed 144 times

0

Hello folks I am having problem in Primefaces component "File Upload", while trying to save the file via "Ftpclient" the file gets corrupted.

Form configuration

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

The Fileupload part in 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;
    }
}

While uploading an "html" file via "Ftpclient" worked now when trying to upload an image the file gets corrupted.

Primefaces 6.1

  • Tested images of various sizes and in all the problem occurs?

  • Yes only image gets corrupted coming from File Upload component, file like "txt" or "html" type I tested and worked.

  • I think the problem should not be with the JSF component, but with FTP. Do a test, in the connecting methodFtp() add the instruction fTPClient.setFileType(FTP.BINARY_FILE_TYPE);

  • I tried here still corrupt I put on this link the image http://www.weder.sitepessoal.com/logotype-tmp.png

  • There in the directory you save the image by FTP, came to check if the file extension is correct. For example, if there is something like image.png.png. Make sure the image size is the same as the original. I couldn’t identify anything wrong with the code.

  • So I checked the name is correct, and the two file by amazing looks have the same size 25 KB.

  • It’s... hard. All of a sudden you try to save this file to any directory, without using FTP, just to make sure you save it right or not. Dai will get a confirmation if the problem is with FTP or not.

  • I saved the file on my machine and it worked, the same problem is with FTP that sad kk.

Show 3 more comments

1 answer

0

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

fTPClient.setFileType(FTP.BINARY_FILE_TYPE);

  • Gee... what a thing. I’m glad you decided to test that instruction again.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.