實(shí)現(xiàn)向MYSQL數(shù)據(jù)庫中存儲(chǔ)或提取圖片文件
一些情況下,需要向數(shù)據(jù)庫中存儲(chǔ)一些2進(jìn)制文件,比如圖片文件等,這時(shí)候,向數(shù)據(jù)庫存儲(chǔ)數(shù)據(jù)不同于普通的字符串存儲(chǔ),我們需要對這個(gè)2進(jìn)制文件使用JAVA處理2進(jìn)制流的API進(jìn)行處理,然后再進(jìn)行存儲(chǔ)。我們需要進(jìn)行以下步驟來實(shí)現(xiàn):
向數(shù)據(jù)庫中存儲(chǔ)文件的時(shí)候,一樣使用標(biāo)準(zhǔn)SQL語句,如: insert into database (column1, column2,..) values(v1,v2,…);注意的是,要在建立存放2進(jìn)制文件的TABLE時(shí),存放的字段要使用BLOB類型,而不是普通的VARCHAR等。BLOB是專門存儲(chǔ)2進(jìn)制文件的類型,他還有大小之分,比如mediablob,logblob等,以存儲(chǔ)大小不同的2進(jìn)制文件,一般的圖形文件使用mediablob足以了。
1 見以下代碼實(shí)現(xiàn)向
MYSQL中儲(chǔ)存圖片文件:
…………………………
private final String insertquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
public void doInsertStaffPic(String loginname,String source_URL) {
Connection conn = null;
PreparedStatement pre = null;
try {
// 進(jìn)行數(shù)據(jù)庫連接,這里我使用的是在STRUTS中配置的連接池,當(dāng)然也可// 以自己通過JDBC直接連
conn = DBProcess.getConnection();
//從圖片源中獲得圖片對象并寫到緩存中
Image image = new ImageIcon(source_URL).getImage();
BufferedImage bImage = new BufferedImage(image.getWidth(null),
image.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics bg = bImage.getGraphics();
bg.drawImage(image, 0, 0, null);
bg.dispose();
//將圖片寫入2進(jìn)制的輸出流 并放如到byte[] buf中
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);
byte[] buf = out.toByteArray();
//獲得這個(gè)輸出流并將他設(shè)置到BLOB中
ByteArrayInputStream inStream = new ByteArrayInputStream(buf);
pre = conn.prepareStatement(insertstaffpicquery);
pre.setString(1, loginname);
pre.setBinaryStream(2, inStream, inStream.available());
// 執(zhí)行寫如數(shù)據(jù)
pre.executeUpdate();
} catch (Exception exc) {
exc.printStackTrace();
}
finally {
try {
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2 下代碼實(shí)現(xiàn)從MYSQL中獲取圖片文件并寫入本地文件系統(tǒng):
…………………………
private final String writeoutquery = "insert into employeephoto (Employee_ID,Binary_Photo,LastMod,Created) values (?,?, NOW(), NOW())";
// retrive the picture data from database and write it to the local disk
public void doGetAndShowStaffPic(String loginname, String dir) {
FileOutputStream output = null;
InputStream input = null;
Connection conn = null;
ResultSet rs = null;
PreparedStatement pre = null;
try {
conn = DBProcess.getConnection();
pre = conn.prepareStatement(writeoutquery);
pre.setString(1, loginname);
rs = pre.executeQuery();
if (rs.next()) {
// 從數(shù)據(jù)庫獲得2進(jìn)制文件數(shù)據(jù)
Blob image = rs.getBlob("Binary_Photo");
// setup the streams
Input = image.getBinaryStream();
try {
// 設(shè)置寫出路徑。
output = new FileOutputStream(dir);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// set read buffer size 注意不要設(shè)置的太小,要是太小,圖片可能不完整
byte[] rb = new byte[1024000];
int ch = 0;
// process blob
try {
// 寫入本地文件系統(tǒng)
while ((ch = input.read(rb)) != -1) {
output.write(rb, 0, ch);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} catch (SQLException e) {
e.printStackTrace();
}
finally {
try {
rs.close();
pre.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
[@more@]