<%
@page contentType
=
"
text/html
"
pageEncoding
=
"
UTF-8
"
%>
<% @page import = " cn.java.* " %>
<% @page import = " java.sql.* " %>
<% @page import = " java.text.* " %>
<% @page import = " java.io.* " %>
<% @page import = " java.util.* " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=UTF-8 " >
< title > JSP Page </ title >
</ head >
< body >
<%
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try ... {
String sql = "select binfile from bindata where id=5";
conn = DBUtil.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())...{
ServletOutputStream sout=response.getOutputStream();
InputStream in=rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];//获得二进制输入流
while(in.read(b)!=-1)...{
sout.write(b);//输出字符流
}
sout.flush();
sout.close();//关闭sout输出流
}
} catch (SQLException ex) ... {
out.println(ex.getMessage());
}
finally ... {
try ...{
DBUtil.close(rs, stmt, conn);
} catch (Exception ex) ...{
out.println(ex.getMessage());
}
}
%>
</ body >
</ html >
<% @page import = " cn.java.* " %>
<% @page import = " java.sql.* " %>
<% @page import = " java.text.* " %>
<% @page import = " java.io.* " %>
<% @page import = " java.util.* " %>
<! DOCTYPE HTML PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN "
" http://www.w3.org/TR/html4/loose.dtd " >
< html >
< head >
< meta http - equiv = " Content-Type " content = " text/html; charset=UTF-8 " >
< title > JSP Page </ title >
</ head >
< body >
<%
Connection conn = null ;
Statement stmt = null ;
ResultSet rs = null ;
try ... {
String sql = "select binfile from bindata where id=5";
conn = DBUtil.getConnection();
stmt=conn.createStatement();
rs=stmt.executeQuery(sql);
while(rs.next())...{
ServletOutputStream sout=response.getOutputStream();
InputStream in=rs.getBinaryStream(1);
byte b[] = new byte[0x7a120];//获得二进制输入流
while(in.read(b)!=-1)...{
sout.write(b);//输出字符流
}
sout.flush();
sout.close();//关闭sout输出流
}
} catch (SQLException ex) ... {
out.println(ex.getMessage());
}
finally ... {
try ...{
DBUtil.close(rs, stmt, conn);
} catch (Exception ex) ...{
out.println(ex.getMessage());
}
}
%>
</ body >
</ html >