从HTTP响应体中获取图像文件

5

安卓客户端向服务器发送请求。

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 is = response.getEntity().getContent();

(InputStream)is 包含边界和内容信息。

在IT技术中使用。请注意,保留HTML标记但不返回解释。
--simple_boundary

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

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

--simple_boundary

我该如何获取纯图像二进制数据?这是否可能?
3个回答

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

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

0

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

我多年前使用 Apache Commons FileUpload 处理这种类型的请求(即多部分请求),这个库大大简化了整个过程。

入门指南 部分,您可以找到几个示例。



网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接