如何通过HTTPPost发送图像? [英] How to send an image through HTTPPost?

查看:136
本文介绍了如何通过HTTPPost发送图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建,需​​要使用他们的API的图像上传到一个web服务的活动。

i'm creating an activity which needs to upload an image to a webservice using their api.

我发现,如果我使用UrlEn codedFormEntity,并通过发送图像数据。 web服务没有收到。 (至少它不会能够读取。)

I found that if i use UrlEncodedFormEntity and send the image data through that. the webservice doesn't receive that. ( at least it will not be able to read that .)

事实上,如果我增加一些增值经销商与图像数据发送(如文件名,文件大小),它们可以从Web服务来读取,但图像还是数据,如果我尝试读它的服务器端不会出现。

In fact if i add some vars to send with the image data ( like name of the file, filesize ) they can be read from the webservice but the image data still doesn't appear if i try to read it serverside.

现在,我使用带有BasicNameValuePair UrlEn codedFormEntity作为容器,我的数据。

Right now i'm using UrlEncodedFormEntity with BasicNameValuePair as container for my data.

推荐答案

也许它会帮助你。 我已经使用了相同的功能,但Web服务是由我开发。我已经使用了下面的帖子图片:

May be it will help you. I've used the same functionality, but the web service was developed by me. I've post the image using the following:

  1. 我得到的特殊位图图标和COM preSS就在这样的字节数组:

  1. I get the particular Bitmap icon and compress it in byte array like this:

ByteArrayOutputStream out = new ByteArrayOutputStream(10240);
icon.compress(CompressFormat.PNG, 100, out);

  • 然后创建HttpPost并设置实体。

  • Then create HttpPost and set the the entity.

    httpPostInstance.setEntity(new ByteArrayEntity(out.toByteArray()));
    

  • 选中内容类型标头。你必须正确地将其设置为任何你的服务期望。

  • Check the "Content-type" header. You must properly set it to whatever your service expect.

    这篇关于如何通过HTTPPost发送图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆