java - 将作为图像 URL 的字符串转换为字节数组
问题描述
我有一个来自 Spring RestTemplateget
结果的图像内容:
String url = "https://is2-ssl.mzstatic.com/image/" +
"thumb/Purple114/v4/15/a1/68/15a1681f-dec4-b01f-4362" +
"-e9ff1ece9c09/AppIcon-1x_U007emarketing-0-10-0-0-85" +
"-220-0.png/60x60bb.png";
String imageAsString = restTemplate.getForObject(url, String.class);
我知道这不是通过RestTemplate
. 但我应该为我的旧代码保留它。
如何将此值转换为正确的图像字节数组格式?
imageAsString.getBytes()
-> 这不一样restTemplate.getForObject(url, byte[].class);
解决方案
如果你有一个 image URL
,你可以先把read
它变成 a BufferedImage
,然后再把write
它变成 a FileOutputStream
,如下所示:
public static void main(String[] args) throws IOException {
URL url = new URL("https://is2-ssl.mzstatic.com/image/" +
"thumb/Purple114/v4/15/a1/68/15a1681f-dec4-b01f-4362" +
"-e9ff1ece9c09/AppIcon-1x_U007emarketing-0-10-0-0-85" +
"-220-0.png/60x60bb.png");
BufferedImage bufferedImage = ImageIO.read(url);
ImageIO.write(bufferedImage, "png",
new FileOutputStream("resources/bufferedImage.png"));
}
这是图像:bufferedImage.png
推荐阅读
- python - For循环没有使用pandas DataFrame给出预期的输出
- python - 在 tensorflow 中实现简单的线性代数运算
- facebook - 现在为 facebook 在 maltego 中转换显示
- java - 为什么我在实体 A 的仓库中得到 UnsatisfiedDependencyException 而不是在实体 B 中得到 UnsatisfiedDependencyException,而它们之间几乎没有区别?
- android - app_release.apk 的 Google oauth 中的问题,app_debug.apk 工作正常
- android - 如何将不同大小的列表项作为类参数?
- python - 编码和解码字符串('latin-1')并解析为 json
- flutter - Flutter 升级命令:Flutter 升级失败,退出代码 = 1
- c++ - 具有定义的朋友功能 - 模板或非模板?
- r - pivot_wider 问题“`values_from` 中的值不是唯一标识的;输出将包含 list-cols”