0

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

4

1 回答 1

-2

这不是真正的答案,但可能会有所帮助,如果不需要数据库,具体取决于您的 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(图像,int distFromLeft,int distFromTop,horrizontalSize,verticleSize,空)

于 2015-05-02T00:28:10.973 回答