我可以从我的数据库中显示图像,它们被存储为 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:imagservlet和我尝试使用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中没有错误,但是我看不到页面中的图像,代码中缺少什么?
我还尝试了一些类似的内容,类似于&lt; p:graphicimage&gt; gt;内部&lt; ui:重复&gt;
这不是真正且真正的答案,但可能会有所帮助,如果数据库不需要您,则根据您的不同,您可以将图片直接存储在源代码文件中,并直接使用:
getClass().getResoure(name of image.fileType);
//fileType example png jpg gif so image.png`
示例:
ImageIcon icon = new ImageIcon(getClass().getResource("Icon.png"));
BufferedImage bf = ImageIO.read(getClass().getResource("image.png"));
绘画缓冲图像
ga.drawimage(图像,int distfromleft,int distfromtop,horrizontalsize,verticlesize,null)