/**
* 网址获取图片base64
* @param imgUrl
* @return
*/
public static String getUrlToString(String imgUrl){
byte[] result=null
InputStream inStream=null
String photo = ""
try {
//创建URL
URL url=new URL(imgUrl)
//创建连接
HttpURLConnection conn=(HttpURLConnection) url.openConnection()
conn.setRequestMethod("GET")
conn.setConnectTimeout(5*1000)
inStream=conn.getInputStream()
int count=conn.getContentLength()
result=new byte[count]
int readCount=0
while(readCount<count){//循环到图片读取完整
readCount+=inStream.read(result,readCount,count-readCount)
}
photo = toBase64(result)
} catch (Exception e) {
e.printStackTrace()
logger.error("获取远程图片失败", e)
} finally {
try {
if (inStream != null) {
inStream.close()
}
} catch (IOException e) {
logger.error("文件处理错误!",e)
}
logger.info("o==||=====>读取图片返回");
}
return photo;
}
private static String toBase64(byte[] imgData) {
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(imgData);
}