从数据库中检索图像并在另一个jsp页面中显示 [英] Retrieve Image from Database and display in another jsp page

查看:62
本文介绍了从数据库中检索图像并在另一个jsp页面中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数据库中检索多个图像并在另一个 JSP 页面中显示这些图像.首先,我尝试在特定的 JSP 页面中显示单个图像,我检索了但显示显示为文件类型,我想在特定的 JSP 页面中显示该图像.我正在使用 MySQL 数据库.

我的 servlet 代码:

import java.io.IOException;导入 java.sql.Connection;导入 java.sql.DriverManager;导入 java.sql.PreparedStatement;导入 java.sql.ResultSet;导入 java.sql.SQLException;导入 javax.servlet.RequestDispatcher;导入 javax.servlet.ServletException;导入 javax.servlet.annotation.WebServlet;导入 javax.servlet.http.HttpServlet;导入 javax.servlet.http.HttpServletRequest;导入 javax.servlet.http.HttpServletResponse;@WebServlet("/检索")公共类检索扩展 HttpServlet {静止的 {尝试 {Class.forName("com.mysql.jdbc.Driver");} catch (ClassNotFoundException e) {e.printStackTrace();}}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ 连接 con = null;PreparedStatement ps = null;结果集 rs = null;尝试 {con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas", "root", "root");ps = con.prepareStatement("从粗略中选择*");rs=ps.executeQuery();如果(rs.next()){byte[] content = rs.getBytes("image");response.setContentLength(content.length);response.getOutputStream().write(content);request.setAttribute("图片", 内容);RequestDispatcher rd=request.getRequestDispatcher("View.jsp");rd.forward(请求,响应);} 别的 {response.sendError(HttpServletResponse.SC_NOT_FOUND);//404.}} catch (SQLException e) {throw new ServletException("SQL/DB 级别出现问题.", e);}}}

jsp 代码:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"pageEncoding="ISO-8859-1"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><头><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>显示图片</title><身体><div>显示</div><div style="width:25%; height:25%"><%request.getAttribute("图片");%>

</html>

解决方案

尝试使用 Base64 编码,

使用 Apache Commons Codec 进行 Base64 编码.

byte[] content = rs.getBytes("image");String base64Encoded = new String(Base64.encodeBase64(content), "UTF-8");request.setAttribute("imageBt", base64Encoded);

从 JSP 中检索它

<img src="data:image/png;base64,${requestScope['imageBt']}"/>

对于多个图像,您可以尝试这样的操作,(我没有尝试过)

List图像 = 新的 ArrayList();如果(rs.next()){byte[] content = rs.getBytes("image");图像.添加(新字符串(Base64.encodeBase64(内容),UTF-8"));}request.setAttribute("imageBt", 图像);

在 JSP 中

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><c:forEach var="img" items="${imageBt}"><img src="data:image/png;base64, ${img}"/></c:forEach>

I am trying to retrieve multiple images from database and display those images in another JSP page. First, I tried single image to display in particular JSP page, I retrieved but the display is showing as file type, I want to display that image in particular JSP page. I am using MySQL database.

my servlet code:

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebServlet("/Retrieve")
public class Retrieve extends HttpServlet {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {   Connection con = null;
    PreparedStatement ps = null;
ResultSet rs = null;

        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/vikas", "root", "root");
            ps = con.prepareStatement("select * from rough");
            rs=ps.executeQuery();
                    if (rs.next()) {
                        byte[] content = rs.getBytes("image");
                        response.setContentLength(content.length);
                        response.getOutputStream().write(content);
                        request.setAttribute("image", content);
                        RequestDispatcher rd=request.getRequestDispatcher("View.jsp");  
                        rd.forward(request, response);  
                    } else {
                        response.sendError(HttpServletResponse.SC_NOT_FOUND); // 404.
                    }

            } catch (SQLException e) {
                throw new ServletException("Something failed at SQL/DB level.", e);
            }
        }


    }

jsp code:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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=ISO-8859-1">
<title>Display Image</title>
</head>
<body>
<div>THE DISPLAY</div>
<div style="width:25%; height:25%">
<%request.getAttribute("image"); %>
</div>

</body>
</html>

解决方案

Try using Base64 encoding,

Use Apache Commons Codec to do Base64 encodings.

byte[] content = rs.getBytes("image");
String base64Encoded = new String(Base64.encodeBase64(content), "UTF-8");
request.setAttribute("imageBt", base64Encoded);

Retrieve it from JSP

<img src="data:image/png;base64,${requestScope['imageBt']}"/>

For multiple images you could try something like this, (i didnt try this)

List<String> images = new ArrayList<>();
if (rs.next()) {
    byte[] content = rs.getBytes("image");
    images.add(new String(Base64.encodeBase64(content), "UTF-8"));
}
request.setAttribute("imageBt", images);

In JSP

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:forEach var="img" items="${imageBt}">
    <img src="data:image/png;base64, ${img}"/>
</c:forEach>

这篇关于从数据库中检索图像并在另一个jsp页面中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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