天天看点

将二进制流转换为图片

如何从数据库中取出blob类型数据,并且转换为图片存到固定的路径

首先从数据库里面讲blob类型的数据取出来:

byte[] photo = userinfo.getPhoto();
        String path="D:\\08\\11.jpg";
        byte2image(photo,path);      

调用函数将二进制流准换为图片存储到本地

public static void byte2image(byte[] data, String path) {
        if (data.length < 3 || path.equals(""))
            return;
        try {
            FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
            imageOutput.write(data, 0, data.length);
            imageOutput.close();
            System.out.println("Make Picture success,Please find image in " + path);
        }
        catch (Exception ex) {
            System.out.println("Exception: " + ex);
            ex.printStackTrace();
        }
    }