我试图在JSP页面中显示存储在我的oracle数据库中的图像。图像存储为BLOB
数据。
这是我的代码:
<%@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();
%>
这是MyUtils
类:
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;
}
}
但是当我运行我的代码时,它只显示存储在我的电脑而不是图像中的图像的URL路径。
我该怎么办?
答案 0 :(得分:-1)
从blob对象获取字节流。将字节流转换为base 64编码的字符串。然后在img标记中使用base 64编码的字符串。您可以使用base 64编码字符串html图像标记。浏览器将呈现图像。
对于以下几点,您将获得大量关于堆栈溢出的帖子