使用Wicket在MYSQL数据库中显示以BLOB形式存储的图像 [英] Using Wicket to display an image stored as BLOB in MYSQL db

查看:108
本文介绍了使用Wicket在MYSQL数据库中显示以BLOB形式存储的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在包含图像的mysql数据库中有一个blob。
我想要的是将此图像显示给用户。



我不能将它放在目录结构中,因为blob可能是从图像到一块HTML,所以有逻辑涉及它是否是一块html或图像等。



我正在考虑检查BLOB以查看它是否是一个图像,它可以输出到一个临时目录,然后加载它作为资源,但后来我注意到wicket有一个BlobImageResource类,但我不知道这是如何实现的,我无法找到任何示例后搜索谷歌。 p>

任何建议如何最好地做到这一点?

我正在使用wicket 6.xx并能访问spring和hibernate,并不反对使用第三方库

解决方案

从MySql DB到HTML页面的Blob图像的Wicket代码



public class BlobToImage extends WebPage {b / b
$ b

  private staticBlob blob; 
public BlobToImage(){

BlobImageResource blobImgSrc = new BlobImageResource(){
$ b @Override
protected Blob getBlob(Attributes attributes){
返回blob;
}
};
尝试{
getBlob();
} catch(SQLException e){
e.printStackTrace();
}

Image img = new Image(image,blobImgSrc);
add(img);
}

public static void getBlob()throws SQLException {
属性ConProps = new Properties();
ConProps.put(user,root);
ConProps.put(password,root);

连接conn = DriverManager.getConnection(jdbc:mysql:// localhost:3306 / google_map,ConProps);
System.out.println(conn);

java.sql.Statement stmt = conn.createStatement();
stmt.execute(SELECT id,image FROM images where id = 1);
ResultSet rs = stmt.getResultSet();
while(rs.next()){
try {
blob = rs.getBlob(image);
} catch(Exception e){
e.printStackTrace();
}
}
}

}
以下HTML代码有助于在网页上显示图片

 图片< img wicket:id = imagealt =image> 


I currently have a blob in my mysql db that contains an image. What i want is to display this image to the user.

I cannot place it in a directory structure as the blob may be anything from an image to a piece of html so there is logic involved as to whether it is a piece of html or an image etc.

I was thinking of checking the BLOB to see if it is an image and it can output it to a temporary directory then load it as a resource but then I noticed wicket has a BlobImageResource class however I am not sure how this is to be implemented and am unable to find any examples after searching on google.

Any suggestions how best to go about doing this ?

i am using wicket 6.xx and have access to spring and hibernate and am not against using third party libraries

解决方案

Wicket code for blob image from MySql DB to HTML page

public class BlobToImage extends WebPage{

private static Blob blob;
public BlobToImage() {

    BlobImageResource blobImgSrc = new BlobImageResource() {

        @Override
        protected Blob getBlob(Attributes attributes) {             
            return blob;
        }
    };
    try {
        getBlob();
    } catch (SQLException e) {
        e.printStackTrace();
    }   

    Image img = new Image("image", blobImgSrc);
    add(img);
}

public static void getBlob() throws SQLException{
    Properties ConProps = new Properties();
    ConProps.put("user","root");
    ConProps.put("password", "root");

    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/google_map",ConProps);
    System.out.println(conn);

    java.sql.Statement stmt = conn.createStatement();
    stmt.execute("SELECT id,image FROM images where id=1");
    ResultSet rs = stmt.getResultSet();
    while(rs.next()){
        try{
            blob = rs.getBlob("image");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

} The following HTML code helps to show the image at the webpage

    Image <img wicket:id="image"  alt="image">      

这篇关于使用Wicket在MYSQL数据库中显示以BLOB形式存储的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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