public String GetImageBase64FromUrl(String imgURL) {
        byte[] data = null;
        String result = null;
        BufferedImage image = null;
        try {
            // 创建URL
            URL url = new URL(imgURL);

            image = ImageIO.read(url);

//            ImageIO.write(image, "jpg", new File("D:\\1.jpg"));

            // bufferImage->base64
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", outputStream);
            BASE64Encoder encoder = new BASE64Encoder();
            result = encoder.encode(outputStream.toByteArray());
            result = result.replaceAll("\r|\n", "");

            /*BASE64Decoder decoder = new BASE64Decoder();
                //Base64解码
           byte[] b = decoder.decodeBuffer(result);
                for(int i=0;i<b.length;++i)
                {
                    if(b[i]<0)
                    {//调整异常数据
                        b[i]+=256;
                    }
                }
                //生成jpeg图片
                String imgFilePath = "d://222.jpg";//新生成的图片
                OutputStream out = new FileOutputStream(imgFilePath);
                out.write(b);
                out.flush();
                out.close();*/
            } catch (Exception e) {
            LogUtils.logException(e);
        }
        return result;
    }

 

相关文章:

  • 2022-01-22
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-02-27
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-04
  • 2021-11-21
  • 2022-12-23
相关资源
相似解决方案