2012-09-26 32 views
4

Android客户端向服务器发送请求。从HTTP响应正文获取图像文件

HttpClient httpclient = new DefaultHttpClient(); 
HttpPost httppost = new HttpPost("http://test.com/test"); 
HttpResponse response = httpclient.execute(httppost); 

,然后收到服务器的响应类似下面

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
Date: Wed, 26 Sep 2012 10:59:35 GMT 
Content-Type: multipart/form-data; type="image/jpeg"; boundary="simple_boundary"; start="<image>"; start-info="image/jpeg" 
Transfer-Encoding: chunked 

2000 

--simple_boundary 

Content-Type: image/jpeg 
Content-Transfer-Encoding: binary 
Content-ID: <image> 

......JPEG Binary Code Here..... 

--simple_boundary 

我怎样才能从响应图像(二进制)。 (InputStream)也包含边界和内容信息。

--simple_boundary 

Content-Type: image/jpeg 
Content-Transfer-Encoding: binary 
Content-ID: <image> 

......JPEG Binary Code Here..... 

--simple_boundary 

如何获取纯图像二进制数据。 而且有可能?

回答

1
Bitmap bitmap = BitmapFactory.decodeStream((InputStream) response.getEntity().getContent()); 
+0

它不起作用。 InputStream不仅包含二进制数据,还包含内容信息。 –

0

请求内容被拆分和编码,因此处理起来很不容易。

我用Apache Commons FileUpload年前处理这种请求(e.i. multipart),这个库大大简化了这个过程。

getting started部分中,您可以找到几个示例。