将文件从数据库(MySQL)中进行读取

package com.play;

import java.io.FileOutputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class DatabaseToFile {

 public static void main(String[] args) throws Exception {
  Class.forName("com.mysql.jdbc.Driver");
  Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");
  String sql = "select * from t_user where name=?";
  PreparedStatement ps = con.prepareStatement(sql);
  ps.setString(1, "wangba");
  ResultSet rs  = ps.executeQuery();
  while(rs.next()){
   Blob blob = rs.getBlob("image");
   OutputStream os = new FileOutputStream("D:\a.jpg");
   os.write(blob.getBytes(1, (int)blob.length()));
   os.flush();
   os.close();
  }
  rs.close();
  ps.close();
  con.close();
 }

}

原文地址:https://www.cnblogs.com/javawebsoa/p/3202871.html