0

I was trying to display image stored in my oracle database in JSP page. Image is stored as BLOB data.

Here is my code:

<%@page import="utils.MyUtils"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<% 

 Connection con = MyUtils.getStoredConnection(request);

 String strSQL = "SELECT IMAGE " 
 + "FROM CUSTOMER " 
 + "WHERE CUST_ID= 113"; 
 Statement stmt = con.createStatement(); 
 ResultSet rs = stmt.executeQuery(strSQL); 
 rs.next(); 

 response.setHeader("expires", "0"); 
 response.setContentType("jpeg"); 

 out.clear(); 
 OutputStream os = response.getOutputStream(); 
 os.write(rs.getBytes("IMAGE")); 
 out.flush(); 

%> 

Here is MyUtils class:

package utils;

import beans.CreateAcc;
import java.sql.Connection;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.UserAccount;

public class MyUtils {

    public static final String ATT_NAME_CONNECTION = "ATTRIBUTE_FOR_CONNECTION";

    private static final String ATT_NAME_USER_NAME = "ATTRIBUTE_FOR_STORE_USER_NAME_IN_COOKIE";

    // Store Connection in request attribute.
    // (Information stored only exist during requests)
    public static void storeConnection(ServletRequest request, Connection conn) {
        request.setAttribute(ATT_NAME_CONNECTION, conn);
    }

    // Get the Connection object has been stored in attribute of the request.
    public static Connection getStoredConnection(ServletRequest request) {
        Connection conn = (Connection) request.getAttribute(ATT_NAME_CONNECTION);
        return conn;
    }

    // Store user info in Session.
    public static void storeLoginedUser(HttpSession session, UserAccount loginedUser) {
        // On the JSP can access via ${loginedUser}
        session.setAttribute("loginedUser", loginedUser);
    }

    // Get the user information stored in the session.
    public static UserAccount getLoginedUser(HttpSession session) {
        UserAccount loginedUser = (UserAccount) session.getAttribute("loginedUser");
        return loginedUser;
    }
}

But when I run my code it only displays the URL path of the image stored in my PC instead of the image.

What should I do?

ric
  • 627
  • 1
  • 12
  • 23
Leolime
  • 197
  • 1
  • 1
  • 11

1 Answers1

-1

Get the byte stream from blob object. Convert the byte stream in to base 64 encoded string. Then use that base 64 encoded string in img tag. You can use base 64 encode string html image tag. And browser will render image.

You will get lot of posts on stack overflow for following points

  • How to convert blob data to by stream/array
  • How to convert byte stream to Base 64 encoded string
  • How to use base 64 encoded string in HTML image tag.
Rohit
  • 146
  • 1
  • 5
  • Don't do this. This approach is very inefficient as this doesn't give the webbrowser any opportunity to cache the image. This should at most be used for "preview" feature of uploaded images and certainly not for persistent images. – BalusC Dec 16 '17 at 21:22
  • @BalusC I do understand that base 64 encoded images are not cached. But since the question was for retrieving a single image,(based on the query that he provided) I provided this answer. If the question was something for an slideshow or something like that i would have not posted this answer. – Rohit Dec 17 '17 at 04:33