将二进制流转换为图片

如何从数据库中取出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();
        }
    }

ok了调用上面的方法就可以实现将二进制流转换为图片了,当然因为数据库里面存储的是图片,你如果是存储的其他的格式的文件仅仅需要更改的是存储路径里面最后的格式就行了

希望对你有所帮助!!


猜你喜欢

转载自blog.csdn.net/datouniao1/article/details/80002615