Read images from remote paths for Base64 transcoding

Source: Internet
Author: User

public static byte[] Getimagefromnetbyurl (String strurl) {try {URL url = new URL (strurl);              HttpURLConnection conn = (httpurlconnection) url.openconnection ();              Conn.setrequestmethod ("GET");              Conn.setconnecttimeout (5 * 1000); InputStream instream = Conn.getinputstream ();//Get Picture data by the input stream byte[] btimg = Readinputstream (instream);//Get the image of the binary          System data return btimg;          } catch (Exception e) {e.printstacktrace ();      } return null; }
Since the reading takes a certain amount of time, it cannot be read in a byte array, so it is necessary to determine whether to read public static byte[] Readinputstream (InputStream instream) throws exception{
Store all byte arrays read Bytearrayoutputstream OutStream = new Bytearrayoutputstream (); byte[] buffer = new byte[1024]; int len = 0; while ((Len=instream.read (buffer))! =-1) {outstream.write (buffer, 0, Len); } instream.close (); return Outstream.tobytearray (); } private static string Getimagestr (String imgurl) {byte[] data = null; try {data =getimagefromnetbyurl (imgurl);; } catch (Exception e) {e.printstacktrace (); } Base64encoder encoder = new Base64encoder (); return Encoder.encode (data); }

  

Read images from remote paths for Base64 transcoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.