日期:2014-05-20 浏览次数:20781 次
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();