欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java+MySql圖片數(shù)據(jù)保存與讀取的具體實例

 更新時間:2013年06月20日 10:28:59   作者:  
之前一直沒有做過涉及到圖片存儲的應用,最近要做的東東涉及到了這個點,就做了一個小的例子算是對圖片存儲的初試吧

1.創(chuàng)建表:

復制代碼 代碼如下:

drop table if exists photo;
CREATE TABLE photo (
    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) COMMENT '名稱',
    photo blob COMMENT '照片'
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;

 圖片在MySql中的數(shù)據(jù)存儲格式為blob類型;Blob是一個可以存儲二進制文件的容器。

2.編寫圖片流數(shù)據(jù)存取的工具類:

復制代碼 代碼如下:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {
    private static File file = null;

    /**
     * 從本地文件讀取圖像的二進制流
     *
     * @param infile
     * @return
     */
    public static FileInputStream getImageByte(String infile) {
        FileInputStream imageByte = null;
        file = new File(infile);
        try {
            imageByte = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return imageByte;
    }

    /**
     * 將圖片流讀出為圖片
     *
     * @param inputStream
     * @param path
     */
    public static void readBlob(InputStream inputStream, String path) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            inputStream.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

3.將本地文件保存到數(shù)據(jù)庫

  需要添加MySql的數(shù)據(jù)庫驅(qū)動--mysql-connector-java-5.1.24-bin.jar

復制代碼 代碼如下:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ImageInsert {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        PreparedStatement preparedStatement = null;
        InputStream inputStream = null;
        inputStream = ImageUtil.getImageByte("D:\\temp\\photo1.png");
        try {
            String sql = "insert into photo(id,name,photo) values(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1, 1);
            preparedStatement.setString(2, "朱莉");
            preparedStatement.setBinaryStream(3, inputStream,
                    inputStream.available());
            preparedStatement.execute();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        connection.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }
}


4.從數(shù)據(jù)庫中讀取并生成圖片
復制代碼 代碼如下:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ImageGet {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        String user = "root";
        String password = "root";
        String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(url, user, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        Statement statement = null;
        ResultSet resultSet = null;
        InputStream inputStream = null;
        try {
            statement = connection.createStatement();
            String sql = "select p.photo from photo p where id = 1";
            resultSet = statement.executeQuery(sql);
            resultSet.next();
            inputStream = resultSet.getBinaryStream("photo");
            ImageUtil.readBlob(inputStream, "D:\\temp\\photo2.png");
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (resultSet != null)
                        resultSet.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (statement != null)
                        if (statement != null)
                            try {
                                statement.close();
                            } catch (SQLException e) {
                                e.printStackTrace();
                            } finally {
                                if (connection != null)
                                    try {
                                        connection.close();
                                    } catch (SQLException e) {
                                        e.printStackTrace();
                                    }
                            }
                }
            }
        }

    }
}


5.Over!

相關文章

  • 通過代碼實例了解SpringBoot啟動原理

    通過代碼實例了解SpringBoot啟動原理

    這篇文章主要介紹了通過代碼實例了解SpringBoot啟動原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-12-12
  • 利用java讀取web項目中json文件為map集合方法示例

    利用java讀取web項目中json文件為map集合方法示例

    這篇文章主要給大家介紹了關于利用java讀取web項目中json文件為map集合的相關資料,文中通過示例代碼給大家介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧。
    2017-08-08
  • Java利用Request請求如何獲取IP地址對應的省份、城市詳解

    Java利用Request請求如何獲取IP地址對應的省份、城市詳解

    之前已經(jīng)給大家介紹了關于Java用Request請求獲取IP地址的相關內(nèi)容,那么下面這篇文章將給大家進入深入的介紹,關于Java利用Request請求如何獲取IP地址對應省份、城市的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • SpringBoot集成Redis及Redis使用方法

    SpringBoot集成Redis及Redis使用方法

    Redis是現(xiàn)在最受歡迎的NoSQL數(shù)據(jù)庫之一,Redis是一個使用ANSI C編寫的開源、包含多種數(shù)據(jù)結(jié)構(gòu)、支持網(wǎng)絡、基于內(nèi)存、可選持久性的鍵值對存儲數(shù)據(jù)庫,這篇文章主要介紹了SpringBoot集成Redis及Redis使用方法,需要的朋友可以參考下
    2023-08-08
  • java中int、double、char等變量的取值范圍詳析

    java中int、double、char等變量的取值范圍詳析

    這篇文章主要給大家介紹了關于java中int、double、char等變量取值范圍的相關資料,每個變量都給出了詳細的實例代碼,對大家學習或者使用java具有一定的參考學習價值,需要的朋友可以參考下
    2021-10-10
  • java網(wǎng)絡編程之群聊功能

    java網(wǎng)絡編程之群聊功能

    這篇文章主要為大家詳細介紹了java網(wǎng)絡編程之群聊功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • Spring注解@RestControllerAdvice原理解析

    Spring注解@RestControllerAdvice原理解析

    這篇文章主要介紹了Spring注解@RestControllerAdvice原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • mybatis參數(shù)String與Integer類型的判斷方式

    mybatis參數(shù)String與Integer類型的判斷方式

    這篇文章主要介紹了mybatis參數(shù)String與Integer類型的判斷方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中覆蓋finalize()方法實例代碼

    Java中覆蓋finalize()方法實例代碼

    這篇文章主要介紹了Java中覆蓋finalize()方法實例代碼,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • java中判斷對象類型的3種方法舉例

    java中判斷對象類型的3種方法舉例

    在Java這種強類型語言中類型轉(zhuǎn)換、類型判斷是經(jīng)常遇到的,下面這篇文章主要給大家介紹了關于java中判斷對象類型的3種方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-01-01

最新評論