如何使用JSTL在jsp页表中显示多个MySQL db blob图像

时间:2016-04-20 10:15:23

标签: java jsp jstl

我的程序中有一个部分执行http GET请求,该部分显示一个包含MySQL数据库行的表。虽然每个包含文本的单元格显示正常,但表格的图像单元格显示对象参考,尽管多次搜索和多次尝试,但仍无法使其正常工作。

我的图像存储为数据库中的Blob,我的行项目存储在对象列表中,如下面的“DisplayChefRecipes.java”所示。码。我在JSP页面上使用EL访问它。

我不想在我的JSP页面中使用Scriplets,原因是Stackoverflow中其他地方所述。

Bean类(仅与图像相关的部分)

def create_zip(path,zipf):
    #path is the directory address (i.e. /Home/Documents/Test_files)
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith(".json"):
                print file
                zipf.write(os.path.join(root, file))
                #zipf.write(file)

DisplayChefRecipes

private InputStream image;
public InputStream getImage() {
    return image;
}
public void setImage(InputStream sImage) {
    this.image = sImage;
}

DisplayChefRecipes.jsp(表格部分)

 public class DisplayChefRecipes extends HttpServlet {

 private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
 */
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    Chef chef = new Chef();
    int chefIdString = Integer.parseInt(request.getParameter("chef_Id"));
    chef.setId(chefIdString);
    List<Object> resultSet = new ArrayList<Object>();

    // Make a connection to the database
    String url = "jdbc:mysql://localhost/traineechefdb";
    String driver = "com.mysql.jdbc.Driver";
    String user = "root";
    String password = null;

    try {
         Class.forName(driver).newInstance();
         Connection conn = (Connection) DriverManager.getConnection(url, user, password);
         System.out.println("Connection Established");      
         Class.forName("com.mysql.jdbc.Driver");

         Statement stmt = (Statement) conn.createStatement();
         String sql = "SELECT R.NAME, R.DESCRIPTION, R.PREP_TIME, R.INGREDIENTS, R.DIRECTIONS, FO.ORIGIN, FT.TYPE_NAME, R.IMAGE " +
                      "FROM RECIPE AS R " +
                        "INNER JOIN FOOD_ORIGIN AS FO " + 
                            "ON R.FOOD_ORIGIN_ID = FO.FOOD_ORIGIN_ID " +
                        "INNER JOIN FOOD_TYPE AS FT " +
                            "ON R.FOOD_TYPE_ID = FT.FOOD_TYPE_ID " +
                      "WHERE R.CHEF_ID = '" + chef.getId() + "' ";

         ResultSet rs = stmt.executeQuery(sql);

         InputStream sImage = null;

         while(rs.next()){

             Recipe recipe = new Recipe();

             recipe.setRecipeName(rs.getString("R.NAME"));
             recipe.setDescription(rs.getString("R.DESCRIPTION"));
             recipe.setPrepTime(rs.getDouble("R.PREP_TIME"));
             recipe.setIngredients(rs.getString("R.INGREDIENTS"));
             recipe.setDirections(rs.getString("R.DIRECTIONS"));
             recipe.setFoodOrigin(rs.getString("FO.ORIGIN"));
             recipe.setFoodType(rs.getString("FT.TYPE_NAME"));
             sImage = rs.getBinaryStream("R.IMAGE");
             recipe.setImage(sImage);

             resultSet.add(recipe);
             recipe.equals(null);
         }
        request.setAttribute("resultSet", resultSet);

        sImage.close();

        rs.close();
        conn.close();

        }catch(SQLException){
            e.printStackTrace();
        }
           request.getRequestDispatcher("jsp/DisplayChefRecipes.jsp").forward(request, response);
    }

1 个答案:

答案 0 :(得分:1)

一种方法是创建一个servlet,它将返回具有指定id的图像,然后只使用常规<img src="/imgserv?${row.imageid}"/>(或沿着这些行)。

最近的另一种可能性是使用Embedding Base64 Images中描述的嵌入式base64编码图像数据。这将要求您将字节数据预处理到Base64,但它将避免必须创建一个单独的servlet来获取图像数据(而不是它有什么问题)。当然,如果您有许多大图像,嵌入式base64的额外开销可能会成为一个问题。