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!
What version of the Servlet and JSF specification are you using? Using some custom filter to handle file receiving or using the native version?
– Wakim
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 !!
– sheldonmera