image不会从mysql数据库加载到我的jsp页面中

nhjlsmyf  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(202)

我在数据库中创建了表,从该数据库加载图片时遇到问题。我用于img的数据类型是blob,我与数据库有很好的连接,所有其他数据都显示在我的jsp页面上,但图片不会加载。img的数据类型为.jpg,图片尺寸为250x250,大小约为14.2kb

CREATE TABLE `akcija` (
  `idakcija` int(11) NOT NULL AUTO_INCREMENT,
  `naziv` varchar(45) NOT NULL,
  `cena` varchar(45) DEFAULT NULL,
  `img` blob,
  PRIMARY KEY (`idakcija`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1

我通过以下插入查询加载数据:

INSERT INTO `games1`.`akcija` (`naziv`, `cena`,`img`)
 VALUES ('TEST','3500', 'C:\\Users\\mlade\\OneDrive\\Desktop\\New Folder (2)\\WebApplication1\\web\\gallery\\ufc4.jpg');

I also tried by right click on field for img and selecting Load Value from File, and then selecting img that I want. And third option that I tried is right click -> open value in editor -> Load image -> Apply -> and Apply on changes

这是我的jsp页面

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.Connection"%>
<%@include  file="connection.jsp"%>
<!DOCTYPE html>
<html>
    <head>
        <%@include file="header.jsp" %>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <div class="container">
            <img class="img-fluid "  src="https://gifcdn.com/1h64r34cpd6coj0dhl.gif" alt="mailtimers.com">
        </div>

        <table class="table table-sm">
            <thead>
                <tr>
                    <th scope="col">Ime igrice</th>
                    <th scope="col">Cena</th>
                    <th scope="col">Slika</th>
                </tr>
            </thead>
            <%                String id = request.getParameter("id");
                Blob img = null;
                Connection connection = null;
                Statement statement = null;
                ResultSet resultSet = null;
                try {
                    connection = DriverManager.getConnection(connectionUrl + database, userid, password);
                    statement = connection.createStatement();
                    String sql = "select * from akcija";
                    resultSet = statement.executeQuery(sql);
                    int i = 0;
                    while (resultSet.next()) {
            %>
            <tbody>
                <tr>
                    <td><%=resultSet.getString("naziv")%></td>
                    <td><%=resultSet.getString("Cena")%></td>
                    <td><img src="<%=resultSet.getBlob("img")%>" width="250" height="250"/></td>
                </tr>
            </tbody>
            <%
                        i++;
                    }
                } catch (Exception e) {
                    out.println("Problem");
                    return;
                } finally {
                    try {
                        resultSet.close();
                        statement.close();
                        connection.close();
                    }
                    catch (SQLException e) {
                     e.printStackTrace();
                    }
                }
            %>
        </table>
    </body>
</html>

来自naziv和cena列的数据显示在jsp页面上,但图像不会加载。请参见图片
图片未加载


这是我的项目结构截图。还有我用来在数据库中插入数据的数据库和查询的截图

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题