java之二进制存取图片(MySQL数据库)

闲来无事,弄了个二进制读取图片,而非本地路径读取,仅供学习之用,大家多多指教!!
第一步建表:

SQL:

create table images_info(

image_id int(10) not null auto_increment primary key,

image_name varchar(255),

image_size int,

image_date datetime,

image_type varchar(10),

image_data longblob,

image_description varchar(255),

author varchar(100));


修改图片传入最大值:

mySQL安装目录下的my-small.ini文件里面的max_allowed_packet = 1M修改成:

max_allowed_packet = 32M

(参考:在Linux系统中它的配置在mycnf文件中加入max_allowed_packet=32M就行
在命令行配置的方法是:mysqld-nt --console --max_allowed_packet=32M)


第二步编写java代码:
1.插入图片到数据库中代码片段:
private Connection conn = null;
private PreparedStatement pstmt = null;
private static final String sql = "INSERT INTO images_info(image_id,image_name,image_size,image_date,image_type,image_description,author,image_data)VALUES(null,?,?,now(),?,?,?,?)";
public boolean addPhoto(ImageVo imageVo) {
boolean flag = false;
try{
//将文件转换为流文件
InputStream photoStream = new FileInputStream(imageVo.getImageData());
//获取数据库连接
conn = ConnectionFactory.getConnection();
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, imageVo.getImageName());
pstmt.setInt(2, imageVo.getImageSize());
pstmt.setString(3 , "jpg");//图片类型
pstmt.setString(4, imageVo.getDescription());
pstmt.setString(5, imageVo.getAuthor());
pstmt.setBinaryStream(6, photoStream, (int)imageVo.getImageData().length());
int row = pstmt.executeUpdate();
if(row == 1){
flag = true;
}
}catch(FileNotFoundException fe){
fe.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}finally{
if(null != pstmt){
try{pstmt.close();}
catch(SQLException e){
e.printStackTrace();
}
}
if(null != conn){
try{conn.close();}
catch(SQLException e){
e.printStackTrace();
}
}
}
return flag;
}

2.从数据库读取图片信息
private Connection conn = null;
private PreparedStatement pstmt = null;
private ResultSet rs = null;
//查询具体图片数据流格式
private static final String data_sql = "select image_data from images_info where image_id = ?";
public void ReadImage(int imageId,String imageName){
FileOutputStream fos = null;
InputStream in = null;
byte[] Buffer = new byte[4096];
try{
conn = ConnectionFactory.getConnection();
pstmt = conn.prepareStatement(data_sql);
pstmt.setInt(1, imageId);
rs = pstmt.executeQuery();
rs.next();
File file = new File(imageName);
if(!file.exists()){
file.createNewFile();//如果文件不存在,则创建
}
fos = new FileOutputStream(file);
in = rs.getBinaryStream("image_data");
int size = 0;
while ((size = in.read(Buffer)) != -1) {
fos.write(Buffer, 0, size);
}
}catch(SQLException e){
e.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
//关闭数据流
if(null != rs){
try{
rs.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(null != pstmt){
try{
pstmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(null != conn){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
对象类属性:
String imageId;
String imageName;
int imageSize;
Date imageDate;
String imageType;
String description;
String author;
File imageData;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值