java-mysql(3) 读写image

在mysql里面用来存储图片有一个特殊的数据对象叫做 Blob(Binary Large Object).

数据库里面插入一张图片:

第一步:需要为图片创建一个文件对象

File img = new File(proppath);
FileInputStream fileInputStream = null;
try {
       fileInputStream = new FileInputStream(img);
  } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
         e1.printStackTrace();
   }

第二部:创建二进制的数据流

preparedStatement.setBinaryStream(1, fileInputStream,
                    (int) img.length());
 preparedStatement.executeUpdate();

调用的是prepared statement的setBinaryStream对象:几个参数分别是需要绑定的参数索引位置,一个文件输入流,图片的字节数

 1 package core;
 2 
 3 import java.io.File;
 4 import java.io.FileInputStream;
 5 import java.io.FileNotFoundException;
 6 import java.sql.Connection;
 7 import java.sql.DriverManager;
 8 import java.sql.PreparedStatement;
 9 import java.sql.SQLException;
10 import java.sql.Statement;
11 import java.sql.ResultSet;
12 
13 public class MethodReferencesTest {
14 
15     public static void main(String[] args) throws CloneNotSupportedException {
16         // TODO Auto-generated method stub
17 
18         Connection connection = null;
19         Statement statement = null;
20         ResultSet resultSet = null;
21         PreparedStatement preparedStatement = null;
22         String proppath = System.getProperty("user.dir")
23                 + "/src/core/c9c15f0edd07ee240f3af06de6775888.jpg";
24         
25         //创建image object
26         File img = new File(proppath);
27         FileInputStream fileInputStream = null;
28         try {
29             fileInputStream = new FileInputStream(img);
30         } catch (FileNotFoundException e1) {
31             // TODO Auto-generated catch block
32             e1.printStackTrace();
33         }
34         String sqlurl = "jdbc:mysql://172.20.23.75:3306/testdb";
35         String sqluser = "root";
36         String sqlpassword = "123456";
37         String sql = "INSERT INTO Images(Data) VALUES(?)";
38         
39         try {
40             connection = DriverManager.getConnection(sqlurl, sqluser,
41                     sqlpassword);
42             
43             preparedStatement = connection.prepareStatement(sql);
44             preparedStatement.setBinaryStream(1, fileInputStream,
45                     (int) img.length());
46             preparedStatement.executeUpdate();
47 
48         } catch (SQLException e) {
49             // TODO Auto-generated catch block
50             e.printStackTrace();
51         } finally {
52             try {
53                 if (resultSet != null) {
54                     resultSet.close();
55                 }
56                 if (statement != null) {
57                     statement.close();
58                 }
59                 if (preparedStatement != null) {
60                     preparedStatement.close();
61                 }
62                 if (connection != null) {
63                     connection.close();
64                 }
65             } catch (SQLException e) {
66                 e.printStackTrace();
67             }
68         }
69 
70     }
71 
72 }
点我看完整代码

 从数据库里面读取图片

第一步:创建一个输出流对象

//创建image object
File img = new File(proppath);
FileOutputStream fileputStream = null;
try {
    fileputStream = new FileOutputStream(img);
} catch (FileNotFoundException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

第二步:调用getblob()获取图片数据

 1 connection = DriverManager.getConnection(sqlurl, sqluser,
 2                     sqlpassword);
 3 
 4             preparedStatement = connection.prepareStatement(sql);
 5             resultSet = preparedStatement.executeQuery();
 6             if (resultSet.next()) {
 7                 // 获取图片数据
 8                 Blob blob = resultSet.getBlob("Data");
 9                 // 获取字节数
10                 int len = (int) blob.length();
11                 byte[] buf = blob.getBytes(1, len);
12                 try {
13                     fileputStream.write(buf, 0, len);
14                 } catch (IOException e) {
15                     // TODO Auto-generated catch block
16                     e.printStackTrace();
17                 }
18 
19             }
原文地址:https://www.cnblogs.com/xiamuyouren/p/3283259.html