public class MainCtl {
UploadedFile upFile;
...
public String upload() throws IOException {
// This is using Hibernate to figure out what folder path
should be used to store the file.
Sysctl sys = Sysctl.getRecord();
FacesContext fc = FacesContext.getCurrentInstance();
fc.getExternalContext().getApplicationMap().put("fileupload_bytes",
upFile.getBytes());
fc.getExternalContext().getApplicationMap().put("fileupload_type",
upFile.getContentType());
fc.getExternalContext().getApplicationMap().put("fileupload_name",
upFile.getName());
String guid = (new VMID()).toString().replaceAll(":", "");
writeFile(upFile, sys.getUploadfolder().trim() + guid);
return null;
}
private void writeFile(UploadedFile uf, String file) throws IOException {
InputStream is = uf.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
int c;
while ((c = is.read()) != -1) {
fos.write(c);
}
}
public UploadedFile getUpFile() {
return upFile;
}
public void setUpFile(UploadedFile x) {
upFile = x;
}
}
-------jsp---------
<x:inputFileUpload id="fileupload"
accept="image/*"
value="#{MainCtl.upFile}"
storage="file"
styleClass="input"
required="false"/>
<h:commandButton value="load it up" action="#{MainCtl.upload}"
styleClass="button"/>