显示来自SQL Server的图像

时间:2018-06-24 14:49:12

标签: java sql-server jsp

我正在尝试从具有字段(IdFilm,海报,标题,国家/地区,性别)的数据库的电影表中打印电影列表。除张贴者字段外,所有字段均正确打印。

我不确定我应该将图像保存在数据库中的哪种数据类型(图像或varbinary(max))。我已将它们另存为varchar,并带有图像名称(例如,photo1.jpg),并且尝试以这种方式显示图像,但是它不起作用:

案例展示:

package entity;

import dao.dbmanager;
import java.sql.*;
import java.util.Vector;

public class show {
    private final String URL = "images/";

    private int id;
    private String photo;
    private String title;
    private String country;
    private String gender;

    public show(int id, String photo, String title, String country, String gender) {
        this.id = id;
        this.photo = photo;
        this.title = title;
        this.country = country;
        this.gender = gender;
    }

    public show() {
    }

    public static Vector ShowData() {
        Vector film = null;
        try {
            dbmanager db = new dbmanager();
            Connection con = db.Connect();
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("Select * from Films");
            film = new Vector();

            while(rs.next()) 
                film.add(new show(rs.getInt("idFilm"), rs.getString("poster"), rs.getString("title"), rs.getString("country"), rs.getString("gender")));
                return film;
        }
        catch (SQLException e){
            System.out.println("Error: " + e.getMessage());
        }
        return null;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getPhoto() {
        return photo;
    }

    public void setPhoto(String photoName) {
        this.photo = URL + photoName;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }
}

index.jsp:

<%@page import="java.util.Vector"%>
<%@page import="entity.show"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form name="f1" method="POST">
            <center>
        <table border="1">
            <tr align="center">
                <td>Id</td>
                <td>Poster</td>
                <td>Title</td>
                <td>Country</td>
                <td>Gender</td>
            </tr>
            <%
                show objfilm = new show();
                Vector film = new Vector();
                film = objfilm.ShowData();

                for(int i=0; i < film.size(); i++) {
                    objfilm = (show)film.get(i);
            %>
            <tr align="center">
                <td><%=objfilm.getId() %></td>
                <td><img src="<%=objfilm.getPhoto() %>"></td>
                <td><%=objfilm.getTitle() %></td>
                <td><%=objfilm.getCountry() %></td>
                <td><%=objfilm.getGender() %></td> 
            </tr>
            <%
                }
            %>
        </table>
            </center>
        </form>
    </body>
</html>

我想知道如何显示每条记录的图像(海报)。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

只需将图像存储在磁盘上(最好在Web服务器外部的文件夹中)。保存后将图像重命名为唯一名称(哈希),然后将此哈希(加上原始名称)存储为表格中的varchar类型。

要显示图像,只需参考映射到海报文件夹的逻辑路径。 在Tomcat中,它类似于

<Context path="/posters" docBase="C:/data/posters" debug="0" privileged="true"></Context>

现在您可以像这样显示帖子

<img src="/posters/<%= objfilm.hashName %>" alt="<%= objfilm.origName %>" />