获取图片链接返回字节流至前端
public void getPhoto(HttpServletRequest request, HttpServletResponse response ,String photo) throws WxErrorException, IOException {
ServletOutputStream out = null;
BufferedInputStream buf = null;
String path = “”;
URL url = null;
try {
url = new URL(photo);
URLConnection con = url.openConnection();
con.setConnectTimeout(5*1000);
InputStream inputStream = con.getInputStream();
buf = new BufferedInputStream(inputStream);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while (-1 != (length = buf.read(buffer,0,buffer.length))){
out.write(buffer,0,length);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(“static/photoFaile.png”);
buf = new BufferedInputStream(inputStream);
out = response.getOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while (-1 != (length = buf.read(buffer,0,buffer.length))){
out.write(buffer,0,length);
}
out.flush();
} finally {
if (buf != null) buf.close();
if (out != null) out.close();
}
}
未保存到本地 ,