当前位置:   article > 正文

将图片存进mysql数据库_mysql数据库存储图片

mysql数据库存储图片

场景:前端页面要展现公司、商铺信息,其中包括公司logo、商铺图片。

正常的图片储存要么放进本地磁盘,要么就存进数据库。存入本地很简单,本篇博客记录如何将图片存进mysql数据库,如果要图片存进数据库,要将图片转化成二进制。

1.数据库存储图片的字段类型要为blob二进制大对象类型

2.将图片流转化为二进制 下面放上代码实例

一、数据库

  1. CREATE TABLE `photo`
  2. ( `id` int(11) NOT NULL,
  3. `name` varchar(255) DEFAULT NULL,
  4. `photo` longblob,
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

备注:

1、记得添加MySQL驱动

2、`photo`字段类型一定要足够大,不然会报错

二、数据库链接

  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.SQLException;
  4. /**
  5. * @author Allen
  6. *
  7. */
  8. public class DBUtil {
  9. // 定义数据库连接参数
  10. public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
  11. public static final String URL = "jdbc:mysql://10.x.x.x:3306/test";
  12. public static final String USERNAME = "root";
  13. public static final String PASSWORD = "123456";
  14. // 注册数据库驱动
  15. static {
  16. try {
  17. Class.forName(DRIVER_CLASS_NAME);
  18. } catch (ClassNotFoundException e) {
  19. System.out.println("注册失败!");
  20. e.printStackTrace();
  21. }
  22. }
  23. // 获取连接
  24. public static Connection getConn() throws SQLException {
  25. return DriverManager.getConnection(URL, USERNAME, PASSWORD);
  26. }
  27. // 关闭连接
  28. public static void closeConn(Connection conn) {
  29. if (null != conn) {
  30. try {
  31. conn.close();
  32. } catch (SQLException e) {
  33. System.out.println("关闭连接失败!");
  34. e.printStackTrace();
  35. }
  36. }
  37. }
  38. //测试
  39. public static void main(String[] args) throws SQLException {
  40. System.out.println(DBUtil.getConn());
  41. }
  42. }

 三、图片流

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. /**
  7. * @author Allen
  8. *
  9. */
  10. public class ImageUtil
  11. {
  12. // 读取本地图片获取输入流
  13. public static FileInputStream readImage(String path) throws IOException
  14. {
  15. return new FileInputStream(new File(path));
  16. }
  17. // 读取表中图片获取输出流
  18. public static void readBin2Image(InputStream in, String targetPath)
  19. {
  20. File file = new File(targetPath);
  21. String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
  22. if (!file.exists())
  23. {
  24. new File(path).mkdir();
  25. }
  26. FileOutputStream fos = null;
  27. try
  28. {
  29. fos = new FileOutputStream(file);
  30. int len = 0;
  31. byte[] buf = new byte[1024];
  32. while ((len = in.read(buf)) != -1)
  33. {
  34. fos.write(buf, 0, len);
  35. }
  36. fos.flush();
  37. }
  38. catch (Exception e)
  39. {
  40. e.printStackTrace();
  41. }
  42. finally
  43. {
  44. if (null != fos)
  45. {
  46. try
  47. {
  48. fos.close();
  49. }
  50. catch (IOException e)
  51. {
  52. e.printStackTrace();
  53. }
  54. }
  55. }
  56. }
  57. }

四、转码存储

  1. import java.io.FileInputStream;
  2. import java.io.InputStream;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. /**
  8. * @author Allen 测试写入数据库以及从数据库中读取
  9. */
  10. public class ImageDemo
  11. {
  12. // 将图片插入数据库
  13. public static void readImage2DB()
  14. {
  15. String path = "G:/PPP/20221014173640.jpg";
  16. Connection conn = null;
  17. PreparedStatement ps = null;
  18. FileInputStream in = null;
  19. try
  20. {
  21. in = ImageUtil.readImage(path);
  22. conn = DBUtil.getConn();
  23. String sql = "insert into photo (id,name,photo)values(?,?,?)";
  24. ps = conn.prepareStatement(sql);
  25. ps.setInt(1, 1);
  26. ps.setString(2, "Tom");
  27. ps.setBinaryStream(3, in, in.available());
  28. int count = ps.executeUpdate();
  29. if (count > 0)
  30. {
  31. System.out.println("插入成功!");
  32. }
  33. else
  34. {
  35. System.out.println("插入失败!");
  36. }
  37. }
  38. catch (Exception e)
  39. {
  40. e.printStackTrace();
  41. }
  42. finally
  43. {
  44. DBUtil.closeConn(conn);
  45. if (null != ps)
  46. {
  47. try
  48. {
  49. ps.close();
  50. }
  51. catch (SQLException e)
  52. {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57. }
  58. // 读取数据库中图片
  59. public static void readDB2Image()
  60. {
  61. String targetPath = "G:/demo.jpg";
  62. Connection conn = null;
  63. PreparedStatement ps = null;
  64. ResultSet rs = null;
  65. try
  66. {
  67. conn = DBUtil.getConn();
  68. String sql = "select * from photo where id =?";
  69. ps = conn.prepareStatement(sql);
  70. ps.setInt(1, 1);
  71. rs = ps.executeQuery();
  72. while (rs.next())
  73. {
  74. InputStream in = rs.getBinaryStream("photo");
  75. ImageUtil.readBin2Image(in, targetPath);
  76. }
  77. }
  78. catch (Exception e)
  79. {
  80. e.printStackTrace();
  81. }
  82. finally
  83. {
  84. DBUtil.closeConn(conn);
  85. if (rs != null)
  86. {
  87. try
  88. {
  89. rs.close();
  90. }
  91. catch (SQLException e)
  92. {
  93. e.printStackTrace();
  94. }
  95. }
  96. if (ps != null)
  97. {
  98. try
  99. {
  100. ps.close();
  101. }
  102. catch (SQLException e)
  103. {
  104. e.printStackTrace();
  105. }
  106. }
  107. }
  108. }
  109. //测试
  110. public static void main(String[] args)
  111. {
  112. /*
  113. 备注:先执行readImage2DB();
  114. 后执行readDB2Image();
  115. */
  116. readImage2DB(); //图片插入数据库方法
  117. //readDB2Image(); //读取数据库图片到指定位置
  118. }
  119. }

 五、结果展现

我这里使用的数据库工具是:DBeaver

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/800581
推荐阅读
相关标签
  

闽ICP备14008679号