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

来源:互联网 发布:淘宝退款率高怎么办 编辑:程序博客网 时间:2024/05/16 01:47

一:建表

 

二:获取数据库连接

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 Administrator12  * 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 Administrator14  * 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 }
0 0
原创粉丝点击