<%@ page contentType="text/html;charset=gb2312"%> <%@ page import="java.sql.*" %> <%@ page import="java.util.*"%> <%@ page import="java.text.*"%> <%@ page import="java.io.*"%>
<% <%Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); //加载驱动程序类 Connection con=DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=upload_Image","sa","sa"); //建立数据库联机,其中upload_Image为数据库名,sa为连接数据库的帐号及密码。 Statement stmt=con.createStatement(); ResultSet rs=null; //建立ResultSet(结果集)对象 int id= Integer.parseInt(request.getParameter("id")); //获得所要显示图片的编号id,并转换为整型 String sql = "select image from picturenews WHERE id="+id+""; //要执行查询的SQL语句 rs=stmt.executeQuery(sql); while(rs.next()) { ServletOutputStream sout = response.getOutputStream(); //图片输出的输出流 InputStream in = rs.getBinaryStream(1); byte b[] = new byte[0x7a120]; for(int i = in.read(b); i != -1;) { sout.write(b); //将缓冲区的输入输出到页面 in.read(b); } sout.flush(); //输入完毕,清除缓冲 sout.close(); } %>