zoukankan      html  css  js  c++  java
  • java-mysql(3) 读写image

    在mysql里面用来存储图片有一个特殊的数据对象叫做 Blob(Binary Large Object).

    数据库里面插入一张图片:

    第一步:需要为图片创建一个文件对象

    File img = new File(proppath);
    FileInputStream fileInputStream = null;
    try {
           fileInputStream = new FileInputStream(img);
      } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
             e1.printStackTrace();
       }

    第二部:创建二进制的数据流

    preparedStatement.setBinaryStream(1, fileInputStream,
                        (int) img.length());
     preparedStatement.executeUpdate();

    调用的是prepared statement的setBinaryStream对象:几个参数分别是需要绑定的参数索引位置,一个文件输入流,图片的字节数

     1 package core;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileNotFoundException;
     6 import java.sql.Connection;
     7 import java.sql.DriverManager;
     8 import java.sql.PreparedStatement;
     9 import java.sql.SQLException;
    10 import java.sql.Statement;
    11 import java.sql.ResultSet;
    12 
    13 public class MethodReferencesTest {
    14 
    15     public static void main(String[] args) throws CloneNotSupportedException {
    16         // TODO Auto-generated method stub
    17 
    18         Connection connection = null;
    19         Statement statement = null;
    20         ResultSet resultSet = null;
    21         PreparedStatement preparedStatement = null;
    22         String proppath = System.getProperty("user.dir")
    23                 + "/src/core/c9c15f0edd07ee240f3af06de6775888.jpg";
    24         
    25         //创建image object
    26         File img = new File(proppath);
    27         FileInputStream fileInputStream = null;
    28         try {
    29             fileInputStream = new FileInputStream(img);
    30         } catch (FileNotFoundException e1) {
    31             // TODO Auto-generated catch block
    32             e1.printStackTrace();
    33         }
    34         String sqlurl = "jdbc:mysql://172.20.23.75:3306/testdb";
    35         String sqluser = "root";
    36         String sqlpassword = "123456";
    37         String sql = "INSERT INTO Images(Data) VALUES(?)";
    38         
    39         try {
    40             connection = DriverManager.getConnection(sqlurl, sqluser,
    41                     sqlpassword);
    42             
    43             preparedStatement = connection.prepareStatement(sql);
    44             preparedStatement.setBinaryStream(1, fileInputStream,
    45                     (int) img.length());
    46             preparedStatement.executeUpdate();
    47 
    48         } catch (SQLException e) {
    49             // TODO Auto-generated catch block
    50             e.printStackTrace();
    51         } finally {
    52             try {
    53                 if (resultSet != null) {
    54                     resultSet.close();
    55                 }
    56                 if (statement != null) {
    57                     statement.close();
    58                 }
    59                 if (preparedStatement != null) {
    60                     preparedStatement.close();
    61                 }
    62                 if (connection != null) {
    63                     connection.close();
    64                 }
    65             } catch (SQLException e) {
    66                 e.printStackTrace();
    67             }
    68         }
    69 
    70     }
    71 
    72 }
    点我看完整代码

     从数据库里面读取图片

    第一步:创建一个输出流对象

    //创建image object
    File img = new File(proppath);
    FileOutputStream fileputStream = null;
    try {
        fileputStream = new FileOutputStream(img);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    第二步:调用getblob()获取图片数据

     1 connection = DriverManager.getConnection(sqlurl, sqluser,
     2                     sqlpassword);
     3 
     4             preparedStatement = connection.prepareStatement(sql);
     5             resultSet = preparedStatement.executeQuery();
     6             if (resultSet.next()) {
     7                 // 获取图片数据
     8                 Blob blob = resultSet.getBlob("Data");
     9                 // 获取字节数
    10                 int len = (int) blob.length();
    11                 byte[] buf = blob.getBytes(1, len);
    12                 try {
    13                     fileputStream.write(buf, 0, len);
    14                 } catch (IOException e) {
    15                     // TODO Auto-generated catch block
    16                     e.printStackTrace();
    17                 }
    18 
    19             }
  • 相关阅读:
    angular js 多处获取ajax数据的方法
    回调函数(在原生ajax中应用) 事件监听 与promise的应用介绍
    AngularJS Scope(作用域)
    angular js 模型 (ng-model指令)
    angular js 指令 ng-model与 no-repeat的使用 ng-各种指令 创建自定义指令 限制使用指令 restrict的不同取值
    Ka的回溯编程练习 Part2|八皇后问题和N皇后问题
    Ka的回溯编程练习 Part1|整划什么的。。
    Ka的递归编程练习 Final.Part8|回溯前传二|排列组合
    Ka的递归编程练习 Part7|回溯前传一|素数环!
    Ka的递归编程练习 Part6|简单背包问题,拒绝动规从我做起
  • 原文地址:https://www.cnblogs.com/xiamuyouren/p/3283259.html
Copyright © 2011-2022 走看看