Null Pointer in Part filetype when upload

Asked

Viewed 156 times

1

I have a purpose using the javax.servlet.http.Part. It is coming null when I upload.

XHTML of the test

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>Teste</title>
    </h:head>
    <h:body>
        <h:form enctype= "multipart/form-data" >

            <h:outputLabel class="col-sm-2 col-sm-2 control-label">Imagem </h:outputLabel>

            <h:inputFile label="Carregar Imagem" value="#{uploadController.foto}"  />
            <h:commandButton  value="Salvar" action="#{uploadController.uploadFoto}" />


        </h:form>
    </h:body>
</html>

Controllerupload

import javax.faces.bean.ManagedBean;
import javax.servlet.http.Part;
import org.springframework.stereotype.Controller;
import br.com.ofertacidade.model.util.UploadUtil;
import javax.faces.bean.SessionScoped;

public class UploadController {

    private String imagem = "";
    private Part foto;


    public String UploadFoto() {
        UploadUtil util = new UploadUtil();
        imagem = util.Upload(foto);

        return "ok";

    }

//getters setters

}

Uploadutil-where has validations and upload

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.servlet.http.Part;

public class UploadUtil {

    private final int limitTamanho = 300000;
    private final String tipoArquivo = "png|jpeg|jpg|gif";
    private final String caminhoArquivo = "D://test//";

    public UploadUtil() {
    }

    public String Upload(Part foto) {
        String arquivoSalvo = "semImagens.jpg";
        try {
            if (foto.getSize() > 0) {
                String nomeArquivo = getFilename(foto);
                if (verificaTipoArquivo(nomeArquivo)) {
                    if (foto.getSize() > this.limitTamanho) {
                        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Arquivo muito grande !", ""));
                    } else {
                        String nomeAtualArquivo = nomeArquivo;
                        String ext = nomeAtualArquivo.substring(nomeAtualArquivo.lastIndexOf("."), nomeAtualArquivo.length());
                        String nomeArquivoFinal = nomeAtualArquivo + ext;
                        String fileSavePath = FacesContext.getCurrentInstance().getExternalContext().getRealPath(caminhoArquivo);

                        try {
                            byte[] conteudoArquivo = new byte[(int) foto.getSize()];
                            InputStream input = foto.getInputStream();
                            input.read(conteudoArquivo);
                            File novoArquivo = new File(fileSavePath, nomeArquivoFinal);
                            File pasta = new File(fileSavePath);
                            if (!pasta.exists()) {
                                pasta.mkdirs();
                            }
                            FileOutputStream outPut = new FileOutputStream(novoArquivo);
                            outPut.write(conteudoArquivo);
                            outPut.flush();
                            outPut.close();
                            arquivoSalvo = nomeArquivoFinal;

                        } catch (IOException e) {
                            arquivoSalvo = "semImagens.jpg";
                        }
                    }

                } else {
                    arquivoSalvo = "semImagens.jpg";
                }
            }
        } catch (Exception ex) {
            arquivoSalvo = "semImagens.jpg";
        }
        return arquivoSalvo;
    }

    private boolean verificaTipoArquivo(String nomeArquivo) {
        if (nomeArquivo.length() > 0) {
            String[] tipoParts = nomeArquivo.split("\\.");
            if (tipoParts.length > 0) {
                String ext = tipoParts[tipoParts.length - 1];
                return this.tipoArquivo.contains(ext);

            }

        }
        return false;
    }

    private String getFilename(Part foto) {
        for (String cd : foto.getHeader("content-disposition").split(";")) {
            if (cd.trim().startsWith("filename")) {
                String filename = cd.substring(cd.indexOf('=') + 1).trim().replace("\"", "");
                return filename.substring(filename.lastIndexOf('/') + 1).substring(filename.lastIndexOf('\\') + 1);
            }
        }
        return null;
    }

}

Whenever I debug, the Part foto is coming null. Someone can give me an idea of what happens?

Thank you!

  • 1

    What version of the Servlet and JSF specification are you using? Using some custom filter to handle file receiving or using the native version?

  • 1

    Putz, thank you very much Walkin, was the version I was using of Servlet, had not noticed that only from the 3.0 that was implemented from the Part, was using the 2.5. Thank you very much !!

1 answer

1


It really was the version I was using

I traded my dependencies :

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>2.5</version>
    </dependency>

for:

 <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
    </dependency>

I just updated the version of javax.Servlet in my pom.xml

Browser other questions tagged

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