java 如何将 blob 转换为 JSP 中显示的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11616955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:47:39  来源:igfitidea点击:

How do i convert blob to image displayed in a JSP

javajspblob

提问by FredHomme

I am trying to retrieve images from database. Currently i was able to show :

我正在尝试从数据库中检索图像。目前我能够展示:

    `com.mysql.jdbc.Blob@2aba2aba `

in my jsp output. May i know how to convert that into an image?

在我的jsp输出中。我可以知道如何将其转换为图像吗?

i have use the below to call out the above

我用下面的来呼出上面的

photo[i].getPhotoFileData();

回答by Mike Deck

This is more of an issue with the way HTML documents work than with your JSP. You need to understand that HTML doesn't embed images directly. Instead, it uses <img>tags to reference images hosted at different URLs.

与 JSP 相比,这更像是 HTML 文档工作方式的问题。您需要了解 HTML 不直接嵌入图像。相反,它使用<img>标签来引用托管在不同 URL 上的图像。

In order to display an image stored in a database on an HTML page you're going to need a separate servlet that can handle requests for the image. Your JSP should render an HTML document like the following:

为了在 HTML 页面上显示存储在数据库中的图像,您将需要一个单独的 servlet 来处理对图像的请求。您的 JSP 应该呈现如下所示的 HTML 文档:

<html>
  <head>
  ...
  </head>
  <body>
    ...
    <img src="www.mydomain.com/images/1234.png" />
    ...
  </body>
</html>

Then you would create a separate servlet to handle all the requests to /images which would make a database call and send the raw bytes from the blob it gets back to the response's output stream. Make sure you also set the Content-Type header correctly based on what image encoding you're using.

然后,您将创建一个单独的 servlet 来处理对 /images 的所有请求,这将进行数据库调用并将原始字节从 blob 发送回响应的输出流。确保您还根据您使用的图像编码正确设置了 Content-Type 标头。

In order to send the image back to the requester you have one of two options. You can get the blob's bytes as an array and write that to the OutputStream (e.g. out.write(blob.getBytes(0,blob.length());). Or you can use the getBinaryStream()method and then copy bytes from the InputStream to the OutputStream. Here's an example of that:

为了将图像发送回请求者,您有两个选择之一。您可以将 blob 的字节作为数组获取并将其写入 OutputStream(例如out.write(blob.getBytes(0,blob.length());)。或者您可以使用该getBinaryStream()方法,然后将字节从 InputStream 复制到 OutputStream。这是一个例子:

public static void copy(Blob from, OutputStream to)
    throws IOException {
  byte[] buf = new byte[4096];
  try(InputStream is = from.getBinaryStream()) {
    while (true) {
      int r = is.read(buf);
      if (r == -1) {
        break;
      }
      to.write(buf, 0, r);
    }
  }
}

NB: This code has not been tested or even compiled, it should only be used as a starting point.

注意:此代码尚未经过测试甚至编译,仅应用作起点。

回答by pafau k.

You're getting a Blob object - not it's contents. If you want to get raw byte data you have to ask the Blob object for it, e.g.:

你得到的是一个 Blob 对象——而不是它的内容。如果你想获得原始字节数据,你必须向 Blob 对象询问它,例如:

Blob blob =  photo[i].getPhotoFileData();
byte[] data = blob.getBytes(0, blob.length());

If you want to create an image on the fly, then just call:

如果您想即时创建图像,只需调用:

BufferedImage img = ImageIO.read(new ByteArrayInputStream(data));

You can then save the image or ... actually I don't know what else. Thing. Stuff. Display it. Print. Limitless possibilities! Just like at zombo.com!

然后您可以保存图像或...实际上我不知道还有什么。事物。东西。显示它。打印。无限可能!就像在 zombo.com 一样!

回答by abhishek ringsia

first convert blob to input stream to string . then use that String instead of image URL .

首先将 blob 转换为输入流到 string 。然后使用该 String 而不是 image URL 。

Converting blob to String

将 blob 转换为字符串

        try {
            Blob blob = staticOffer.getImage(); //blob of image from db
            strOut = new StringBuffer();
            String aux;
            BufferedReader br;

            br = new BufferedReader(new InputStreamReader(blob.getBinaryStream()));
            while ((aux=br.readLine())!=null) {
                strOut.append(aux);
            }
            offerPicStr = strOut.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

Now use that string it html/jsp in following way

现在以以下方式使用该字符串 html/jsp

<img src="data:image/jpeg;base64,${offerPicStr}" width="100" height="100"></img>