【问题标题】:How to print blob type directly to jsp如何将blob类型直接打印到jsp
【发布时间】:2013-10-10 14:16:52
【问题描述】:

我正在用 jquerymobile + jsp 制作一个应用程序

结构不像普通的servlet-jsp MVC。

它直接获取数据并像这样打印,

<%@ page contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>

<%! Connection con=null; %>
<%! Statement st= null; %>



<% 
try {
ResultSet rs= null; 
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/first_app";
String user = "root";
String password = "test"; 
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url, user, password);
try {
    String sql = "SELECT * FROM lunch_menu";
        PreparedStatement ps = con.prepareStatement(sql);
        rs = ps.executeQuery();

%>


<div data-role="page">
    <div data-role="header">
        <h1>MENU PAGE</h1>
    <a href="#" data-rel="back" data-icon="arrow-l">Back</a>
</div>

<div data-role="content">
    <ul data-role="listview">
        <!--
        <li><img src="/FirstApp/assets/img/1.jpg" />2013-10-08 똥맛카레</li>
        <li><img src="/FirstApp/assets/img/2.jpg" />2013-10-08 카레맛똥</li>
        -->
        <%

        while(rs.next()){
        Blob image = null;
        image = rs.getBlob("lunch_image");
        out.print("<li>");
            //prints com.mysql.jdbc.Blob@2d58497c
            out.print(rs.getBlob("lunch_image"));
            //prints [B@686fdca5
            //out.print(image.getBytes(1,(int)image.length()));


            out.print("</li>");
        }
        } catch (SQLException e) {
        e.printStackTrace();
        }
        }catch(ClassNotFoundException ce){out.println(ce);}
        %>


        </ul>
    </div>


</div>

就像我评论的那样,

我已经检查了 blob 类型数据是否存在,但我不知道如何显示数据。如果这是 jsp-servlet,我知道我只需要这样做。

InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}

但我不明白这种情况。

【问题讨论】:

    标签: jsp blob


    【解决方案1】:

    您不能将 blob 类型直接打印到 html (jsp) 代码中。当您尝试这样做时,这将永远不会起作用。

    我的建议是创建一个输出图像的 servlet。请参阅此处如何执行此操作 How to retrieve image from database and display in JSP via Servlet? 。然后在您的代码中调用 servlet 如下:

    <img src='path_to_servlet/ServletName?imageId=xxx'>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 2014-06-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多