美文网首页
Echarts 图片保存到服务器 详解

Echarts 图片保存到服务器 详解

作者: MangoDai | 来源:发表于2017-09-18 18:11 被阅读0次
  1. Echarts 介绍 点我

  2. 通过事件跳转到JS函数

  3. 通过全局echartsDOM实例得到图片
    是被加密过的

      img = myChart.getDataURL({
      pixelRatio: 2, // double pixel
      backgroundColor: '#fff' 
    });
    
  4. Base64位图片传到服务器

    1. base64位图片直接传到服务器,会被http协议转换掉,具体是空格+
    String base64Str = request.getParameter("img");
    base64Str = base64Str.replaceAll(" ", "+");
    
    1. js 加密
    imgStr = encodeURIComponent(img);
    

    这个加密后,不用Java是不用decode的,原因是request.getParameter()是会自动解密。

  5. 去除Base64协议类型头

    base64Str = base64Str.split("base64,")[1];
    
  6. 保存图片

    /**
     * base64字符串转化成图片
     * * @Author Mangodai
     * @Date 9/18/2017 6:10 PM
     * @param imgStr
     * @param imgFile
     * @return
     */
    public static boolean GenerateImage(String imgStr, File imgFile) {
        //对字节数组字符串进行Base64解码并生成图片
        if (imgStr == null) //图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            //Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            OutputStream out = new FileOutputStream(imgFile);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }
    

相关文章

网友评论

      本文标题:Echarts 图片保存到服务器 详解

      本文链接:https://www.haomeiwen.com/subject/hsuojxtx.html