程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> 關於MYSQL數據庫 >> 向MYSQL數據庫中存儲或提取圖片文件

向MYSQL數據庫中存儲或提取圖片文件

編輯:關於MYSQL數據庫

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       }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved