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;"/>
-
/**
-
* Remote read image conversion to Base64 string
-
* @param imgurl
-
* @return
-
*/
-
Private String Image2base64 (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 ();
-
//Create a buffer string
-
byte [] buffer = new byte[1024x768];
-
//String length per read, if 1, indicates all read completed
-
int len = 0;
-
//Use an input stream to read the data from buffer
-
while ((len=is.read (buffer))! =-1 ) {
-
//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
-
Outstream.write (buffer, 0, Len);
-
}
-
//byte array Base64 encoding
-
return New Base64encoder (). Encode (Outstream.tobytearray ());
-
}catch (Exception e) {
-
E.printstacktrace ();
-
} Download
-
finally {
-
if (IS! = null)
-
{
-
Try {
-
Is.close ();
-
} catch (IOException e) {
-
E.printstacktrace ();
-
}
-
}
-
if (OutStream! = null)
-
{
-
Try {
-
Outstream.close ();
-
} catch (IOException e) {
-
E.printstacktrace ();
-
}
-
}
-
if (Httpurl! = null)
-
{
-
Httpurl.disconnect ();
-
}
-
}
-
return Imgurl;
-
}
Java remote get picture generation base64 string