栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 从blob类型得到图片文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 从blob类型得到图片文件

 
    public static String getImageForBlob(byte[] bytes,String name) {

        // 读取文件的前几个字节来判断图片格式
        byte[] b = new byte[4];
        for (int i = 0; i < b.length; i++) {
            b[i] = bytes[i];
        }
        String type = HexUtil.encodeHexStr(b).toUpperCase();;
        if (type.contains("FFD8FF")) {
            type = "JPG";
        } else if (type.contains("89504E47")) {
            type = "png";
        } else if (type.contains("47494638")) {
            type = "GIF";
        } else if (type.contains("424D")) {
            type = "BMP";
        }else{
            type = "jpeg";
        }

        // 设置文件地址
        String destDir = "d:/pic";
        String path = StrUtil.format("{}/{}."+type, destDir,name );

        // 将byte[]转换为输入流写入文件
        InputStream sbs = new ByteArrayInputStream(bytes);
        FileUtil.writeFromStream(sbs, path);

        return urlPath;

    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704717.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号