欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java+MySql图片数据保存与读取的具体实例

程序员文章站 2024-02-11 17:19:10
1.创建表:复制代码 代码如下:drop table if exists photo;create table photo (    id i...

1.创建表:

复制代码 代码如下:

drop table if exists photo;
create table photo (
    id int not null auto_increment primary key,
    name varchar(100) comment '名称',
    photo blob comment '照片'
)
engine=innodb
default charset=utf8
collate=utf8_general_ci;

 图片在mysql中的数据存储格式为blob类型;blob是一个可以存储二进制文件的容器。

2.编写图片流数据存取的工具类:

复制代码 代码如下:

import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;

public class imageutil {
    private static file file = null;

    /**
     * 从本地文件读取图像的二进制流
     *
     * @param infile
     * @return
     */
    public static fileinputstream getimagebyte(string infile) {
        fileinputstream imagebyte = null;
        file = new file(infile);
        try {
            imagebyte = new fileinputstream(file);
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        }
        return imagebyte;
    }

    /**
     * 将图片流读出为图片
     *
     * @param inputstream
     * @param path
     */
    public static void readblob(inputstream inputstream, string path) {
        try {
            fileoutputstream fileoutputstream = new fileoutputstream(path);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputstream.read(buffer)) != -1) {
                fileoutputstream.write(buffer, 0, len);
            }
            inputstream.close();
            fileoutputstream.close();
        } catch (filenotfoundexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        }

    }
}

3.将本地文件保存到数据库

  需要添加mysql的数据库驱动--mysql-connector-java-5.1.24-bin.jar

复制代码 代码如下:

import java.io.ioexception;
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.preparedstatement;
import java.sql.sqlexception;

public class imageinsert {
    public static void main(string[] args) {
        try {
            class.forname("com.mysql.jdbc.driver").newinstance();
        } catch (instantiationexception e) {
            e.printstacktrace();
        } catch (illegalaccessexception e) {
            e.printstacktrace();
        } catch (classnotfoundexception e) {
            e.printstacktrace();
        }
        string user = "root";
        string password = "root";
        string url = "jdbc:mysql://localhost:3306/test?characterencoding=utf-8";
        connection connection = null;
        try {
            connection = drivermanager.getconnection(url, user, password);
        } catch (sqlexception e) {
            e.printstacktrace();
        }
        preparedstatement preparedstatement = null;
        inputstream inputstream = null;
        inputstream = imageutil.getimagebyte("d:\\temp\\photo1.png");
        try {
            string sql = "insert into photo(id,name,photo) values(?,?,?)";
            preparedstatement = connection.preparestatement(sql);
            preparedstatement.setint(1, 1);
            preparedstatement.setstring(2, "朱莉");
            preparedstatement.setbinarystream(3, inputstream,
                    inputstream.available());
            preparedstatement.execute();
        } catch (sqlexception e) {
            e.printstacktrace();
        } catch (ioexception e) {
            e.printstacktrace();
        } finally {
            try {
                if (inputstream != null)
                    inputstream.close();
            } catch (ioexception e) {
                e.printstacktrace();
            } finally {
                try {
                    if (preparedstatement != null)
                        preparedstatement.close();
                } catch (sqlexception e) {
                    e.printstacktrace();
                } finally {
                    try {
                        connection.close();
                    } catch (sqlexception e) {
                        e.printstacktrace();
                    }
                }
            }
        }

    }
}


4.从数据库中读取并生成图片
复制代码 代码如下:

import java.io.ioexception;
import java.io.inputstream;
import java.sql.connection;
import java.sql.drivermanager;
import java.sql.resultset;
import java.sql.sqlexception;
import java.sql.statement;

public class imageget {
    public static void main(string[] args) {
        try {
            class.forname("com.mysql.jdbc.driver").newinstance();
        } catch (instantiationexception e) {
            e.printstacktrace();
        } catch (illegalaccessexception e) {
            e.printstacktrace();
        } catch (classnotfoundexception e) {
            e.printstacktrace();
        }
        string user = "root";
        string password = "root";
        string url = "jdbc:mysql://localhost:3306/test?characterencoding=utf-8";
        connection connection = null;
        try {
            connection = drivermanager.getconnection(url, user, password);
        } catch (sqlexception e) {
            e.printstacktrace();
        }
        statement statement = null;
        resultset resultset = null;
        inputstream inputstream = null;
        try {
            statement = connection.createstatement();
            string sql = "select p.photo from photo p where id = 1";
            resultset = statement.executequery(sql);
            resultset.next();
            inputstream = resultset.getbinarystream("photo");
            imageutil.readblob(inputstream, "d:\\temp\\photo2.png");
        } catch (sqlexception e) {
            e.printstacktrace();
        } finally {
            try {
                if (inputstream != null)
                    inputstream.close();
            } catch (ioexception e) {
                e.printstacktrace();
            } finally {
                try {
                    if (resultset != null)
                        resultset.close();
                } catch (sqlexception e) {
                    e.printstacktrace();
                } finally {
                    if (statement != null)
                        if (statement != null)
                            try {
                                statement.close();
                            } catch (sqlexception e) {
                                e.printstacktrace();
                            } finally {
                                if (connection != null)
                                    try {
                                        connection.close();
                                    } catch (sqlexception e) {
                                        e.printstacktrace();
                                    }
                            }
                }
            }
        }

    }
}


5.over!