I read many pages but, i'm sure, I did some errors somewhere and I can't do the upload of a file simply as It should be.
First, I'm developing using JSF2.0, Primefaces 3.2 and JPA2 on Glassfish 3.1 with Netbeans 7 and JDK 1.7.
The next thing that I must say is that I'm tringg to insert this code into an interface that will be extended from many others classes and that should store the files in many folders on filesystem! So now I will report what I wrote and, please, tell me where is the problem!
This is the code into 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>D:/Cyborg/</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
The page that contains the p:uploadFile contains also many other form's fields and I will copy only what I think is of your interest:
...
<h:form method="POST">
...
<h:outputLabel for="uploadFile" value="#{bundle.LabelUploadFile}" />
<p:fileUpload value="#{progettiController.uploadFile}" mode="simple" />
...
<h:commandLink action="#{progettiController.create}" value="#{bundle.SaveLink}" />
...
this is the code into the interface called AbstractController:
protected UploadedFile uploadFile;
public void setUploadFile(UploadedFile uploadFile) {
this.uploadFile=uploadFile;
}
public UploadedFile getUploadFile() {
return uploadFile;
}
and finally the method Create into ProgettiController:
public String create() {
try {
getFacade().create(current);
System.out.println("Message: ProgettiController: Create: new row created successfully!");
try {
current=getFacade().findLast();
String ext=uploadFile.getFileName();
System.out.println("Message: ProgettiController: Create: FileName="+ext);
ext=ext.substring(ext.lastIndexOf("."),ext.length());
System.out.println("Messaggio: ProgettiController: ext="+ext);
current.setNomeFile(ext.substring(0,ext.lastIndexOf(".")));
current.setTipoFile(ext.substring(ext.lastIndexOf(".")+1,ext.length()));
getFacade().edit(current);
ext=urlFilesystem+current.getId()+ext.substring(ext.lastIndexOf(".")+1,ext.length());
System.out.println("Message: ProgettiController: Create: posizione e nome del file="+ext);
File oldFile= (File)uploadFile;
File newFile= new File(ext);
oldFile.renameTo(newFile);
} catch (Exception e) {
System.out.println("Messaggio: ProgettiController: Create: errore nel try legato al upload="+e.getMessage());
e.printStackTrace();
}
JsfUtil.addSuccessMessage(ResourceBundle.getBundle("/Bundle").getString("ProgettiCreated"));
recreateModel();
System.out.println("Messaggio: ProgettiController: create try");
return url+"List?faces-redirect=true";
} catch (Exception e) {
JsfUtil.addErrorMessage(e, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
System.out.println("Messaggio: ProgettiController: create catch="+e.getMessage());
e.printStackTrace();
return url+"List?faces-redirect=true";
}
}
If I try this code the problem is that when the page call the method returns null into uploadFile, if I add the "enctype="multipart/form-data"" into h:form tag the application didn't call the method! Can someone help me? Thanks a lot!