我正在尝试实现 ImageUpload 并使用 DynamicImage 在同一页面上立即显示上传的图像。我的问题是,我无法强制刷新 p:graphicImage 内容并在上传后显示上传的图像。
@ManagedBean(name = "myformbean")
@Controller
@ViewScoped
@Data
public class MyFormBean implements Serializable {
private StreamedContent listImage = null;
public StreamedContent getListImage() {
if (listImage == null) {
try {
listImage = new DefaultStreamedContent(new FileInputStream("E:/t.jpg"), "image/png"); // load a dummy image
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
return listImage;
}
public void handleFileUpload(FileUploadEvent event) {
final UploadedFile uploadedFile = event.getFile();
listImage = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFile.getContents()), "image/png");
}
}
在 .xhtml 文件中:
<p:graphicImage value="#{myformbean.listImage}" />