温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-11 12:19:39 来源:ITPUB博客 阅读:214 作者:rainytag 栏目: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中储存图片文件:



…………………………

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 {

// 进行数据库连接,这里我使用的是在STRUTS中配置的连接池,当然也可// 以自己通过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进制的输出流 并放如到byte[] buf中

ByteArrayOutputStream out = new ByteArrayOutputStream();

ImageIO.write(bImage, "jpg", out);

byte[] buf = out.toByteArray();



//获得这个输出流并将他设置到BLOB中

ByteArrayInputStream inStream = new ByteArrayInputStream(buf);

pre = conn.prepareStatement(insertstaffpicquery);

pre.setString(1, loginname);

pre.setBinaryStream(2, inStream, inStream.available());

// 执行写如数据

pre.executeUpdate();





} catch (Exception exc) {

exc.printStackTrace();

}



finally {

try {

pre.close();

conn.close();



} catch (SQLException e) {

e.printStackTrace();

}

}



}



2 下代码实现从MYSQL中获取图片文件并写入本地文件系统:





…………………………

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()) {

// 从数据库获得2进制文件数据

Blob image = rs.getBlob("Binary_Photo");

// setup the streams

Input = image.getBinaryStream();


try {

// 设置写出路径。

output = new FileOutputStream(dir);

} catch (FileNotFoundException e1) {



e1.printStackTrace();

}

// set read buffer size 注意不要设置的太小,要是太小,图片可能不完整

byte[] rb = new byte[1024000];

int ch = 0;

// process blob



try {

// 写入本地文件系统

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@]
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI