I am uploading file in JSF and it is always null
so that I get a NullPointerException
.
My backingBean
:
@ManagedBean
@SessionScoped
public class UploadFile implements Serializable {
private Part fileUp;
public String processFileUpload() throws IOException {
Part uploadedFile = getFileUp();
final Path destination = Paths.get("c:/temp/" + FilenameUtils.getName(getSubmittedFileName(uploadedFile)));
InputStream bytes = null;
if (null != uploadedFile) {
bytes = uploadedFile.getInputStream(); //Copies bytes to destination.
Files.copy(bytes, destination);
}
return "success";
}
public Part getFileUp() {
return fileUp;
}
public void setFileUp(Part fileUp) {
this.fileUp = fileUp;
}
My front page:
<h:form id="fileUpload" enctype="multipart/form-data">
<h:outputLabel for="fu" value="Name:" />
<h:inputFile id="fu" value="#{uploadFile.fileUp}">
</h:inputFile>
<p>
<h:messages id="messagesUpload" />
</p>
<h:commandButton value="Upload File" action="#{uploadFile.processFileUpload}">
<f:ajax execute="fileUpload" render="@all" />
</h:commandButton>
</h:form>
Could anyone please help me set the file to its value and tell me why it's getting the null value?