在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查看效果。

posted on 2008-09-19 14:51 鲁胜迪 阅读(1805) 评论(0)  编辑  收藏 所属分类: 一点点


只有注册用户登录后才能发表评论。


网站导航:
 
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(4)

随笔分类

随笔档案

文章分类

新闻分类

搜索

最新评论

阅读排行榜

评论排行榜