jsp_获取图片并显示

<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>

<%@ page import="com.suntendy.framework.db.DBPool"%>

<%@ page import="java.sql.*" %>

<%@ page import="java.io.*" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK">

<title>Insert title here</title>

</head>

<body>

<%

    Connection con=null;

    Statement stmt=null;

try

{

    con=DBPool.getConn();

    stmt=con.createStatement();

}

catch(SQLException ex)

{

  out.println(ex);

}

//取图片显示

try

{

    response.reset();

    response.setContentType("image/jpeg");

    OutputStream op=response.getOutputStream();

    String sql="select * from zp where id='09000142'";

    ResultSet rs=stmt.executeQuery(sql);

    while(rs.next())

    {

        InputStream is=null;

        is=rs.getBinaryStream("zp");

        int total=0;

        int nread=0;

        byte[] buffer=new byte[4*1024];

        while((nread=is.read(buffer))!=-1)

        {

            op.write(buffer,0,nread);

            total+=nread;

        }

        op.flush();

        op.close();

    }

    rs.close();

}

catch(Exception e)

{

    out.println(e.toString());

}

finally

{

    if(stmt!=null)

    {

        stmt.close();

    }

    if(con!=null)

    {

        DBPool.close(con);

    }

}

%>

</body>

</html>

你可能感兴趣的:(jsp)