java实现解析富文本中存在的截取图片base64字符串转译为oss上传路径图片

直接上代码–工具类

 /**
     * 对富文本base64图片进行转换上传处理
     * @param content
     * @return
     * @throws Exception
     */
    public String handlerBase64Content(String content) throws Exception{
        if(StringUtil.isNotEmpty(content)){
            //获取src值
            List<String> srcvalues=getImgSrc(content);
            String temp=null;
            for(String src:srcvalues){
                if(src.startsWith("data:image/")){
                    temp=uploadBase64File(src);
                    content=content.replace(src,temp);
                }
//                else if(src.contains(parentName)){
//                    temp=src.substring(src.indexOf(parentName));
//                    content=content.replace(src,temp);
//                }
                else{
                    //其他的属于网络图片,不用处理
                    //返回图片地址
                }

            }
        }
        return content;
    }

    /**
     * 获取文本中的img标签的src属性值
     * @param htmlStr
     * @return
     */
    public static List<String> getImgSrc(String htmlStr) {
        String img = "";
        Pattern p_image;
        Matcher m_image;
        List<String> pics = new ArrayList<String>();
        String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
        p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
        m_image = p_image.matcher(htmlStr);
        while (m_image.find()) {
            img = img + "," + m_image.group();
            Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
            while (m.find()) {
                pics.add(m.group(1));
            }
        }
        return pics;
    }

    /**
     * 上传base64位的图片
     * @param imgStr
     * @throws Exception
     */
    private String uploadBase64File(String imgStr) throws Exception{
        InputStream is = GenerateImage(imgStr);
        File file=stream2file(is);
        String fileUrl=uploadFile(file);
        File files=new File(file.getPath());
        if(files.exists()) files.delete();
        return fileUrl;
    }
    //base64字符串转化成图片
    private InputStream GenerateImage(String imgStr) throws Exception{

        String newfilename= UUID.randomUUID().toString()+"";
        BASE64Decoder decoder = new BASE64Decoder();
        //Base64解码
        imgStr=imgStr.substring(imgStr.indexOf("base64,")+7);
        byte[] b = decoder.decodeBuffer(imgStr);
        for(int i=0;i<b.length;++i)
        {
            if(b[i]<0)
            {//调整异常数据
                b[i]+=256;
            }
        }
        InputStream inputStream = new ByteArrayInputStream(b);
        return inputStream;
    }
    /**
     * 上传文件
     * @param file
     * @return
     * @throws Exception
     */
    private String uploadFile(File file){
        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        headers.setContentLength(file.length());

        MultiValueMap<String, Object> forms = new LinkedMultiValueMap<>();
        forms.add("multipartFile", new FileSystemResource(file));
        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(forms, headers);

        String data=restTemplate.postForObject("https://xxx.com/xxx/upload",httpEntity,String.class);
        if(StringUtils.isNotEmpty(data)){
            return data;
        }else{
            return null;
        }


    }

实现层直接引用

下面展示一些 内联代码片

//举例context为富文本内容 该方法会直接对IMG内容中 base64字符串转成oss文件路径
public ResultData<String> getUrl(String context) throws Exception {
        String url=appTokenUtils.handlerBase64Content(context);
        return ResultFactory.okResponse(url);
    }

减少数据库负担

  • 11
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值