有时前台根据路径不能显示图片 因为他可能访问不到后台url路径 需要我们转一下处理显示图片
//根据fileUrl转base64码
public String urlToBase(String fileUrl) throws Exception{
String baseStr = null;
ByteArrayOutputStream data = new ByteArrayOutputStream();
byte[] buff = new byte[2048];
InputStream inputStream = null;
URL url = null;
HttpURLConnection conn = null;
url = new URL(fileUrl);
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.connect();
inputStream = conn.getInputStream();
int num = -1;
while ((num = inputStream.read(buff)) != -1) {
data.write(buff, 0, num);
}
BASE64Encoder encoder = new BASE64Encoder();
baseStr = encoder.encode(data.toByteArray());
return baseStr;
}
fileUrl : "http: //xxxx. png? sgdndjhdnd"
转完base64 : “ivhdhdjdgdjGxdjhdh.”