从Mysql数据库显示图像而不存储到本地系统 [英] display image from Mysql database without storing to local system

查看:145
本文介绍了从Mysql数据库显示图像而不存储到本地系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Struts应用程序,我想以BLOB数据类型显示MySQL DB中存在的图像.

I'm developing a Struts application where I want to display images present in MySQL DB in BLOB datatype.

我不想将这些图像存储到本地系统,但是我想直接在浏览器中显示它们.我们需要将它们存储在临时存储器中. 我能够获取它并将其存储到FileOutputStrem中,并且需要从该对象中将图像传递给JSP.

I don't want to store those images to local system but I want to directly display them in a browser. We need to store them in an temp memory. I am able to fetch it and store it into FileOutputStrem and from this object i need to pass an image to JSP.

下面是代码

ResultSet result = statement.executeQuery();

if (result.next()) {
    Blob blob = result.getBlob("photo");
    InputStream inputStream = blob.getBinaryStream();
    // read the input stream...

}

请让我知道我该怎么做.

Please let me know how can I do this.

推荐答案

您可以使用

byte[] content = result.getBytes("photo");
response.setContentType("image/jpg");
response.setContentLength(content.length);
response.getOutputStream().write(content);

HTML代码

$(document).ready(function () {
    $("#submit").click(function () {
        $.ajax({
            type: "GET",
            url: "servleturl",
            success: function (result) {
                $("#content").html('<img src="'+result+'" >'); 
            }
        });
    });
}); 

这篇关于从Mysql数据库显示图像而不存储到本地系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆