Java remote get picture generation base64 string

Source: Internet
Author: User

Under the background, the project encountered the front-end JS get picture cross-domain problem, the server does not support anonymous access, only through the server to obtain pictures base64 code to display. The code is as follows: Download

Java code 650) this.width=650; "class=" star "src=" Http://songfeng-123.iteye.com/images/icon_star.png "alt=" Favorites Code "style= "border:0px;"/>

  1. /**

  2. * Remote read image conversion to Base64 string

  3. * @param imgurl

  4. * @return

  5.  */

  6. Private String Image2base64 (String imgurl) {

  7. URL url = null;

  8. InputStream is = null;

  9. Bytearrayoutputstream OutStream = null;

  10. HttpURLConnection Httpurl = null;

  11. Try {

  12. url = new URL (imgurl);

  13. Httpurl = (httpurlconnection) url.openconnection ();

  14. Httpurl.connect ();

  15. Httpurl.getinputstream ();

  16. is = Httpurl.getinputstream ();

  17. OutStream = new bytearrayoutputstream ();

  18. //Create a buffer string

  19. byte [] buffer = new byte[1024x768];

  20. //String length per read, if 1, indicates all read completed

  21. int len = 0;

  22. //Use an input stream to read the data from buffer

  23. while ((len=is.read (buffer))! =-1 ) {

  24. //Use the output stream to write data to buffer, where the intermediate parameters represent where to begin reading, and Len represents the length of the read

  25. Outstream.write (buffer, 0, Len);

  26. }

  27. //byte array Base64 encoding

  28. return New Base64encoder (). Encode (Outstream.tobytearray ());

  29. }catch (Exception e) {

  30. E.printstacktrace ();

  31. } Download

  32. finally {

  33. if (IS! = null)

  34. {

  35. Try {

  36. Is.close ();

  37. } catch (IOException e) {

  38. E.printstacktrace ();

  39. }

  40. }

  41. if (OutStream! = null)

  42. {

  43. Try {

  44. Outstream.close ();

  45. } catch (IOException e) {

  46. E.printstacktrace ();

  47. }

  48. }

  49. if (Httpurl! = null)

  50. {

  51. Httpurl.disconnect ();

  52. }

  53. }

  54. return Imgurl;

  55. }


Java remote get picture generation base64 string

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.