前言
实现方式:通过图片URL获取二进制流,再对字节数组进行Base64编码转换
@RestController
@RequestMapping("/mobile")
public class ImageController {
@GetMapping("/getImage")
public static String getImage(@RequestParam("url") String imageUrl) throws IOException {
URL url = new URL(imageUrl);
InputStream is = url.openStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = bis.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
byte[] bytes = baos.toByteArray();
String base64String = Base64.getEncoder().encodeToString(bytes);
return "data:image/png;base64," + base64String;
}
}
转换之后 拿结果集 直接在网站里输入 Base64编码结果 就能直接访问图片,