从数据库中读取blob字段并生成图片文件

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-23 07:30   11   0
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.lineteam.hibernate.DAO.custom.Db" %>

<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Blob" %>
<%@ page import="java.io.*" %>
<%
String pPath = request.getRealPath("/");
String sPath=pPath+"news//";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>从数据库中读取图片并生成对应的文件</title>

<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->

</head>

<body>
<%
//获取生成图片的物理路径
Connection con=Db.getConn();
Statement st=con.createStatement();
String sql="select * from news.picnews";
ResultSet rs=st.executeQuery(sql);
FileOutputStream outSTr = null;
BufferedOutputStream Buff=null;
while(rs.next())
{
try{
Blob blob = rs.getBlob("photo");

outSTr = new FileOutputStream(new File(sPath+"//"+rs.getString("path")));
Buff=new BufferedOutputStream(outSTr);
Buff.write( blob.getBytes(1, (int) blob.length()));
Buff.flush();
Buff.close();
}catch(Exception e)
{
System.out.println(e.getMessage());

}

}
%>
<br>
</body>
</html>

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP