BaseUtIis工具类

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

}
 

 

posted @ 2019-09-10 09:41  这很周锐  阅读(179)  评论(0编辑  收藏  举报