根据图片url直接获取图片base64

从网上看了好多根据图片url获取图片base64,都挺麻烦的

下面这个挺简单的(亲测挺好用)
只需把下面这个工具类copy,(图片url地址)—>返回Base64

package cn.com.zcits.exchange.modules.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUtil {
     
    private static final Logger LOGGER = LoggerFactory.getLogger(FileUtil.class);
    /**
     * @Function: 将一张网络图片转化成Base64字符串
     * @param imgSrc - 网络图片资源位置
     */
    public static String getImageBase64StrFromUrl(String imgSrc) throws Exception{
     
        URL url = null;
        InputStream is = null;
        ByteArrayOutputStream outStream = null;
        HttpURLConnection httpUrl = null;
        try{
     
            url = new URL(imgSrc);
            httpUrl = (HttpURLConnection) url.openConnection();
            httpUrl.setRequestMethod("GET");
            httpUrl.setConnectTimeout(30 * 1000);
            httpUrl.connect();
            httpUrl.getInputStream();
            is = httpUrl.getInputStream();
            outStream = new ByteArrayOutputStream();
            //创建一个Buffer字符串
            byte[] buffer = new byte[2048];
            //每次读取的字符串长度,如果为-1,代表全部读取完毕
            int len = 0;
            //使用一个输入流从buffer里把数据读取出来
            while( (len=is.read(buffer)) != -1 ){
     
                //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度
                outStream.write(buffer, 0, len);
            }
            // 对字节数组Base64编码
            return java.util.Base64.getEncoder().encodeToString(outStream.toByteArray());
        }catch (Exception e) {
     
            throw new Exception(e);
        }finally{
     
            if(is != null) {
     
                try {
     is.close(); } catch (IOException e) {
     e.printStackTrace(); }
            }
            if(outStream != null) {
     
                try {
     outStream.close();} catch (IOException e) {
     e.printStackTrace();}
            }
            if(httpUrl != null){
      httpUrl.disconnect();}
        }
    }

    /**
     *  根据地址获得数据的字节流 - GET请求
     * @param strUrl
     * @return
     */
    public static byte[] getImageFromNetByUrl(String strUrl) throws Exception {
     
         try {
     
            URL url = new URL(strUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(10 * 1000);
            // 通过输入流获取图片数据
            InputStream inStream = conn.getInputStream();
            // 得到图片的二进制数据
            byte[] btImg = readInputStream(inStream);
            return btImg;
        } catch (Exception e) {
     
            e.printStackTrace();
            throw  new Exception(e.getMessage());
        }
    }

    /**
     * @Function: 从输入流中获取数据
     * @param inStream - 输入流
     * @throws IOException
     */
    private static byte[] readInputStream(InputStream inStream) throws IOException {
     
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while ((len = inStream.read(buffer)) != -1) {
     
            outStream.write(buffer, 0, len);
        }
        inStream.close();
        return outStream.toByteArray();
    }
}

你可能感兴趣的:(工具类,Java基础系列,开发必知,java,base64)