Check you are correctly using
"org.apache.myfaces.custom.fileupload.UploadedFile" for the type of you
java bean. Since you didn't provide us with import rules, it might be
you are using the Trinidad Uploadedfile or
org.apache.commons.fileupload.FileUpload, which are different classes /
interfaces with similar API. Since they wouldn't match the tomahawk
fileupload type, the setter wouldn't be found by EL, and wouldn't be called.
Another possibility, you are uploading file greater than the limit you
set in your web.xml
le3000 a écrit :
I follow the example in "Upload Files with JSF and MyFaces" from ONJava.com
to do the file upload. I am having problem on UploadedFile null pointer.
The UploadedFile always return null. actually, the setter-setMyFile never
even get called.
Here is what I have in my jsp:
<[EMAIL PROTECTED] uri="http://java.sun.com/jsf/core" prefix="f"%>
<[EMAIL PROTECTED] uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@ taglib uri="http://myfaces.apache.org/tomahawk" prefix="t"%>
<f:subview id="sendJobsub">
<h:form id="sendJob" enctype="multipart/form-data">
<h:messages globalOnly="true" styleClass="message"/>
<h:panelGrid columns="3" border="0" cellspacing="5">
<h:outputLabel for="myFileId" value="File: "/>
<t:inputFileUpload id="myFileId" value="#{SendJobBean.myFile}"
storage="file" required="true"/>
<h:outputLabel for="myParamId" value="Param: "/>
<h:selectOneMenu id="myParamId" value="#{SendJobBean.myParam}"
required="true">
<f:selectItem itemLabel="" itemValue=""/>
<f:selectItem itemLabel="MD5" itemValue="MD5"/>
<f:selectItem itemLabel="SHA-1" itemValue="SHA-1"/>
<f:selectItem itemLabel="SHA-256" itemValue="SHA-256"/>
<f:selectItem itemLabel="SHA-384" itemValue="SHA-384"/>
<f:selectItem itemLabel="SHA-512" itemValue="SHA-512"/>
</h:selectOneMenu>
<h:message for="myParamId"/>
<h:outputText value=" "/>
<h:commandButton value="Upload XML File"
action="#{SendJobBean.processXMLFile}"/>
<h:outputText value=" "/>
</h:panelGrid>
</h:form>
</f:subview>
Here is my web.xml:
<filter>
<filter-name>ExtensionsFilter</filter-name>
<filter-class>org.apache.myfaces.webapp.filter.ExtensionsFilter</filter-class>
<init-param>
<param-name>uploadMaxFileSize</param-name>
<param-value>100m</param-value>
</init-param>
<init-param>
<param-name>uploadThresholdSize</param-name>
<param-value>100k</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>ExtensionsFilter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<filter-mapping>
<filter-name>ExtensionsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
My bean is very simple:
.......
private UploadedFile myFile;
.......
public void setMyFile(UploadedFile myFile){
System.out.println("setting uploaded file");
this.myFile=myFile;
}
public UploadedFile getMyFile(){
System.out.println("getting uploaded file");
return this.myFile;
}
public void processXMLFile() throws Exception {
String myResult="";
try {
MessageDigest md = MessageDigest.getInstance(myParam);
InputStream in = new
BufferedInputStream(myFile.getInputStream());
try {
byte[] buffer = new byte[64 * 1024];
int count;
while ((count = in.read(buffer)) > 0)
md.update(buffer, 0, count);
} finally {
in.close();
}
byte hash[] = md.digest();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < hash.length; i++) {
int b = hash[i] & 0xFF;
int c = (b >> 4) & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
c = b & 0xF;
c = c < 10 ? '0' + c : 'A' + c - 10;
buf.append((char) c);
}
myResult = buf.toString();
System.out.println(myResult);
} catch (Exception x) {
x.printStackTrace();
FacesMessage message = new FacesMessage(
FacesMessage.SEVERITY_FATAL,
x.getClass().getName(), x.getMessage());
FacesContext.getCurrentInstance().addMessage( null, message);
}
}
It always always catch null pointer on UploadedFile, and the
UploadedFile(myFile) getter being call when I load that page, but the setter
never being called.
Please help. I've been struggling on this for days. :-(