Java mysql blob 数据读写操作

package com.lw.database;

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

/**
 * CREATE: CREATE TABLE IDCard ( id char(18),pic BLOB);
 * @author fhadmin
 * from www.fhadmin.cn
 */
public class LOBTest {

    protected static final String DEFAULT_URL = "jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8";
    protected static final String DRIVER_NAME = "com.mysql.jdbc.Driver";
    
    private Connection connection = null;
    
    public LOBTest() throws ClassNotFoundException, SQLException {
        Class.forName(DRIVER_NAME);
        connection = DriverManager.getConnection(DEFAULT_URL, "user", "password");
    }
    
    public void insert(String id,String path) throws SQLException, IOException {
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO IDCard VALUES (?,?)");
        preparedStatement.setString(1, id);
        FileInputStream fileInputStream = new FileInputStream(path);
        preparedStatement.setBlob(2, fileInputStream,fileInputStream.available());
        preparedStatement.execute();
    }
    
    public void get(String id) throws SQLException, IOException {
        PreparedStatement preparedStatement = connection.prepareStatement("SELECT pic FROM IDCard WHERE id = ?");
        preparedStatement.setString(1, id);
        ResultSet results = preparedStatement.executeQuery();
        while(results.next()) {
            FileOutputStream outputStream = new FileOutputStream("/Users/liuwei/temp.png");
            InputStream inputStream = results.getBinaryStream(1);
            int num = -1;
            while((num=inputStream.read())!=-1) {
                outputStream.write(num);
            }
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        }
    }
    
    public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException {
        LOBTest test = new LOBTest();
        test.insert("78907656784323", "/Users/liuwei/Documents/bt_next_nor.png");
        test.get("78907656784323");
    }
}

 

注意:

  MySQL的四种BLOB类型 

    类型  大小(单位:字节) 

      TinyBlob  最大 255B

        Blob  最大 65K 

        MediumBlob  最大 16M 

      LongBlob  最大 4G 

插入图像的时候,注意下图像大小,图像超过该类型所能容纳的最大字节的时候,会报错

工作流模块--------------------------------------------------------------------------www.fhadmin.cn
1.模型管理    :web在线流程设计器、预览流程xml、导出xml、部署流程
2.流程管理    :导入导出流程资源文件、查看流程图、根据流程实例反射出流程模型、激活挂起
3.运行中流程:查看流程信息、当前任务节点、当前流程图、作废暂停流程、指派待办人
4.历史的流程:查看流程信息、流程用时、流程状态、查看任务发起人信息
5.待办任务   :查看本人个人任务以及本角色下的任务、办理、驳回、作废、指派一下代理人
6.已办任务   :查看自己办理过的任务以及流程信息、流程图、流程状态(作废 驳回 正常完成)

注:当办理完当前任务时,下一任务待办人会即时通讯收到新任务消息提醒,当作废和完结任务时,
       任务发起人会收到站内信消息通知

posted @ 2021-07-07 14:19  m17193095294  阅读(1263)  评论(0编辑  收藏  举报