我有字符串路径,用于在标题上显示图像,我从数据库中获取返回图像字符串文件名的路径,然后图像位于“D:\Job\Potapota\images\(filename)”中。我已经在 a.setValue(dao.getLogos()); 上初始化了来自 db 的 String 路径。这意味着如果我只想像 a.getValue() 那样获取文件名。
所以这是我的 html 页面
<p:graphicImage value="#{fileUploadViewBean.imageStatus}" library="atlantis-layout"/>
那么 FileUploadViewBean 类就是这个
public class FileUploadViewBean extends CommonBean {
private StreamedContent imageStatus;
public void init() throws IOException{
getLogo();
}
public StreamedContent getImageStatus() {
return imageStatus;
}
public void setImageStatus(StreamedContent imageStatus) {
this.imageStatus = imageStatus;
}
public void getLogo() throws IOException {
SystemMaster a = new SystemMaster();
FacesContext context = FacesContext.getCurrentInstance();
try {
a.setValue(dao.getLogos());
} catch (Exception e) {
setMessage(new FacesMessage(e.getMessage()));
getMessage().setSeverity(FacesMessage.SEVERITY_FATAL);
FacesContext.getCurrentInstance().addMessage(null, getMessage());
}
if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
// So, we're rendering the view. Return a stub StreamedContent so
// that it will generate right URL.
imageStatus = new DefaultStreamedContent();
} else {
// So, browser is requesting the image. Return a real
// StreamedContent with the image bytes.
String filename =
context.getExternalContext().getRequestParameterMap().get(a.getValue());
imageStatus = new DefaultStreamedContent(
new FileInputStream(new File("D:\\Job\\Potapota\\images\\",
filename)), "image/jpeg");
}
}
但是随后图像没有显示出来,有人可以知道什么是真正的代码。