代码如下:
1 package com.futuredata.dataservice.util; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.net.HttpURLConnection; 10 import java.net.URL; 11 12 13 import sun.misc.BASE64Decoder; 14 import sun.misc.BASE64Encoder; 15 16 @SuppressWarnings("restriction") 17 public class Base64Utils { 18 19 public static void main(String[] args) throws Exception { 20 21 //本地图片地址 22 String url = "C:/Users/Administrator/Desktop/628947887489084892.jpg"; 23 //在线图片地址 24 String string = "http://bpic.588ku.com//element_origin_min_pic/17/03/03/7bf4480888f35addcf2ce942701c728a.jpg"; 25 26 String str = Base64Utils.ImageToBase64ByLocal(url); 27 28 String ste = Base64Utils.ImageToBase64ByOnline(string); 29 30 System.out.println(str); 31 32 Base64Utils.Base64ToImage(str, "C:/Users/Administrator/Desktop/test1.jpg"); 33 34 Base64Utils.Base64ToImage(ste, "C:/Users/Administrator/Desktop/test2.jpg"); 35 } 36 37 /** 38 * 本地图片转换成base64字符串 39 * 40 * @param imgFile 图片本地路径 41 * @return 42 * @author ZHANGJL 43 * @dateTime 2018-02-23 14:40:46 44 */ 45 public static String ImageToBase64ByLocal(String imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理 46 47 48 InputStream in = null; 49 byte[] data = null; 50 51 // 读取图片字节数组 52 try { 53 in = new FileInputStream(imgFile); 54 55 data = new byte[in.available()]; 56 in.read(data); 57 in.close(); 58 } catch (IOException e) { 59 e.printStackTrace(); 60 } 61 // 对字节数组Base64编码 62 BASE64Encoder encoder = new BASE64Encoder(); 63 64 return encoder.encode(data);// 返回Base64编码过的字节数组字符串 65 } 66 67 68 /** 69 * 在线图片转换成base64字符串 70 * 71 * @param imgURL 图片线上路径 72 * @return 73 * @author ZHANGJL 74 * @dateTime 2018-02-23 14:43:18 75 */ 76 public static String ImageToBase64ByOnline(String imgURL) { 77 ByteArrayOutputStream data = new ByteArrayOutputStream(); 78 try { 79 // 创建URL 80 URL url = new URL(imgURL); 81 byte[] by = new byte[1024]; 82 // 创建链接 83 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 84 conn.setRequestMethod("GET"); 85 conn.setConnectTimeout(5000); 86 InputStream is = conn.getInputStream(); 87 // 将内容读取内存中 88 int len = -1; 89 while ((len = is.read(by)) != -1) { 90 data.write(by, 0, len); 91 } 92 // 关闭流 93 is.close(); 94 } catch (IOException e) { 95 e.printStackTrace(); 96 } 97 // 对字节数组Base64编码 98 BASE64Encoder encoder = new BASE64Encoder(); 99 return encoder.encode(data.toByteArray()); 100 } 101 102 103 /** 104 * base64字符串转换成图片 105 * 106 * @param imgStr base64字符串 107 * @param imgFilePath 图片存放路径 108 * @return 109 * @author ZHANGJL 110 * @dateTime 2018-02-23 14:42:17 111 */ 112 public static boolean Base64ToImage(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片 113 114 if (isEmpty(imgStr)) // 图像数据为空 115 return false; 116 117 BASE64Decoder decoder = new BASE64Decoder(); 118 try { 119 // Base64解码 120 byte[] b = decoder.decodeBuffer(imgStr); 121 for (int i = 0; i < b.length; ++i) { 122 if (b[i] < 0) {// 调整异常数据 123 b[i] += 256; 124 } 125 } 126 127 OutputStream out = new FileOutputStream(imgFilePath); 128 out.write(b); 129 out.flush(); 130 out.close(); 131 132 return true; 133 } catch (Exception e) { 134 return false; 135 } 136 137 } 138 139 public static boolean isEmpty(String input) { 140 return input == null || input.equals(""); 141 } 142 143 }