【发布时间】:2015-07-13 22:16:27
【问题描述】:
我无法显示我的数据库中的图像,它们存储为 bytea,我正在像这样映射它们:
@Entity
@Table(name = "photograph", schema = "public")
public class Photograph{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "photograph_id", unique = true, nullable = false)
private Long id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "diagnostic_id", nullable = false, insertable = false, updatable = false)
private Diagnostic diagnostic;
@Column(name = "photo", nullable = false)
private byte[] photo;
@Column(name = "photograph_description", nullable = false, length = 100)
private String photographDescription;
@Column(name = "photograph_content_type")
private String photographContentType;
//Getters and Setters...
}
我可以毫无问题地将所有图像存储在数据库中。问题是当我想像这样在 p:dataTable 中显示它们时:
<p:dataTable id="dataTableLoadedPhotos"
value="#{imageController.photographListUpdate}" var="image">
<p:column headerText="Fotografías cargadas" width="110">
<o:graphicImage value="#{imageStreamer.getById(image.photographId)}"
alt="#{msgs['label.diagnostic.photograph.notFound']}" />
</p:column>
</p:dataTable>
我正在使用基于The BalusC Code: ImageServlet 的流媒体,我尝试使用o:graphicImage 没有成功,mi 代码中缺少某些内容:
@ManagedBean
@ApplicationScoped
public class ImageStreamer {
@EJB
private PhotographService photographService;
public byte[] getById(Long id) {
try {
return photographService.getContent(id);
} catch (ServiceException e) {
FacesMessage mensaje = new FacesMessage(
FacesMessage.SEVERITY_ERROR,
"Error al buscar la fotografía ", e.getMessage());
FacesContext.getCurrentInstance().addMessage(null, mensaje);
}
return null;
}
}
我还有一个带有 @RequestScoped 的托管 bean:
@ManagedBean
@RequestScoped
public class ImageController {
@EJB
private PhotographService photographService;
@ManagedProperty(value = "#{diagnosticDataManager}")
private DiagnosticDataManager diagnosticDataManager;
private List<Photograph> photographListUpdate = new ArrayList<Photograph>();
private Photograph selectedPhoto;
/**
*
*/
public ImageController() {
diagnosticDataManager = new DiagnosticDataManager();
}
@PostConstruct
public void init() {
if (diagnosticDataManager.getDiagnostic().getDiagnosticId() != null)
photographListUpdate = photographService
.findPhotosByDiagnostic(diagnosticDataManager
.getDiagnostic());
for (Photograph photograph : photographListUpdate) {
byte[] imageContent = org.apache.commons.codec.binary.Base64
.decodeBase64(photograph.getPhoto());
ExternalContext ec = FacesContext.getCurrentInstance()
.getExternalContext();
ec.getSessionMap()
.put(photograph.getId().toString(),
imageContent);
}
}
// Getters and setters....
}
我只看到 p:dataTable 中的两行对应于没有图像预加载的图像,只显示了 o:graphicImage 的“alt”属性的消息
使用萤火虫我只在每一行看到这个:
<img alt="Imagen no encontrada" src="/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7">
我也试过类似Display database blob images in <p:graphicImage> inside <ui:repeat>的东西
【问题讨论】:
-
我使用 ap:fileUpload 将图像保存在 DB 中,init() 方法有
byte[] imageContent = org.apache.commons.codec.binary.Base64 .decodeBase64(photograph.getPhoto());因为我在其他问题中看到,我不确定图像是否存储为 base64 编码字节,我只是将 jpg 图像上传到数据库,我认为由于图像大小,我不得不这样解码,什么是正确的方法? -
我删除了所有base64的东西,我仍然看到
<img alt="Imagen no encontrada" src="/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7">,在浏览器的网络上我看到http://localhost:8080/patientdiagnostics/javax.faces.resource/ImageStreamer_getById.xhtml?ln=omnifaces.graphic&v=0&p=7,它有一列写着200 OK(BFCache)。如果您需要查看我将图像保存到数据库的方法,请告诉我 -
src="data:image;base64,"
-
我想我修复了方法,现在我得到的是 src="data:image;base64,AAAAAAAAAAAAAAAAAAAAA......AAAAAAA" 但我还看不到图像
-
好吧,显然它只返回零。
标签: jsf jsf-2.2 omnifaces graphicimage