Java实现MySQL图片存取

Reference

Java实现对Mysql的图片存取操作
java 字节流读取图片,字符流读取,二进制读取
mysql BLOB字段类型用法介绍

Notes

  • Java对图片的读写就跟其它文件一样的,但要用字节流用字符流
  • MySQL中各种blob的大小限制
TinyBlobBlobMediumBlobLongBlob
225B65KB16MB4GB

MySQL Table

  • 表名:receipt
RidRpictureRtype
varchar(20)blobvarchar(20)
CREATE TABLE receipt (
    Rid VARCHAR(20) PRIMARY KEY,
    Rpicture BLOB NOT NULL,
    Rtype VARCHAR(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Code

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PhotoIO {
//--- Parameter
    private static final String driver = "com.mysql.jdbc.Driver";
    private static final String URL = "jdbc:mysql://<IP_ADDRESS>:<PORT>/<DB_NAME>";
    private static final String user = "<USER_NAME>";
    private static final String psw = "<USER_PASSWORD>";

//--- Private Member
    private static Connection con = null;
    private static PreparedStatement pst = null;
    private static ResultSet rs = null;

//--- Auto-Run
    static {
        try {
            // 加载驱动
            Class.forName(driver);
            // 连接数据库
            con = DriverManager.getConnection(URL, user, psw);
        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }

//--- Public Function
    // 将图片放进数据库
    // id->Rid,pic是文件路径
    public static void store_file(String id, String pic) {
        // "?"是参数,相当于Qt中QString中的"%1"
        String sql = "INSERT receipt(Rid, Rpicture, Rtype) VALUES(?, ?, ?)";
        String type = pic.substring(pic.lastIndexOf((int)'.'));
        try {
            pst = con.prepareStatement(sql);
            // 打开文件
            FileInputStream fis = new FileInputStream(file);
            // 将第一个问号替换为id
            pst.setString(1, id);
            // 替换第二个问号
            pst.setBlob(2, fis, fis.available());
            // 替换第三个问号
            pst.setString(3, type);
            // 执行SQL
            pst.executeUpdate();
            // 关闭资源
            fis.close();
            pst.close();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        } catch(SQLException e) {
            e.printStackTrace();
        }
    }

    // 将图片从数据库中取出
    // id->Rid,path是存图路径
    public static boolean load_file(String id, String path) {
        String sql = "SELECT * FROM receipt WHERE Rid=?";
        boolean got = false;
        try {
            pst = con.prepareStatement(sql);
            pst.setString(1, id);
            rs = pst.executeQuery();
            if(rs.next()) { // 找到记录
                InputStream is = rs.getBinaryStream("Rpicture");
                byte buf[] = new byte[is.available()];
                is.read(buf);
                String type = rs.getString("Rtype");
                FileOutputStream fos = new FileOutputStream(path + id + type);
                fos.write(buf);
                // 刷新缓冲区
                fos.flush();
                // 关闭流
                fos.close();
                is.close();
                got = true;
            } else { // 没找到
                System.out.println("Not such id!");
                got = false;
            }
            pst.close();
            rs.close();
        } catch(SQLException e) {
            e.printStackTrace();
        } catch(FileNotFoundException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
        return got;
    }

/*--- Debug
    public static void main(String args[]) {
        store_file("12138", "D:/test.jpg");
        load_file("12138", "C:/");
    }
---*/
}
  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值