日期:2014-05-20  浏览次数:20727 次

java 读取orcale blob 生成图片保存到D盘

设:已从数据库取出Blob(java.sql.Blob)变量名 blob

取出的是张图片,我现在要保存在 D盘 高手贴代码,要完整的,在线等,可运行 立刻给分

------解决方案--------------------
给你个静态方法参考,blob 是你取出的值,name 是你的图片名字,不过我没用

Java code

public static void writeAvatarToDisk(Blob blob, String name) throws IOException {

        byte[] data = null;
        try {
            InputStream inStream = blob.getBinaryStream();
            
              BufferedImage bimage = null;                   //缓存图片对象
              BufferedInputStream ins = new BufferedInputStream(inStream);       //生成缓存输入流
              bimage=ImageIO.read(ins);                      //由ImageIO对象读成缓存图片对象。ImageIO是个很重要的对象
              FileOutputStream sos=new FileOutputStream(new File("d:/111.jpg"));
              BufferedOutputStream bos = new BufferedOutputStream(sos);    //生成输出缓存
              JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);  //生成JPEG图片对象
              encoder.encode(bimage);                         //按格式把图片流进行编码
              bos.close();
              sos.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

------解决方案--------------------
我来手写一下

Blob blob = rs.getBlob("imageField");

InputStream is = blob.getInputStream();

FileOutputStream fos = new FileOutputStream("D:/a.jpg");

byte[] data = new byte[8096];
int length = 0;
while((length = is.read(data, length,8096)) != -1) {
 fos.write(data, 0 ,length);
 fos.flush();
}

fos.close();
is.close();