如何使用jsp从mysql中检索多个blob图像

时间:2013-10-17 07:29:35

标签: java mysql image jsp blob

我正在尝试使用java从mysql数据库中检索一些blob图像。现在,问题是我一次只能获得一个图像,其他图像不会显示。

下面是我的jsp代码,我正在这样做(仅用于演示目的):

   <?xml version="1.0" encoding="UTF-8" ?>
   <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"   %>
   <%@ page language="java" %>
    <%@ page import="java.sql.*" %>
    <%@ page import="java.io.*" %> 
     <%@ page import="java.util.*"%>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       <html xmlns="http://www.w3.org/1999/xhtml">
       <head>
    <title>MindDotEditor posted Data</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta name="robots" content="noindex, nofollow" />
    <link href="../sample.css" rel="stylesheet" type="text/css" />
    <link rel="shortcut icon" href="../fckeditor.gif" type="image/x-icon" />
     </head>
     <body>

       <%
String url = "jdbc:mysql://localhost:3306/grandsho_register";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;


try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection(url,"root","root");
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT image FROM user ");
    int i = 1;
    if(rs.next()) {
        Blob len1 = rs.getBlob("image");
        int len = (int)len1.length();
        byte[] b = new byte[len];
        InputStream readImg = rs.getBinaryStream(1);
        int index = readImg.read(b, 0, len);
        System.out.println("index" +index);
        stmt.close();
        response.reset();
        response.setContentType("image/jpg");
        response.getOutputStream().write(b,0,len);
        response.getOutputStream().flush();
    }
} catch(Exception ex) {
    out.println(ex);
} finally {
    rs.close();
    stmt.close();
    con.close();
}
       %>

    <br>
    <center><input type="button" value="Print" onclick="window.print();return false;"   />        </center>
    </body>
  </html>    

有谁能建议如何在jsp页面上显示多个图像?

2 个答案:

答案 0 :(得分:0)

而不是if(rs.next()) {},您必须使用while(rs.next()){}

答案 1 :(得分:-1)

大家好,如果您提出所有问题,我会给您解决方案:来自仇恨编码

1.首先想知道通过查询检索多个图像(例如从照片中选择图像)是不可能的,你一次只能检索一个图像,但我们可以通过一些不同的方式检索多个图像。 / p>

[* - &GT;更重要的是,我希望你们已经将图像上传到数据库这里我将只显示检索多个图像,如果你想要编码图像上传然后把你的邮件ID放在这里]

2.对于图像操作首先,您需要将这些jar文件添加到项目Libaries

---&GT;公地文件上传-1.3.jar  http://www.java2s.com/Code/Jar/c/Downloadcommonsfileupload13jar.htm

---&GT;公地IO-2.4.jar  http://www.java2s.com/Code/Jar/c/Downloadcommonsio24jar.htm

3.使用sql查询创建表(我正在使用MYSQL)

create table upload_image(id int NOT NULL AUTO_INCREMENT主键,bImage Blob);

4.一次导入了jar并创建了表时间来做一些编码的坏事,添加代码

第一个HTML代码: &LT; ----- ------的index.jsp&GT;

`

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Reterving multiple images</title>
</head>

<body>
    <img src="veiw.jsp?id=1">
     <img src="veiw.jsp?id=2">
</body>

</html>

`

&LT; ----- ------ veiw.jsp&GT;

[<%@ page import="java.sql.*" %>
<%@ page import="org.apache.commons.fileupload.*"%>
<%@ page import="org.apache.commons.io.output.*"%>
<%@ page import="org.apache.commons.fileupload.servlet.*"%>
<%@ page import="org.apache.commons.fileupload.disk.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%
String url="jdbc:mysql://localhost:3306/testzeroDateTimeBehavior=convertToNull";
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
String j=request.getParameter("id");
int i=Integer.parseInt(j);
try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    con = DriverManager.getConnection(url,"root","");
    stmt = con.createStatement();
    rs = stmt.executeQuery("SELECT bImage FROM upload_image where id="+i);
      OutputStream o = response.getOutputStream();
    if(rs.next())
    {
             Blob bl = rs.getBlob(1);
             byte\[\] pict = bl.getBytes(1,(int)bl.length());
             response.setContentType("image/jpg");
             o.write(pict);
             o.flush();
             o.close();


    }

}
catch(Exception ex)
{
    out.println(ex);
} 
finally 
{
    rs.close();
    stmt.close();
    con.close();
}

%>[image shows the added jar file then start processing from step 3 ][3]