我无法显示数据库中的图像,它们存储为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">
<h:graphicImage value="images?id=#{image.photographId}" style="width:100px;"
alt="#{msgs['label.diagnostic.photograph.notFound']}" />
</p:column>
</p:dataTable>
我正在使用基于BalusC 代码的 servlet:ImageServlet,我尝试使用o:graphicImage没有成功,mi 代码中缺少一些东西:
@WebServlet("/images/*") //<<<<<<<I am not sure if this is ok
public class ImageServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@EJB
private PhotographService photographService;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Get ID from request.
String imageId = request.getParameter("id");
// Check if ID is supplied to the request.
if (imageId == null) {
// Do your thing if the ID is not supplied to the request.
// Throw an exception, or send 404, or show default/warning image,
// or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Lookup Image by ImageId in database.
// Do your "SELECT * FROM Image WHERE ImageID" thing.
Photograph image = null;
try {
image = photographService.findPhotoById(Long.valueOf(imageId));
} catch (ServiceException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Check if image is actually retrieved from database.
if (image == null) {
// Do your thing if the image does not exist in database.
// Throw an exception, or send 404, or show default/warning image,
// or just ignore it.
response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
return;
}
// Init servlet response.
response.reset();
response.setContentType(image.getPhotographContentType());
response.setContentLength(image.getPhoto().length);
// Write image content to response.
response.getOutputStream().write(image.getPhoto());
}
}
当我使用 maven 时,我的应用程序目录如下:
src
|----main
|----webapp
|----images
我的 server.log 中没有错误,但我在页面中看不到图像,我的代码中缺少什么?
我还尝试了类似于Display database blob images in <p:graphicImage> inside <ui:repeat>