Fork me on GitHub

blob字段入库与下载的示例

工具类代码如下

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

public class ImageUtil {

// 读取本地图片获取输入流
public static FileInputStream readImage(String path) throws IOException {
    return new FileInputStream(new File(path));
}

// 读取表中图片获取输出流
public static void readBin2Image(InputStream in, String targetPath) {
    File file = new File(targetPath);
    String path = targetPath.substring(0, targetPath.lastIndexOf("\\"));
    if (!file.exists()) {
        new File(path).mkdir();
    }
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(file);
        int len = 0;
        byte[] buf = new byte[1024];
        while ((len = in.read(buf)) != -1) {
            fos.write(buf, 0, len);
        }
        fos.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != fos) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
}

示例代码如下

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

import com.cloud.icenter.common.utils.StringUtil;
import com.cloud.icenter.national.jdbc.DbUtil;
import com.google.common.io.BaseEncoding;
import com.google.common.io.ByteStreams;

public class ImageDemo {

// 将图片插入数据库
public static void readImage2DB() {
    String path = "E:\\微信图片.jpg";
    Connection conn = null;
    PreparedStatement ps = null;
    FileInputStream in = null;
    try {
        in = ImageUtil.readImage(path);
        conn = DbUtil.getConnection();
        String sql = "insert into a_a_sd (id,zp) values (?,?)";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, 1);
        //            ps.setString(2, "Tom");
        ps.setBinaryStream(2, in, in.available());
        //            ps.setBlob(2, in, in.available());
        int count = ps.executeUpdate();
        if (count > 0) {
            System.out.println("插入成功!");
        } else {
            System.out.println("插入失败!");
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DbUtil.closeDbConnection(null, ps, conn);
        if (null != ps) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

}

// 读取数据库中图片
public static void readDB2Image() {
    String targetPath = "E:\\image\\黄色图片.jpg";
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        conn = DbUtil.getConnection();
        String sql = "select * from a_a_sd where id =?";
        ps = conn.prepareStatement(sql);
        ps.setInt(1, 1);
        rs = ps.executeQuery();
        while (rs.next()) {
            InputStream in = rs.getBinaryStream("ZP");
            ImageUtil.readBin2Image(in, targetPath);
//                java.sql.Blob blob = rs.getBlob("ZP");
//                InputStream inStream = blob.getBinaryStream();
//                byte[] bytes = ByteStreams.toByteArray(inStream);
//                BaseEncoding baseEncoding = BaseEncoding.base64();
//                String str = baseEncoding.encode(bytes);
//                str = StringUtil.compress(str);
//                System.out.println(str);

                //data是读出并需要返回的数据,类型是byte[]
//                byte[] s = new byte[(int)blob.length() ];
//                inStream.read(s);
//                inStream.close();

        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        DbUtil.closeDbConnection(null, ps, conn);
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
//测试
public static void main(String[] args) {
    //        readImage2DB();
    readDB2Image();
}
}