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

    /**
     * @param acceptContext 受理内容
     * @return Map
     * @description: 获取处理内容富文本中的图片,并传入禅道附件
     * @author hyh
     * @date 2022/8/5 9:12
     */
    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");
                        //BASE64解码成File文件
                        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);
            //首先将base64保存在服务器端,便于获取file对象
            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;
    }

    /**
     * @return java.util.List
     * @Author shipj
     * @Description 提取富文本图片(img标签src属性)
     * @Date 20:49 2021-04-12
     * @Param [htmlStr]
     **/
    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;
    }

你可能感兴趣的:(JAVA,java)