Boa noite pessoal!
Peço a desculpa pela domora, mas estava estudando todas as possibilidades de como eu poderia prosseguir.
Li algumas coisas sobre JAXB e gostei muito da facilidade de uso do mesmo, mas acabei optando em tratar apenas um arquivo .csv.
Bem, com relação ao componente de <p:fileUpload> ainda estou apanhando bem.
Busquei bastante material sobre o mesmo e acabei fazendo o seguinte:
.xhtml
</h:form>
<p:dialog id="basicDialog" header="Importar arquivo" widgetVar="dlg1">
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{controlProductivityMB.file}" mode="simple" update="grow1"/>
<p:commandButton value="#{bundle.ok}" action="#{controlProductivityMB.upLoad()}" />
<p:growl id="grow1" showDetail="true"/>
</h:form>
Bean:
[code]@ManagedBean
@SessionScoped
public class controlProductivityMB implements Serializable {
/**
* Creates a new instance of controlProductivityMB
*/
private String destination = "/tmp";
private UploadedFile file;
public void setFile(UploadedFile file) {
this.file = file;
}
public UploadedFile getFile() {
return file;
}
public void setTransferFile(String fileName, InputStream in) {
try {
OutputStream out = new FileOutputStream(new File(destination + fileName));
int reader = 0;
byte[] bytes = new byte[(int) getFile().getSize()];
while ((reader = in.read(bytes)) != -1) {
out.write(bytes, 0, reader);
}
in.close();
out.flush();
out.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
public void upLoad() {
String extValidate;
if (getFile() != null) {
String ext = getFile().getFileName();
if (ext != null) {
extValidate = ext.substring(ext.indexOf(".") + 1);
} else {
extValidate = "null";
}
if (extValidate.equals("pdf")) {
try {
setTransferFile(getFile().getFileName(), getFile().getInputstream());
} catch (IOException ex) {
Logger.getLogger(controlProductivityMB.class.getName()).log(Level.SEVERE, null, ex);
FacesContext faces = FacesContext.getCurrentInstance();
faces.addMessage(null, new FacesMessage("Wrong", "Error UpLoding File"));
}
FacesContext faces = FacesContext.getCurrentInstance();
faces.addMessage(null, new FacesMessage("Sucessful", getFile().getFileName() + "is uploaded. Typecontent " + getFile().getContentType() + "Size" + getFile().getSize()));
} else {
FacesContext faces = FacesContext.getCurrentInstance();
faces.addMessage(null, new FacesMessage("Wrong_ext", "only extension in .pdf"));
}
} else {
FacesContext faces = FacesContext.getCurrentInstance();
faces.addMessage(null, new FacesMessage("Wrong", "Select a file."));
}
}
}[/code]
Mapeamento web.xml
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/uploaded</param-value>
</init-param>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>51200</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>/tmp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
O problema que está ocorrendo no momento é que meu objeto file está vindo nulo.
Não consigo enxergar onde está o erro.
Obrigado pela atenção de todos até o momento.