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

JAVA:富文本框截图,将图片的Base64转File文件进行保存

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

JAVA:富文本框截图,将图片的Base64转File文件进行保存

    
    public static Map getAcceptContextImgs( String acceptContext ) {
        Map fileMap = new HashMap<>(1 << 4);
        String num = String.format("%04d", new Random().nextInt(10000));
        if (StringUtils.isNotEmpty(acceptContext)) {
            List imgList = getImgStr(acceptContext);
            if (!org.springframework.util.CollectionUtils.isEmpty(imgList)) {
                for (int i = 0; i < imgList.size(); i++) {
                    if (imgList.get(i).contains("base64,")) {
                        String fileName = String.format("%s%s%s", num, i, ".jpg");
                        fileMap.put(base64ToFile(imgList.get(i).split("base64,")[1], fileName), fileName);
                    }
                }
            }
        }

        return fileMap;
    }

    //BASE64解码成File文件
    public static File base64ToFile( String base64, String fileName ) {
        File file = null;
        //创建文件目录
        String filePath = ZEN_TAO_ACCEPTCONTEXT_IMGS_PATH;
        File dir = new File(filePath);
        if (!dir.exists() && !dir.isDirectory()) {
            dir.mkdirs();
        }
        BufferedOutputStream bos = null;
        java.io.FileOutputStream fos = null;
        try {
            byte[] bytes = Base64.getDecoder().decode(base64);
            file = new File(filePath + "/" + fileName);
            fos = new java.io.FileOutputStream(file);
            bos = new BufferedOutputStream(fos);
            bos.write(bytes);
        } catch (Exception e) {
            log.error("禅道工单富文本图片存储失败0", e);
            throw new BaseException("禅道工单富文本图片存储失败");
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    log.error("禅道工单富文本图片存储失败1", e);
                    throw new BaseException("禅道工单富文本图片存储失败");
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    log.error("禅道工单富文本图片存储失败2", e);
                    throw new BaseException("禅道工单富文本图片存储失败");
                }
            }
        }
        return file;
    }

    
    public static List getImgStr( String htmlStr ) {
        List list = new ArrayList<>();
        Matcher m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            // 得到数据
            String img = m_image.group();
//            System.out.println(img);
            // 匹配中的src数据
            Matcher m = r_image.matcher(img);
            while (m.find()) {
                list.add(m.group(1));
            }
        }
        return list;
    }

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

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

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