显示bytea到图像,jsp?

时间:2018-10-07 03:21:53

标签: java jsp web byte

您能帮我将保存的图像转换为数据库(字节,在PostgreSQL中),图像并显示在网页(jsp)上吗?

我以这种方式转换图像,然后保存在数据库中:

    Part part = req.getPart("profilePic");

    byte[] prfilePic = new byte[(int) part.getSize()];
    InputStream stream = part.getInputStream();
    stream.read(prfilePic);
    stream.close();

但是我如何才能将Bytea再次转换为图像并显示呢?

2 个答案:

答案 0 :(得分:0)

InputStream in = new ByteArrayInputStream(prfilePic);
BufferedImage bImageFromConvert = ImageIO.read(in);

答案 1 :(得分:0)

从数据库获取de bytea,使用:

String url = "data:image/jpeg;base64," + Base64.getEncoder().encodeToString(user.getProfilePic());
session.setAttribute("url", url);

然后在jsp上使用会话:

<img src="${sessionScope.url}">