如何从JSP页面中的数据库检索和显示图像?
如何从JSP页面中的数据库中检索和显示图像?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何从JSP页面中的数据库中检索和显示图像?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(6)
让我们在步骤中查看应该发生的事情:
< img>
元素。src
属性。src
属性需要指向有效的http://
url,因此不是本地磁盘文件系统路径file> file> file> file> file> file>当服务器和客户端在物理上不同的机器上运行时,将永远无法使用。
http://example.com/context/images/foo.png
)中具有图像标识符或作为请求参数(例如http:http:http:http:http:http:http: //example.com/context/images?id=1
)。/Images/*
,以便您可以在特定URL上执行一些Java代码。byte []
或inputStream
从db, jdbc api 提供resultset#getBytes()
和resultset#getBinarySet#getBinarystream()
为此,并且 jpa api 提供@lob
为此。byte []
或inputStream
utputsstream
posters 通常 java io way。content-type
也需要设置响应标头。您可以通过ServletContext#getMimetype()
基于图像文件扩展名,您可以通过Web中的
< mime-mapping>
在Web中扩展和/或覆盖。 XML
。应该就是这样。它几乎写代码本身。让我们从html开始(在 jsp ):
您可以在必要时动态设置
src
,withsrc
a href =“ https://stackoverflow.com/tags/el/info”> el 使用 a>:然后定义/创建 servlet 在
/images/images/*< /code>,以下示例使用普通的香草JDBC进行工作:
就是这样。如果您担心头部和缓存标题并根据这些请求做出适当响应,请使用此静态资源servlet的抽象模板。
另请参见:
Let's see in steps what should happen:
<img>
element.src
attribute.src
attribute needs to point to a validhttp://
URL and thus not a local disk file system pathfile://
as that would never work when the server and client run at physically different machines.http://example.com/context/images/foo.png
) or as request parameter (e.g.http://example.com/context/images?id=1
)./images/*
, so that you can just execute some Java code on specific URL's.byte[]
orInputStream
from the DB, the JDBC API offers theResultSet#getBytes()
andResultSet#getBinaryStream()
for this, and JPA API offers@Lob
for this.byte[]
orInputStream
to theOutputStream
of the response the usual Java IO way.Content-Type
response header needs to be set as well. You can obtain the right one viaServletContext#getMimeType()
based on image file extension which you can extend and/or override via<mime-mapping>
inweb.xml
.That should be it. It almost writes code itself. Let's start with HTML (in JSP):
You can if necessary also dynamically set
src
with EL while iterating using JSTL:Then define/create a servlet which listens on GET requests on URL pattern of
/images/*
, the below example uses plain vanilla JDBC for the job:That's it. In case you worry about HEAD and caching headers and properly responding on those requests, use this abstract template for static resource servlet.
See also:
我建议您将其作为两个问题。有几个问题和答案与两者相关。
如何从mysql加载斑点
参见例如检索作为blob < /a>
如何动态显示图像
参见例如 /p>
I suggest you address that as two problems. There are several questions and answer related to both.
How to load blob from MySQL
See for instance Retrieve image stored as blob
How to display image dynamically
See for instance Show thumbnail dynamically
我已经使用Oracle数据库编写并配置了JSP中的代码。
希望它会有所帮助。
I've written and configured the code in JSP using Oracle database.
Hope it will help.
如果不显示输出流,请尝试冲洗并关闭输出流。
blob image = rs.getBlob(ImageColname);
inputStream in = image.getBinarystream();
//将斑点输出到httpservletresponse
response.setContentType(“ Image/jpeg”);
bufferedOutputStream o = new BufferedOutputStream(response.getOutputStream());
Try to flush and close the output stream if it does not display.
Blob image = rs.getBlob(ImageColName);
InputStream in = image.getBinaryStream();
// Output the blob to the HttpServletResponse
response.setContentType("image/jpeg");
BufferedOutputStream o = new BufferedOutputStream(response.getOutputStream());
我使用了SQL Server数据库,因此答案的代码符合。您所要做的就是在JSP页面中包括一个
&lt; img&gt;
标记,并从其SRC属性中调用Servlet这样的Servlet,此处1是数据库中image的唯一ID,并且ID是一个变量。我们在Servlet中收到此变量的值。在Servlet代码中,我们从表中的正确列中获取二进制流输入。那就是您的图像存储在哪个列中。在我的代码中,我使用了第三列,因为我的图像在第三列中存储为二进制数据。从表中检索输入流数据后,我们在输出流中读取其内容,因此可以写在屏幕上。这是
在执行JSP或HTML文件后,您将在屏幕上看到图像。
I used SQL SERVER database and so the answer's code is in accordance. All you have to do is include an
<img>
tag in your jsp page and call a servlet from its src attribute like thisHere 1 is unique id of image in database and ID is a variable. We receive value of this variable in servlet. In servlet code we take the binary stream input from correct column in table. That is your image is stored in which column. In my code I used third column because my images are stored as binary data in third column. After retrieving input stream data from table we read its content in an output stream so it can be written on screen. Here is it
After the execution of your jsp or html file you will see the image on screen.
您还可以创建用于显示图像的自定义标签。
1)创建自定义标签Java类和TLD文件。
2)写逻辑以显示图像,例如BASE 64的字节[]转换为字符串。
因此,无论您仅在单个JSP页面中显示一个图像还是多个图像,它都用于每个图像。
You can also create custom tag for displaying image.
1) create custom tag java class and tld file.
2) write logic to display image like conversion of byte[] to string by Base64.
so it is used for every image whether you are displaying only one image or multiple images in single jsp page.