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

该页面展示了一段Java代码,用于从数据库中读取Blob类型的图片数据,并将其保存为物理路径下的图片文件。通过建立数据库连接,执行SQL查询从news.picnews表中获取所有记录,然后将每条记录的photo字段(Blob类型)转化为二进制写入到文件系统中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<%@ 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值