MMMMS
MMMMS

Reputation: 2201

how to get blob images in img tag in jsp

I could not find solution for get blob images from Db and display in img tag using jsp.What i tried as below code,

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border=1>
<%
try
{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/raptor1_5","root","");
Statement st=connection.createStatement();
ResultSet rst = st.executeQuery("select * from contacts");
while(rst.next())
{
    Blob image = rst.getBlob("Images");
    byte[ ] imgData = null ; 
    imgData = image.getBytes(1,(int)image.length());
    String answer = rst.getString("Answers");
    //response.setContentType("image/gif");

    //OutputStream o = response.getOutputStream();


%>
    <tr>
    <td><img src="<%=imgData %>" alt="images Here" width="130px" height="90px"></td>
    <td><%=answer %></td>
    </tr>
<%} 
}
catch(Exception e)
{
    e.printStackTrace();
}



%>
</table>
</body>
</html>

I tried many questions like retrieve blob file from DB in <img> but i dont understand how to display.So please someone tell me how to get that blob images in <img> tag dynamically using jsp.

I hope someone will help me out..!

Upvotes: 1

Views: 6275

Answers (1)

You can try with inline images with data url, more info here: http://www.websiteoptimization.com/speed/tweak/inline-images/

This to encode the data to base64.

String imgDataBase64=new String(Base64.getEncoder().encode(imgData));

And this to show the image in the web page

<img src="data:image/gif;base64,<%= imgDataBase64 %>" alt="images Here" width="130px" height="90px"/>

If you have problem with the base64, you can use this function from https://gist.github.com/EmilHernvall/953733

public static String encode(byte[] data)
    {
        char[] tbl = {
            'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
            'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
            'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
            'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/' };

        StringBuilder buffer = new StringBuilder();
        int pad = 0;
        for (int i = 0; i < data.length; i += 3) {

            int b = ((data[i] & 0xFF) << 16) & 0xFFFFFF;
            if (i + 1 < data.length) {
                b |= (data[i+1] & 0xFF) << 8;
            } else {
                pad++;
            }
            if (i + 2 < data.length) {
                b |= (data[i+2] & 0xFF);
            } else {
                pad++;
            }

            for (int j = 0; j < 4 - pad; j++) {
                int c = (b & 0xFC0000) >> 18;
                buffer.append(tbl[c]);
                b <<= 6;
            }
        }
        for (int j = 0; j < pad; j++) {
            buffer.append("=");
        }

        return buffer.toString();
    }

To use it, just

String imgDataBase64=encode(imgData));

Upvotes: 2

Related Questions