如何用Java/Spring将JSON中的字节数组转换为图片?
Java/Spring实现Base64编码图片转JPG/PNG文件
当然可以实现,以下是Java原生和Spring环境下的具体实现方案:
Java原生实现
步骤1:解析JSON提取Base64字符串
使用JSON解析库(如Jackson、Gson)从API返回的JSON数据中提取image字段的Base64内容:
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; // 假设jsonStr是API返回的JSON字符串 ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonStr); String base64Image = jsonNode.get("image").asText();
步骤2:解码Base64为字节数组
若Base64字符串包含data:image/jpeg;base64,这类前缀,需先移除再解码:
import java.util.Base64; // 移除前缀(如果存在) if (base64Image.startsWith("data:image/")) { base64Image = base64Image.split(",")[1]; } byte[] imageBytes = Base64.getDecoder().decode(base64Image);
步骤3:写入为图片文件
用FileOutputStream将字节数组写入本地文件,后缀设为.jpg或.png即可:
import java.io.FileOutputStream; import java.io.IOException; try (FileOutputStream fos = new FileOutputStream("output.jpg")) { fos.write(imageBytes); } catch (IOException e) { e.printStackTrace(); }
Spring环境下的实现
Spring项目中可借助框架工具类简化操作,以下是两种常见场景:
场景1:调用外部API获取JSON并转换
import org.springframework.web.client.RestTemplate; import org.springframework.util.Base64Utils; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.FileOutputStream; RestTemplate restTemplate = new RestTemplate(); // 调用目标API获取JSON字符串 String jsonStr = restTemplate.getForObject("https://your-api-endpoint", String.class); // 解析JSON提取Base64内容 ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(jsonStr); String base64Image = jsonNode.get("image").asText(); // 解码并写入文件 byte[] imageBytes = Base64Utils.decodeFromString(base64Image.replaceFirst("data:image/.*?base64,", "")); try (FileOutputStream fos = new FileOutputStream("output.png")) { fos.write(imageBytes); } catch (IOException e) { throw new RuntimeException("写入图片文件失败", e); }
场景2:接收前端Base64图片并转换
在Controller中接收包含Base64图片的JSON请求,转换为本地图片:
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.util.Base64Utils; import java.io.FileOutputStream; @RestController public class ImageConvertController { @PostMapping("/convert-image") public String convertImage(@RequestBody ImageRequest request) { try { String base64Image = request.getImage(); // 移除Base64前缀 if (base64Image.startsWith("data:image/")) { base64Image = base64Image.split(",")[1]; } byte[] imageBytes = Base64Utils.decodeFromString(base64Image); // 写入为JPG文件 try (FileOutputStream fos = new FileOutputStream("converted_image.jpg")) { fos.write(imageBytes); } return "图片转换成功"; } catch (Exception e) { return "图片转换失败:" + e.getMessage(); } } // 定义接收JSON的实体类 static class ImageRequest { private String image; public String getImage() { return image; } public void setImage(String image) { this.image = image; } } }
注意事项
- 前缀处理:部分API返回的Base64字符串会带格式前缀,必须移除后才能正确解码。
- 格式匹配:保存文件时的后缀要和原图片格式一致(JPG对应
.jpg,PNG对应.png),避免图片无法正常打开。 - 异常处理:实际开发中需完善异常捕获逻辑,处理解码失败、文件写入异常等情况。
内容的提问来源于stack exchange,提问作者Gorostiaga
相关产品推荐
相关产品推荐

