我想从sql中检索多个图像,并在指定位置的jsp页面中打印它

时间:2014-05-04 14:48:44

标签: mysql sql jsp

<%@ page import="java.sql.*"%>
<%@ page import="java.io.*"%>
<html>
<% 
byte[] imgData = null;
%>
<%
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con =  
DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select photo from employee ");

while (rs.next()) {
Blob image = rs.getBlob(1);
imgData = image.getBytes(1,(int)image.length());
 }
// display the image
response.setContentType("image/png");
OutputStream o = response.getOutputStream();
%>

<img="<%o.write(imgData);%>" width="10" height="20">
<%o.flush();
o.close();

out.println("hi");
rs.close();
stmt.close();
con.close();
} catch (Exception e) {
out.println("Unable To Display image");
out.println("Image Display Error=" + e.getMessage());
return;
} 

%>

此代码将图像打印为图像的大小。我无法调整图像大小。我尝试了很多方法。

如果我能从任何其他表格中提取图像,请提供帮助

2 个答案:

答案 0 :(得分:0)

好吧,我认为您应该创建一个单独的jsppage,其唯一目的是输出图像。然后,在此页面上,在while循环中,您需要设置标记的图像URL,如下所示。我无法保证这段代码能够正常工作(我不习惯JSP),但一般的方法应该有效

JSP页面生成图像(调用此GetImage.jsp):

    <%@ Page import="java.sql.*" %>
    <%@ Page import="java.io.*" %>

    <html>
    <%
    byte[] imgData = null;
    try 
    {
        Class.forName("com.mysql.jdbc.Driver");
        Connection con =  
        DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select photo from employee where employee_id=" + request.getParameter("empId"));
        while (rs.next()) 
        {
            Blob image = rs.getBlob(1);
            imgData = image.getBytes(1,(int)image.length());
        }
        response.setContentType("image/png");
        OutputStream o = response.getOutputStream();
        o.flush();
        o.close();
        rs.close();
        stmt.close();
        con.close();
    } 
    catch (Exception e) 
    {
        out.println("Unable To Display image");
        out.println("Image Display Error=" + e.getMessage());
        return;
    } 
    %>
    </html>

用于列出员工图像的JSP页面(调用此EmployeeList.jsp):

    <%@ page import="java.sql.*"%>
    <%@ page import="java.io.*"%>
    <html>
    <%
    try 
    {
        String EmpFirstName;
        String EmpSurname;
        String EmpId;
        Class.forName("com.mysql.jdbc.Driver");
        Connection con =  DriverManager.getConnection("jdbc:mysql://localhost/try","root","root");
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("select employee_id,first_name,surname from employee");
        while (rs.next()) 
        {
            EmpFirstName = rs.getString("first_name");
            EmpSurname = rs.getString("surname");
            EmpId = rs.getString("EmpId");
            <DIV><%=EmpFirstName5> <%=Surname%> </DIV>
            <img src="http://localhost/GetImage.jsp?empId=<%=EmpId%>" />
        }
        rs.close();
        stmt.close();
        con.close();
    } 
    catch (Exception e) 
    {
        out.println(e.Message);
        return;
    } 
    %>
    </html>

因此,在新页面中,您将接受表示员工ID的参数(通过查询字符串或其他方式)。然后,在该页面上,您将向DB查询该特定员工ID的图像,并按照您的操作进行操作 - 不使用while循环。

上面代码的问题在于它将整个页面的输出流设置为图像。

答案 1 :(得分:0)

public function cari()
{
  $id= $this->uri->segment(3);
  $this->model_name->search_by_id($id);
}