`

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

阅读更多
<%@ 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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics