将HTTPResponse转换为图像



对于其中一个实现,从Java代码中,我正在使用labelary Rest服务将ZPL格式的代码转换为Image。我能够成功获取响应。但是我无法将HttpResponse转换为图像文件。

HttpClient client = new DefaultHttpClient(); 
HttpPost post = new HttpPost "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/"); 
byte[] byteArray = Base64.decodeBase64(base64Val.getBytes());
String decodedString = new String(byteArray);
StringEntity requestEntity;
try {
requestEntity = new StringEntity(decodedString);
requestEntity.setContentType("application/x-www-form-urlencoded");
post.setEntity(requestEntity);
HttpResponse response = client.execute(post);
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
#Need suggestions to convert BufferReader to Image
}

引用建议答案后,代码看起来像:

HttpResponse response = client.execute(post); InputStream inStream = response.getEntity().getContent(); String dataString = convertStreamToString(inStream); byte[] imageBytes = javax.xml.bind.DatatypeConverter.parseBase64Binary(dataString); BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageBytes)); File outputfile = new File("myImage.png"); ImageIO.write(image, "png", outputfile);

使用这个字节到图像转换器。过去的值,并检查您是否获得了图像。

相关内容

  • 没有找到相关文章

最新更新