前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >MySQL 中Blob类型数据的插入和读取

MySQL 中Blob类型数据的插入和读取

作者头像
Dream城堡
发布2019-05-24 09:43:52
9.2K0
发布2019-05-24 09:43:52
举报
文章被收录于专栏:Spring相关Spring相关

​ 我们在操作数据存入blob数据的类型,常用来存储头像图片等流数据,blob类型如果想要存储比较大的流文件的数据,建议选用longBlob的数据类型,Demo中的数据就简单的示范了一下,sql文件如下:

代码语言:javascript
复制
DROP TABLE IF EXISTS `image_save`;
CREATE TABLE `image_save` (
  `image_name` varchar(255) DEFAULT NULL,
  `image_in` longblob
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

​ 插入图片和读取图片到本机的操作如下:

代码语言:javascript
复制
public class BlobTest {

    public static void main(String[] args) throws IOException, SQLException {
        //把图片存为blob的格式到数据库
        // storePicBlog();

        //从数据库读取blob的格式的图片数据
        getPicBlog();
    }

    public static void storePicBlog() throws FileNotFoundException, SQLException, IOException {

        String m_dbDriver    ="com.mysql.jdbc.Driver";
        String m_dbUrl   ="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        Connection conn = DriverManager.getConnection(m_dbUrl, "root", "root");

        File f = new File("C:\\Users\\admin\\Desktop\\timg.jpg");
        FileInputStream fis = new FileInputStream(f);

        String sql = "insert into image_save(image_name,image_in) values(?,?)";

        PreparedStatement ps = conn.prepareStatement(sql);

        ps.setString(1, "临时图片");
        ps.setBinaryStream(2, fis, (int)f.length());

        ps.executeUpdate();

        conn.close();
        ps.close();
    }
    public static void getPicBlog() throws FileNotFoundException, SQLException, IOException {

        String m_dbDriver    ="com.mysql.jdbc.Driver";
        String m_dbUrl   ="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC";
        Connection conn = DriverManager.getConnection(m_dbUrl, "root", "root");

        File f = new File("C:\\Users\\admin\\Desktop\\timg.jpg");
        FileInputStream fis = new FileInputStream(f);

        String sql = "select image_in from image_save where image_name='临时图片'";

        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = ps.executeQuery(sql);
        Blob imageIn = null;
        while (rs.next()){
             imageIn = rs.getBlob("image_in");
        }
        InputStream in = imageIn.getBinaryStream();

        OutputStream out = new FileOutputStream("C:\\Users\\admin\\Desktop\\cat.jpg");
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len = in.read(buffer)) != -1){
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();

        conn.close();
        ps.close();
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.05.23 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档