我有这个XHTML代码:
<h:form>
<ice:graphicImage value="#{uploadImage.byteArrayImage}"
rendered="#{uploadImage.byteArrayImage!=null}"/>
<ace:fileEntry id="fileEntryComp"
label="File Entry"
relativePath="uploaded"
useOriginalFilename="true"
fileEntryListener="#{uploadImage.customValidator}"/>
<h:commandButton value="Upload Image" / >
<h:message for="fileEntryComp" />
</h:form>
这个用于上传图片的java方法:
public void customValidator(FileEntryEvent entryEvent) throws IOException {
FileEntryResults results = ((FileEntry) entryEvent.getComponent()).getResults();
for (FileEntryResults.FileInfo file : results.getFiles()) {
if (file.isSaved()) {
if (!file.getContentType().equals("image/png")) {
file.updateStatus(new FileEntryStatus() {
@Override
public boolean isSuccess() {
return false;
}
@Override
public FacesMessage getFacesMessage(
FacesContext facesContext, UIComponent fileEntry, FileEntryResults.FileInfo fi) {
return new FacesMessage(FacesMessage.SEVERITY_ERROR,
"Only PNG files can be uploaded. Your upload has been cancelled.",
"Only PNG files can be uploaded. Your upload has been cancelled.");
}
},
true, true);
} else {
this.byteArrayImage = readIntoByteArray(new FileInputStream(file.getFile()));
}
}
}
}
我需要在页面上上传和显示图片,但它确实有用,我不知道为什么。谢谢你的回答。