Estou desenvolvendo uma aplicação mobile que enviar fotos do aplicativo para o servidor, feito em springboot. Ao receber a requisição com a imagem, via json, tento fazer a conversão de String para ImageIO. Porem, na conversão ele retorna null. Por algum motivo a conversão nao esta acontecendo e nao salvo a imagem.
@PostMapping(value = "/paciente")
public void paciente(@RequestBody String foto) throws IOException {
try {
byte[] encoded = Base64.encodeBase64(foto.getBytes());
System.out.println("Encoded retorna >" + encoded)
ByteArrayInputStream input = new ByteArrayInputStream(encoded);
System.out.println("URL retorna >" + input);
BufferedImage image = ImageIO.read(input);
System.out.println("IMAGE retorna >" + image);
ImageIO.write(image, "jpg", new File("C:/Users/carlo/Downloads/SisCAF/images/myImage1.jpg"));
System.out.println("image created");
} catch (Exception e) {
}
}
A saída: Encoded retorna >[B@7646546e URL retorna >java.io.ByteArrayInputStream@1b7a9dc1 IMAGE retorna >null
Base64.encodeBase64
está transformando essa foto em base64. tenta utilizar assim:byte[] encoded = Base64.decode(foto);