如何使用<img>标签显示未存储在文件中的图像?(请参阅说明)



我想在JSP中使用标记显示来自数据库的图像。如何在标签中使用blob ?(或者这有可能吗?)如果没有,是否有一种方法可以将blob用于一些有意义的目的?)

这是我从servlet中获得的代码:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    List<Blob> blobList = new ArrayList<Blob>();
    String id = request.getParameter("id");
    Connection con = null;
    Statement stmt = null;
    ResultSet rs = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","username","password");
        stmt = con.createStatement();
        rs = stmt.executeQuery("SELECT ICON FROM IMAGES WHERE IMG_NAME=" + id);
        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();
        for (int i = 1; i < columnCount + 1; i++) {
            blobList.add(rs.getBlob(i));
        }

    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

您可以对图像进行base64编码,并在"数据URI" (Google是您的朋友)中使用结果。

相关内容

最新更新