1. import sun.misc.BASE64Encoder;
    2. import java.net.HttpURLConnection;
    3. import java.io.InputStream;
    4. import java.io.IOException;
    5. import java.net.URL;
    6. import java.io.ByteArrayOutputStream;
    1. public static String getPictureBase(String fileUrl){
    2. //获取图片流地址
    3. InputStream in = null;
    4. final ByteArrayOutputStream data = new ByteArrayOutputStream();
    5. //读取图片字节数组
    6. try {
    7. URL url = new URL(fileUrl);
    8. final byte[] by = new byte[1024];
    9. // 创建链接获取图片
    10. final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    11. conn.setRequestMethod("GET");
    12. conn.setConnectTimeout(5000);
    13. in = conn.getInputStream();
    14. int len = -1;
    15. while ((len = in.read(by)) != -1) {
    16. data.write(by, 0, len);
    17. }
    18. in.close();
    19. } catch (IOException e) {
    20. e.printStackTrace();
    21. }
    22. //对字节数组Base64编码
    23. BASE64Encoder encoder = new BASE64Encoder();
    24. //返回Base64编码过的字节数组字符串
    25. String encode = encoder.encode(data.toByteArray());
    26. encode = encode.replaceAll("[\\s*\t\n\r]", "");
    27. return encode;
    28. }