public static String readInfoStcreamToBytes(InputStream is) { if (is == null) { return null; } // 字节数组 byte[] bCache = new byte[2048]; int readSize = 0;// 每次读取的字节长度 int totalSize = 0;// 总字节长度 ByteArrayOutputStream infoStream = new ByteArrayOutputStream(); try { // 一次性读取2048字节 while ((readSize = is.read(bCache)) > 0) { totalSize += readSize; if (totalSize > 1024*1024) { return null; } // 将bcache中读取的input数据写入infoStream infoStream.write(bCache, 0, readSize); } } catch (IOException e1) { return null; } finally { try { is.close(); } catch (IOException e) { } } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(infoStream.toByteArray()); } public static void main(String []args) throws IOException { try { URL url=new URL("http://p8.qhimg.com/t0195b7243f682baef3.png"); System.out.println(readInfoStcreamToBytes(url.openStream())); } catch (MalformedURLException e) { e.printStackTrace(); }
图片下载转base64
最新推荐文章于 2023-12-15 17:04:21 发布