我正在使用带有 primfaces 的 jsf2.0 并使用 p:fileupload 来上传照片。这里我需要在支持 bean 中传递参数。因为没有通过 p:fileupload 传递参数的选项。我也使用了绑定选项,但它在支持 bean 上返回空值...
这是我的 fileupload.xhtml
<h:form enctype="multipart/form-data">
<p:panel header="Upload Photos" id="getImageId"
style="font-size:12px;height:499px">
<p:messages id="messages" for="imaload"></p:messages>
<p:fileUpload id="imaload" fileUploadListener="#
{ngoPhotoUpload.photoUpload}"
mode="advanced" multiple="true"
immediate="true"
update="messages"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>
<!-- <p:growl life="1000" id="messages"/> -->
</p:panel>
</h:form>
这是我的支持 bean 方法
public void photoUpload(FileUploadEvent event) throws IOException,
InterruptedException {
BufferedImage bufferedImage;
String tmpFile = scontext.getRealPath(("/ngoPhotos/")
+ event.getFile().getFileName());
File result = new File(tmpFile);
byte[] imageByte = event.getFile().getContents();
storeImage(imageByte, tmpFile);
String imageName = event.getFile().getFileName();
String createdby = loginBean.getEmail();
if (loginBean.getType().equals("admin")
|| loginBean.getType().equals("ngo_coordinator")) {
if (ngoRegnPojo.getNgo_id() != 0) {
ngoPhotoBean.setNgo_id(ngoRegnPojo.getNgo_id());
} else {
ngoPhotoBean.setNgo_id(loginBean.getUser_id());
}
} else {
ngoPhotoBean.setNgo_id(loginBean.getUser_id());
}
ngoPhotoBean.setImageName(imageName);
ngoPhotoBean.status = "status";
ngoPhotoBean.setCreatedDate(new Date());
ngoPhotoBean.setCreatedby(createdby);
//ngoPhotoBean.setPathName(tmpFile);
ngoPhotoBean.disable = "false";
bufferedImage = ImageIO.read(result);
if (bufferedImage.getWidth() <= 400 && bufferedImage.getHeight() <= 400) {
ngoPhotoBean.disable = "false";
getMthd(imageByte, tmpFile);
} else {
try {
ngoPhotoBean.disable = "false";
bufferedImage = ImageIO.read(result);
ImageIO.write(resize(bufferedImage, 400, 400), "jpg", new File(
tmpFile));
photoUploadDaoService.uploadNgoPhoto(ngoPhotoBean);
NgoPhotoBean ngoPhotoBean = new NgoPhotoBean();
FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap().put("ngoPhotoBean", ngoPhotoBean);
FacesMessage msg = new FacesMessage("Successfully Uploaded");
FacesContext.getCurrentInstance().addMessage(null, msg);
} catch (Exception e) {
e.printStackTrace();
FacesMessage error = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"The files were not uploaded!", "");
FacesContext.getCurrentInstance().addMessage(null, error);
}
}
}
提前致谢