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

  • Post author:
  • Post category:java


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

第一步建表:

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;



版权声明:本文为Shabrave原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。