java+mysql实现保存图片到数据库,以及读取数据库存储的图片

一:建表

 

二:获取数据库连接

1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar

2:写代码连接数据库,如下:

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

三:封装读取图片的流

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

四:实现图片(本地、数据库互相传输)

 1 /**
 2  * 
 3  */
 4 package com.hlcui.file;
 5 
 6 import java.io.FileInputStream;
 7 import java.io.InputStream;
 8 import java.sql.Connection;
 9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 
13 /**
14  * @author Administrator 测试写入数据库以及从数据库中读取
15  */
16 public class ImageDemo {
17 
18     // 将图片插入数据库
19     public static void readImage2DB() {
20         String path = "D:/1.png";
21         Connection conn = null;
22         PreparedStatement ps = null;
23         FileInputStream in = null;
24         try {
25             in = ImageUtil.readImage(path);
26             conn = DBUtil.getConn();
27             String sql = "insert into photo (id,name,photo)values(?,?,?)";
28             ps = conn.prepareStatement(sql);
29             ps.setInt(1, 1);
30             ps.setString(2, "Tom");
31             ps.setBinaryStream(3, in, in.available());
32             int count = ps.executeUpdate();
33             if (count > 0) {
34                 System.out.println("插入成功!");
35             } else {
36                 System.out.println("插入失败!");
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         } finally {
41             DBUtil.closeConn(conn);
42             if (null != ps) {
43                 try {
44                     ps.close();
45                 } catch (SQLException e) {
46                     e.printStackTrace();
47                 }
48             }
49         }
50 
51     }
52 
53     // 读取数据库中图片
54     public static void readDB2Image() {
55         String targetPath = "D:/image/1.png";
56         Connection conn = null;
57         PreparedStatement ps = null;
58         ResultSet rs = null;
59         try {
60             conn = DBUtil.getConn();
61             String sql = "select * from photo where id =?";
62             ps = conn.prepareStatement(sql);
63             ps.setInt(1, 1);
64             rs = ps.executeQuery();
65             while (rs.next()) {
66                 InputStream in = rs.getBinaryStream("photo");
67                 ImageUtil.readBin2Image(in, targetPath);
68             }
69         } catch (Exception e) {
70             e.printStackTrace();
71         } finally {
72             DBUtil.closeConn(conn);
73             if (rs != null) {
74                 try {
75                     rs.close();
76                 } catch (SQLException e) {
77                     e.printStackTrace();
78                 }
79             }
80             if (ps != null) {
81                 try {
82                     ps.close();
83                 } catch (SQLException e) {
84                     e.printStackTrace();
85                 }
86             }
87 
88         }
89     }
90     //测试
91     public static void main(String[] args) {
92         //readImage2DB();
93         readDB2Image();
94     }
95 }

以上代码均已经验证!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值