http超链接获取图片转base64字符串,把base64字符串保存为本地文件,及读取base64文件
package org.jeecg.common.util; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; public class Base64ImageUtil { /** * 超链接获取图片 转base64字符串 * @param imageUrl 超链接 * @return */ public static String getURLImage(String imageUrl) { String base64String = ""; try { //new一个URL对象 URL url = new URL(imageUrl); //打开链接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置请求方式为"GET" conn.setRequestMethod("GET"); //超时响应时间为5秒 conn.setConnectTimeout(5 * 1000); //通过输入流获取图片数据 InputStream inStream = conn.getInputStream(); //得到图片的二进制数据,以二进制封装得到数据,具有通用性 byte[] data = readInputStream(inStream); BASE64Encoder encode = new BASE64Encoder(); base64String = encode.encode(data); } catch (Exception e) { e.printStackTrace(); } return base64String; } /** * 把base64字符串转换成文件保存到本地 * @param base64String * @param filePath * @param fileName * @throws IOException */ public static void convertBase64ToFile(String base64String, String filePath, String fileName) throws IOException { File file = null; File filedir = new File(filePath); if (!filedir.exists()) { filedir.mkdirs(); } file = new File(filePath + File.separator + fileName); if (file.exists()){ file.delete(); } BASE64Decoder decoder = new BASE64Decoder(); // Base64解码,对字节数组字符串进行Base64解码并生成文件 byte[] byt = decoder.decodeBuffer(base64String); for (int i = 0, len = byt.length; i < len; ++i) { // 调整异常数据 if (byt[i] < 0) { byt[i] += 256; } } OutputStream out = null; InputStream input = new ByteArrayInputStream(byt); try { // 生成指定格式的文件 out = new FileOutputStream(filePath + File.separator + fileName); byte[] buff = new byte[1024]; int len = 0; while ((len = input.read(buff)) != -1) { out.write(buff, 0, len); } } catch (IOException e) { e.printStackTrace(); } finally { out.flush(); out.close(); } } /** * 把文件转为base64字符串 * @param filePath * @return */ public static String readBase64File(String filePath) { String StringBase64 = ""; File file = null; file = new File(filePath); if (!file.exists()){ return StringBase64; } InputStream in = null; byte[] data = null; // 读取文件字节数组 try { in = new FileInputStream(filePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } // 对字节数组Base64编码 BASE64Encoder encoder = new BASE64Encoder(); // 返回 Base64 编码过的字节数组字符串 return encoder.encode(data); } private static byte[] readInputStream(InputStream inStream) throws Exception { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用一个输入流从buffer里把数据读取出来 while ((len = inStream.read(buffer)) != -1) { //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } //关闭输入流 inStream.close(); //把outStream里的数据写入内存 return outStream.toByteArray(); } }
//获取图片转base64字符串 base64String = getURLImage(httppath); //保存到本地 convertBase64ToFile(base64String, filepath, fileName);
读取base64文件为字符串
String imgBase64 = "data:image/jpeg;base64," + readBase64File(filepath);