代码如下:
@GetMapping("getImage")
public void getImage(HttpServletResponse response) throws IOException {
String path = "你的图片网络地址";
URL url = new URL(path);
InputStream in = url.openStream();
byte[] buff = new byte[in.available()];
int bytesRead = 0;
ByteArrayOutputStream bao = new ByteArrayOutputStream();
while ((bytesRead = in.read(buff)) != -1) {
bao.write(buff, 0, bytesRead);
}
in.close();
byte[] data = bao.toByteArray();
ServletOutputStream out = null;
ByteArrayInputStream ips = new ByteArrayInputStream(data);
try {
out = response.getOutputStream();
//读取文件流
int len = 0;
byte[] buffer = new byte[ips.available()];
while ((len = ips.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
out.flush();
} catch (Exception e) {
logger.info("失败了");
} finally {
out.close();
ips.close();
}
}
结束。