开发者

display an image stored in the database with Java

开发者 https://www.devze.com 2023-01-03 04:16 出处:网络
I have a problem when I wa开发者_如何学Cnt to display an image stored in the database with OLE using JAVA.If your image data is stored in your database as some blob, you would probably want to use an

I have a problem when I wa开发者_如何学Cnt to display an image stored in the database with OLE using JAVA.


If your image data is stored in your database as some blob, you would probably want to use an ImageIcon and construct it using the byte[] imageData constructor:

ImageIcon(byte[] imageData)

Creates an ImageIcon from an array of bytes which were read from an image file containing a supported image format, such as GIF, JPEG, or (as of 1.3) PNG.


Trying to do a good guess, I would say you have to read it from the database using a blob, something like:

PreparedStatement pstmt = connection.prepareStatement("SELECT imageblob FROM YOUR_TABLE where id = ?");
pstmt.setInt( 1, imageId );
ResultSet rs = pstmt.executeQuery();
byte[]data = null;
while( rs.next() ) {
    data = rs.getBlob(1).getBytes();
}

And then displaying it with a servlet by writing the data to the output stream.

....

response.setContentType("image/jpg");
response.getOutputStream().write(data,0,data.length );
response.getOutputStream().flush();        

But this is just a guess, because your question lack of essential information.

Here's are some links related to Blob and Servlets

http://www.java2s.com/Code/Java/Database-SQL-JDBC/BlobJDBCdealswithBinaryData.htm

http://java.sun.com/j2se/1.5.0/docs/guide/jdbc/blob.html

http://www.roseindia.net/servlets/retreiveimage.shtml

I hope this helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号