网络图片url转为base64数据


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;
    }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM