@Renato_Wilder escreveu:
Ola a todos tenho o seguinte problema estou tentando implementar com primefaces uma tela tela te upload de arquivos, só que quando executo passo o arquivo e clico para fazer o upload ele me retorna NullPointerException já tentei pegar o nome do arquivo com o value em uma variável mais também não resolveu se alguém puder me ajudar fico grato abaixo o código do prime faces e minha classe de upload
<h:form class="upload" > <p:fileUpload fileUploadListener="#{upload_file.handleFileUpload}" mode="advanced" dragDropSupport="false" multiple="true" update="messages" sizeLimit="100000" fileLimit="100" allowTypes="/(\.|\/#_)(gif|jpe?g|png|.bat|.rar)$/" /> <p:growl id="messages" showDetail="true" /> </h:form>
public class FileUploadView { private String arquivo; public String getArquivo() { return arquivo; } public void setArquivo(String arquivo) { this.arquivo = arquivo; } public void handleFileUpload(FileUploadEvent event) throws FileNotFoundException, IOException { try { ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext(); HttpServletResponse response = (HttpServletResponse) externalContext.getResponse(); FacesContext aFacesContext = FacesContext.getCurrentInstance(); ServletContext context = (ServletContext) aFacesContext.getExternalContext().getContext(); String realPath = context.getRealPath("/"); byte[] arquivo = event.getFile().getContents(); String caminho = realPath + "/scripts/" + event.getFile().getFileName(); try ( // esse trecho grava o arquivo no diretório FileOutputStream fos = new FileOutputStream(caminho)) { fos.write(arquivo); FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded."); FacesContext.getCurrentInstance().addMessage(null, message); // mensagem pra saber se ouve sucesso } } catch (Exception ex) { System.out.println("Erro no upload de imagem" + ex); } } }
Mensagens: 5
Participantes: 2