I added FileUploadForm.java as a managed bean in my app which I call
from my upload form copied from UploadForm.jsp - when I click 'upload' I
get this error:
SEVERE: #{uploadController.upload}: javax.faces.el.EvaluationException:
java.lang.NullPointerException
javax.faces.FacesException: #{uploadController.upload}:
javax.faces.el.EvaluationException: java.lang.NullPointerException
at
com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:78)
at javax.faces.component.UICommand.broadcast(UICommand.java:312)
Further down the dump I see the offending line:
facesContext.getExternalContext().getApplicationMap().put("fileupload_bytes",
_upFile.getBytes());
Why is this null? Why, if upFile is bound to the inputText control on
the download form is this FacesContext gymnastics needed when my bean is
scopded 'session'?
================================== uploadform.java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.myfaces.custom.fileupload.UploadedFile;
import javax.faces.context.FacesContext;
import java.io.*;
public class FileUploadForm
{
private UploadedFile _upFile;
private String _name = "";
public UploadedFile getUpFile()
{
return _upFile;
}
public void setUpFile(UploadedFile upFile)
{
_upFile = upFile;
}
public String getName()
{
return _name;
}
public void setName(String name)
{
_name = name;
}
public String upload() throws IOException
{
System.out.println("Uploading");
FacesContext facesContext = FacesContext.getCurrentInstance();
facesContext.getExternalContext().getApplicationMap().put("fileupload_bytes",
_upFile.getBytes());
facesContext.getExternalContext().getApplicationMap().put("fileupload_type",
_upFile.getContentType());
facesContext.getExternalContext().getApplicationMap().put("fileupload_name",
_upFile.getName());
System.out.println("file " + _upFile.getName());
return null;
}
public boolean isUploaded()
{
FacesContext facesContext = FacesContext.getCurrentInstance();
return
facesContext.getExternalContext().getApplicationMap().get("fileupload_bytes")!=null;
}
private void writeFile(UploadedFile uf) throws IOException {
// create new trinityurldoc with original fname, type and desc,
return new filename in format FILE000n
System.out.append("Writing file " + uf.getName());
//String fname = appService.createNewFile(uf.getName(),
getUpFileDescription(), uf.getContentType());
InputStream is = uf.getInputStream();
FileOutputStream fos = new
FileOutputStream(uf.getName()); //defaultDir+fname);
int c;
while ((c = is.read()) != -1) {
fos.write(c);
}
}
}
**************************** Upload form
<h:panelGrid border="1" bgcolor="Aquamarine">
<h:form id="uploadForm">
<h:outputText value="Upload" />
<x:inputFileUpload id="fileupload"
accept="image/*"
value="#{uploadController.upFile}"
storage="file"
styleClass="fileUploadInput"
required="true"/>
<h:outputText value="Description" />
<h:inputText value="#{uploadController.name}" />
<h:commandButton value="Upload"
action="#{uploadController.upload}"
styleClass="button" />
</h:form>
***************************** Facesconfig
<managed-bean>
<description>Upload Form</description>
<managed-bean-name>uploadController</managed-bean-name>
<managed-bean-class>
mateoweb.FileUploadForm
</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>