java 从 HTTP 响应正文获取图像文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12600742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:30:36  来源:igfitidea点击:

Get Image file from HTTP Response Body

javaandroidhttpresponsemultipart

提问by Yang-Jae Ahn

Android Client send a request to the server.

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

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

and then received response from server like below

然后从服务器收到如下响应

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

How can I get the Image(binary) from the response.

如何从响应中获取图像(二进制)。

InputStream is = response.getEntity().getContent();

(InputStream)is contains boundary and content information also.

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

--simple_boundary

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

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

--simple_boundary

How can I get pure Image binary data. And it's possible??

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

回答by Richard

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

回答by Víctor Herraiz

The request content is split and encoded, therefore it is far from easy deal with it.

请求内容被拆分和编码,因此处理起来远非易事。

I used Apache Commons FileUploadyears ago to process this kind of request (e.i. multipart), this library simplified the process greatly.

多年前我使用Apache Commons FileUpload来处理这种请求(ei multipart),这个库大大简化了这个过程。

In the getting startedsection you could find several examples.

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