zoukankan      html  css  js  c++  java
  • 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 }

    以上代码均已经验证!

  • 相关阅读:
    Java Web(三) 会话机制,Cookie和Session详解(转载)
    如何转载别人的文章
    C#操作Word的+ CKEditor 輸出成Word文件(包含圖案上傳)
    vs2013 命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”。是否缺少程序集引用?
    CKEditor与CKFinder学习--自定义界面及按钮事件捕获
    JS获取CkEditor在线编辑的内容
    <%#Eval() %>的常用方法
    ASP.NET 中 ContentType 类型
    富文本编辑器 CKeditor 配置使用+上传图片
    VS2013 蛋疼的“AJAX Control Toolkit”安装过程
  • 原文地址:https://www.cnblogs.com/warrior4236/p/5682830.html
Copyright © 2011-2022 走看看