在JSF页面中显示数据库的图片

在JSF页面中显示数据库的图片

list.jsp用于显示图片的JSF页面
 <h:graphicImage url="show.jsp"/>
  
show.jsp 用于获取图片信息的jsp页面
<% @ page contentType = " text/html; charset=gbk "   %>
<% @ page  import = " java.io.* " %>
<% @ page  import = " java.sql.*, javax.sql.* "   %>
<% @ page  import = " java.util.* " %>
<% @ page  import = " java.math.* " %>

<%
// String photo_no = request.getParameter("photo_no");

// mysql连接 
// Class.forName("com.mysql.jdbc.Driver").newInstance();
// String URL="jdbc:mysql: // localhost:3306/job?user=root&password=111111";
// Connection con = DriverManager.getConnection(URL);

// oracle连接 jdbc:oracle:thin:@192.168.1.33:1521:ora9i
String URL = " jdbc:oracle:thin:@192.168.1.33:1521:ora9i " ;
String user
= " wtcx " ;
String password
= " wtcx " ;
Connection con 
=  DriverManager.getConnection(URL,user,password);


try {
// 准备语句执行对象
Statement stmt = con.createStatement();

String sql 
= "select t.* from t_file t Where fileid Like '402881e41c460e0a011c460eccd50009' ";
ResultSet rs 
= stmt.executeQuery(sql);
if (rs.next()) {
Blob blob 
= rs.getBlob("content");
long size = blob.length();
//out.print(size);
byte[] bytes = blob.getBytes(1, (int)size);
response.setContentType(
"image/jpeg"); 
OutputStream outs 
= response.getOutputStream(); 
outs.write(bytes);
outs.flush();
rs.close(); 
}

else {
rs.close();
response.sendRedirect(
"./images/error.gif");
}

}

finally {
con.close();
}

%>
 list.jsp和show.jsp两个页面在同意目录中的,在完成以上两个页面后就可以进行测试了
在ie中输入 http://localhost:8089/...../list.faces查看效果。

你可能感兴趣的:(在JSF页面中显示数据库的图片)