Null pointer in Part filetype when uploading

1

I'm having a proplem using javax.servlet.http.Part . It is coming null when I upload.

Test XHTML

<?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 you have 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, Part foto is coming null. Can anyone give me an idea of what happens?

Thank you!

    
asked by anonymous 27.02.2015 / 15:54

1 answer

1

It was really the version I was using

I changed my dependencies:

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

by:

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

I just updated the javax.servlet version in my pom.xml

    
27.02.2015 / 18:34