mysql将图片写入数据库

11 篇文章 0 订阅

  实现向MYSQL数据库中存储或提取图片文件

 
一些情况下,需要向数据库中存储一些2进制文件,比如图片文件等,这时候,向数据库存储数据不同于普通的字符串存储,我们需要对这个2进制文件使用JAVA处理2进制流的API进行处理,然后再进行存储。我们需要进行以下步骤来实现:

 
向数据库中存储文件的时候,一样使用标准SQL语句,如: insert into database (column1, column2,..) values(v1,v2,…);注意的是,要在建立存放2进制文件的TABLE时,存放的字段要使用BLOB类型,而不是普通的VARCHAR等。BLOB是专门存储2进制文件的类型,他还有大小之分,比如mediablob,logblob等,以存储大小不同的2进制文件,一般的图形文件使用mediablob足以了。


1 见以下代码实现向MYSQL中储存图片文件:
 1private final String insertquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
 2
 3
 4
 5public void doInsertStaffPic(String loginname,String source_URL) {
 6
 7
 8
 9              Connection conn = null;
10
11              PreparedStatement pre = null;
12
13
14
15              try {
16
17             // 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过JDBC直接连
18
19                     conn = DBProcess.getConnection();
20
21                    
22
23//从图片源中获得图片对象并写到缓存中
24
25                     Image image = new ImageIcon(source_URL).getImage();
26
27                     BufferedImage bImage = new BufferedImage(image.getWidth(null),
28
29                                   image.getHeight(null), BufferedImage.TYPE_INT_RGB);
30
31                     Graphics bg = bImage.getGraphics();
32
33                     bg.drawImage(image, 0, 0, null);
34
35                     bg.dispose();
36
37                    
38
39//将图片写入2进制的输出流 并放如到byte[] buf中
40
41                     ByteArrayOutputStream out = new ByteArrayOutputStream();
42
43                     ImageIO.write(bImage, "jpg", out);
44
45                     byte[] buf = out.toByteArray();
46
47                    
48
49            //获得这个输出流并将他设置到BLOB中
50
51                     ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
52
53                     pre = conn.prepareStatement(insertstaffpicquery);
54
55                     pre.setString(1, loginname);
56
57                     pre.setBinaryStream(2, inStream, inStream.available());
58
59                     // 执行写如数据
60
61pre.executeUpdate();
62
63                    
64
65
66
67              } catch (Exception exc) {
68
69                     exc.printStackTrace();
70
71              }
72
73
74
75              finally {
76
77                     try {
78
79                            pre.close();
80
81                            conn.close();
82
83
84
85                     } catch (SQLException e) {
86
87                            e.printStackTrace();
88
89                     }
90
91              }
92
93
94
95       }
96
97
2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:
  1private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
  2
  3
  4
  5// retrive the picture data from database and write it to the local disk
  6
  7       public void doGetAndShowStaffPic(String loginname, String dir) {
  8
  9
 10
 11        FileOutputStream output = null;
 12
 13        InputStream input = null;
 14
 15
 16
 17              Connection conn = null;
 18
 19              ResultSet rs = null;
 20
 21              PreparedStatement pre = null;
 22
 23
 24
 25              try {
 26
 27                     conn = DBProcess.getConnection();
 28
 29                     pre = conn.prepareStatement(writeoutquery);
 30
 31                     pre.setString(1, loginname);
 32
 33                     rs = pre.executeQuery();
 34
 35
 36
 37                     if (rs.next()) {
 38
 39                // 从数据库获得2进制文件数据
 40
 41                            Blob image = rs.getBlob("Binary_Photo");
 42
 43                            // setup the streams
 44
 45                            Input = image.getBinaryStream();
 46                  
 47
 48                            try {
 49
 50                    // 设置写出路径。
 51
 52                                   output = new FileOutputStream(dir);
 53
 54                            } catch (FileNotFoundException e1) {
 55
 56                                  
 57
 58                                   e1.printStackTrace();
 59
 60                            }
 61
 62                            // set read buffer size 注意不要设置的太小,要是太小,图片可能不完整
 63
 64                            byte[] rb = new byte[1024000];
 65
 66                            int ch = 0;
 67
 68                            // process blob
 69
 70
 71
 72                            try {
 73
 74                   // 写入本地文件系统
 75
 76                                   while ((ch = input.read(rb)) != -1) {
 77
 78                                          output.write(rb, 0, ch);
 79
 80                                         
 81
 82                                   }
 83
 84                                  
 85
 86                            } catch (IOException e) {
 87
 88                                  
 89
 90                                   e.printStackTrace();
 91
 92                            }
 93
 94                           
 95
 96                            try {
 97
 98                                   input.close();
 99
100                            } catch (IOException e) {
101
102                                  
103
104                                   e.printStackTrace();
105
106                            }
107
108                            try {
109
110                                   output.close();
111
112                            } catch (IOException e) {
113
114                           
115
116                                   e.printStackTrace();
117
118                            }
119
120
121
122                     }
123
124
125
126              } catch (SQLException e) {
127
128                     e.printStackTrace();
129
130              }
131
132
133
134              finally {
135
136                     try {
137
138                            rs.close();
139
140                            pre.close();
141
142                            conn.close();
143
144
145
146                     } catch (SQLException e) {
147
148                            e.printStackTrace();
149
150                     }
151
152              }
153
154       }
155
156

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值