java—从数据库检索图像并显示在另一个jsp页面中

tvmytwxo  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(472)

这个问题在这里已经有答案了

如何在jsp页面中从数据库检索和显示图像(5个答案)
去年关门了。
我试图从数据库中检索多个图像,并在另一个jsp页面中显示这些图像。首先,我尝试在特定的jsp页面中显示单个图像,我检索到了该图像,但是该图像显示为文件类型,我希望在特定的jsp页面中显示该图像。我正在使用mysql数据库。
我的servlet代码:

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代码:

<%@ 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>
8ftvxx2r

8ftvxx2r1#

尝试使用 Base64 编码,
使用apachecommons编解码器进行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<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);

在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>

相关问题