最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java+MySql圖片數據保存與讀取的具體實例

 更新時間: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中的數據存儲格式為blob類型;Blob是一個可以存儲二進制文件的容器。

2.編寫圖片流數據存取的工具類:

復制代碼 代碼如下:

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.將本地文件保存到數據庫

  需要添加MySql的數據庫驅動--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.從數據庫中讀取并生成圖片
復制代碼 代碼如下:

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!

相關文章

  • Java如何優(yōu)雅實現數組切片和拼接操作

    Java如何優(yōu)雅實現數組切片和拼接操作

    在做一道算法題的時候用到數組合并,并且有性能要求,這里對Java數組合并進行總結,下面這篇文章主要給大家介紹了關于Java如何優(yōu)雅實現數組切片和拼接操作的相關資料,需要的朋友可以參考下
    2024-04-04
  • 關于mybatis3中@SelectProvider的使用問題

    關于mybatis3中@SelectProvider的使用問題

    這篇文章主要介紹了mybatis3中@SelectProvider的使用技巧,@SelectProvide指定一個Class及其方法,并且通過調用Class上的這個方法來獲得sql語句,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-12-12
  • SpringMVC解析JSON請求數據問題解析

    SpringMVC解析JSON請求數據問題解析

    這篇文章主要介紹了SpringMVC解析JSON請求數據問題解析,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • java多線程有序讀取同一個文件

    java多線程有序讀取同一個文件

    這篇文章主要為大家詳細介紹了java多線程有序讀取同一個文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • 多線程(多窗口賣票實例講解)

    多線程(多窗口賣票實例講解)

    下面小編就為大家?guī)硪黄嗑€程(多窗口賣票實例講解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • Java深入學習圖形用戶界面GUI之事件處理

    Java深入學習圖形用戶界面GUI之事件處理

    這篇文章主要介紹了基于Java GUI 事件處理方式,一個圖形界面制作完成了,在程序開發(fā)中只是完成了起步的工作。要想讓一個組件都發(fā)揮自己的作用.就必須對所有的組件進行事件處理
    2022-05-05
  • java基礎的詳細了解第一天

    java基礎的詳細了解第一天

    這篇文章對Java編程語言的基礎知識作了一個較為全面的匯總,在這里給大家分享一下。需要的朋友可以參考,希望能給你帶來幫助
    2021-08-08
  • Spring?Validation中的用戶注冊、JWT令牌之用戶登入功能

    Spring?Validation中的用戶注冊、JWT令牌之用戶登入功能

    本文介紹了使用SpringValidation進行用戶注冊驗證和JWT進行用戶登錄的方法,在用戶注冊時,通過@Validated注解和@Pattern注解對用戶名和密碼進行格式校驗,并使用Result對象返回驗證結果,感興趣的朋友一起看看吧
    2024-11-11
  • spring?boot集成loback日志配置的示例代碼

    spring?boot集成loback日志配置的示例代碼

    這篇文章主要介紹了spring?boot集成loback日志配置的示例代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2024-01-01
  • SpringMVC后端Controller頁面跳轉的三種方式匯總

    SpringMVC后端Controller頁面跳轉的三種方式匯總

    這篇文章主要介紹了SpringMVC后端Controller頁面跳轉的三種方式匯總,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評論

响水县| 靖宇县| 托里县| 金乡县| 通河县| 禹城市| 大安市| 衡阳县| 贺兰县| 思南县| 边坝县| 凯里市| 新郑市| 鄂尔多斯市| 峨边| 辉县市| 上林县| 永新县| 大新县| 镇赉县| 洞头县| 虞城县| 兰州市| 离岛区| 正宁县| 渑池县| 中西区| 图木舒克市| 老河口市| 肥东县| 阿荣旗| 合山市| 保山市| 虞城县| 株洲县| 达拉特旗| 靖州| 呼伦贝尔市| 榆林市| 辰溪县| 云南省|