public class Base64Util { /** * 图片URL转Base64编码 * @param imgUrl 图片URL * @return Base64编码 */ public static String imageUrlToBase64(String imgUrl) { URL url = null; InputStream is = null; ByteArrayOutputStream outStream = null; HttpURLConnection httpUrl = null; try { url = new URL(imgUrl); httpUrl = (HttpURLConnection) url.openConnection(); httpUrl.connect(); httpUrl.getInputStream(); is = httpUrl.getInputStream(); outStream = new ByteArrayOutputStream(); //创建一个Buffer字符串 byte[] buffer = new byte[1024]; //每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0; //使用输入流从buffer里把数据读取出来 while( (len = is.read(buffer)) != -1 ){ //用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len); } // 对字节数组Base64编码 return encode(outStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); } finally { try { if(is != null) { is.close(); } if(outStream != null) { outStream.close(); } if(httpUrl != null) { httpUrl.disconnect(); } } catch (Exception e) { e.printStackTrace(); } } return null; } /** * 图片流转Base64 * @param image 图片Buffer * @return Base64编码 */ public static String encode(byte[] image){ BASE64Encoder decoder = new BASE64Encoder(); return replaceEnter(decoder.encode(image)); } /** * 本地路径转Base64 */ public static String convertFileToBase64(String imgPath) { byte[] data = null; // 读取图片字节数组 try { InputStream in = new FileInputStream(imgPath); System.out.println("文件大小(字节)="+in.available()); data = new byte[in.available()]; in.read(data); in.close(); } catch (IOException e) { e.printStackTrace(); } // 对字节数组进行Base64编码,得到Base64编码的字符串 BASE64Encoder encoder = new BASE64Encoder(); String base64Str = encoder.encode(data); return base64Str; } /** * Base64压缩 */ public static String resizeImageTo40K(String base64Img) { try{ BASE64Decoder decoder = new BASE64Decoder(); byte[] bytes1 = decoder.decodeBuffer(base64Img); InputStream stream = new ByteArrayInputStream(bytes1); BufferedImage src = ImageIO.read(stream); BufferedImage output = Thumbnails.of(src).size(src.getWidth() / 3, src.getHeight() / 3).asBufferedImage(); String base64 = imageToBase64(output); if (base64.length() - base64.length() / 8 * 2 > 40000) { output = Thumbnails.of(output).scale(1 / (base64.length() / 40000)).asBufferedImage(); base64 = imageToBase64(output); } return base64; } catch (Exception e) { return base64Img; } } // BufferedImage转换成base64,在这里需要设置图片格式,如下是jpg格式图片: public static String imageToBase64(BufferedImage bufferedImage) { Base64 encoder = new Base64(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImage, "jpg", baos); } catch (IOException e) { } return new String(encoder.encode((baos.toByteArray()))); } /** * Base64转图片 */ public static byte[] decode(String base64Str){ byte[] b = null; BASE64Decoder decoder = new BASE64Decoder(); try { b = decoder.decodeBuffer(replaceEnter(base64Str)); } catch (IOException e) { e.printStackTrace(); } return b; } /** * 字符替换 * @param str 字符串 * @return 替换后的字符串 */ public static String replaceEnter(String str){ String reg ="[\n-\r]"; Pattern p = Pattern.compile(reg); Matcher m = p.matcher(str); return m.replaceAll(""); } }
Base64通用工具类整理
于 2023-04-26 15:00:55 首次发布