当前位置:Gxlcms >
数据库问题 >
JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>
JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>
时间:2021-07-01 10:21:17
帮助过:36人阅读
1
public static void writeImg(OutputStream os) {
2 Connection con;
3 try {
4 con =
ConnectionFactory.getConnection();
5 Statement stmt =
con.createStatement();
6 ResultSet rs = stmt.executeQuery("select pic from blob_table where b_id=1"
);
7 byte[] b =
new byte[1024
];
8 if (rs.next()) {
9 Blob blob = rs.getBlob(1
);
10 InputStream is =
blob.getBinaryStream();
11 int i = 0
;
12 while ((i = is.read(b)) != -1
) {
13 os.write(b, 0
, i);
14 }
15 os.close();
16 is.close();
17 }
18 rs.close();
19 stmt.close();
20 con.close();
21 }
catch (Exception e) {
22 e.printStackTrace();
23 }
24 }
然后做一个servlet,在doGet方法里调用writeImg方法
1 public void doGet(HttpServletRequest request, HttpServletResponse response)
2 throws ServletException, IOException {
3
4 DBOperator.writeImg(response.getOutputStream());
5
6 }
jsp代码:
<img src="servlet/newServlet" >
转载:作者longdick http://longdick.iteye.com
JSP读取Oracle数据库里的图片Blob字段并显示在页面上<转>
标签:creat state name ati style test exe set nfa