【问题标题】:Display a List of Images stored in PostgresSQL using Servlet and h:graphicImage [duplicate]使用 Servlet 和 h:graphicImage 显示存储在 PostgresQL 中的图像列表
【发布时间】:2015-05-02 00:02:51
【问题描述】:

我无法显示我的数据库中的图像,它们存储为 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>

我正在使用基于 The BalusC Code: ImageServlet 的 servlet,但我尝试使用 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>

【问题讨论】:

  • servlet 的 doGet() 没有被调用,我通过在方法的开头放置一个断点来检查。我也尝试了 value="#{request.contextPath}/images?id=#{image.photographId}",在查看浏览器的 HTTP 控制台后,我看到了&lt;img style="width:100px;" alt="Imagen no encontrada" src="/patientdiagnostics/patientdiagnostics/images?id=11"&gt;。现在我注意到有两次 /patientdiagnostics/patientdiagnostics/ (之前在 src 属性中只有一次)
  • 在浏览器 HTTP 流量监控器中显示 404 http://localhost:8080/patientdiagnostics/patientdiagnostics/images?id=11
  • 我删除了#{request.contextPath},在浏览器的网络控制台上仍然显示 404。
  • URL 是 http://localhost:8080/patientdiagnostics/pages/images?id=11 在我注意到 pages 在 URL 中之后,我在 pages 文件夹中创建了一个 images 文件夹,但我仍然看到 404
  • 改用&lt;img src="#{request.contextPath}/images?id=#{image.photographId}" ... /&gt;

标签: jsf servlets jsf-2.2 graphicimage


【解决方案1】:

这不是真正和真实的答案,但可能会有所帮助,如果根据您的 IDE 不需要数据库,您可以直接将图片与源代码文件一起存储并通过以下方式直接访问它们:

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(image, int distFromLeft, int distFromTop, horrizontalSize, verticleSize, null)

【讨论】:

  • 您将 Swing 与 HTTP/HTML 混淆了。
  • 实际上这适用于我的代码
  • 您严重混淆了“服务器端”和“客户端”。只有当服务器和客户端都在物理上同一台机器上运行时,它才会“工作”。
猜你喜欢
  • 2014-09-03
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 2012-07-28
  • 2020-07-29
  • 2015-07-01
  • 2010-09-08
  • 1970-01-01
相关资源
最近更新 更多