发送使用HTTP POST图像 [英] Sending images using Http Post

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

问题描述

我想从Android客户端发送图像,使用HTTP POST Django的服务器。图片选自画廊。在present,我使用列表值名称对从Django的以JSON发送必要的数据到服务器并接收响应。可同样的方法可以用于图像(以URL的嵌入在JSON响应图像)?

I want to send an image from the android client to the Django server using Http Post. The image is chosen from the gallery. At present, I am using list value name Pairs to send the necessary data to the server and receiving responses from Django in JSON. Can the same approach be used for images (with urls for images embedded in JSON responses)?

此外,这是一个较好的方法:远程地访问图像,而不从服务器下载或者下载和存储它们在Bitmap阵列和使用它们本地?图像为数目(小于10),等等。与小尺寸(50 * 50倾角)

Also, which is a better method: accessing images remotely without downloading them from the server or downloading and storing them in a Bitmap array and using them locally? The images are few in number (<10) and small in size (50*50 dip).

任何教程,以解决这些问题将是非常美联社preciated。

Any tutorial to tackle these problems would be much appreciated.

编辑:从库选择的图像被它缩放到所需尺寸后发送给服务器。

The images chosen from the gallery are sent to the server after scaling it to required size.

推荐答案

我会假设你知道你要上传的图片的路径和文件名。这个字符串添加到您的的NameValuePair <​​/ code>使用图片作为密钥名称。

I'm going to assume that you know the path and filename of the image that you want to upload. Add this string to your NameValuePair using image as the key-name.

发送图像可以使用 HttpComponents库来完成。下载最新的HttpClient(目前<一个href="http://apache.cs.uu.nl/dist/httpcomponents/httpclient/binary/httpcomponents-client-4.0.1-bin-with-dependencies.zip">4.0.1)二进制软件包的依赖并复制 Apache的mime4j-0.6.jar httpmime-4.0.1.jar 到项目并将它们添加到您的Java构建路径。

Sending images can be done using the HttpComponents libraries. Download the latest HttpClient (currently 4.0.1) binary with dependencies package and copy apache-mime4j-0.6.jar and httpmime-4.0.1.jar to your project and add them to your Java build path.

您需要将以下导入添加到您的类。

You will need to add the following imports to your class.

import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;

现在,您可以创建一个 MultipartEntity 将图像附加到POST请求。下面code显示了如何做到这一点的例子:

Now you can create a MultipartEntity to attach an image to your POST request. The following code shows an example of how to do this:

public void post(String url, List<NameValuePair> nameValuePairs) {
    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
                // If the key equals to "image", we use FileBody to transfer the data
                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
            } else {
                // Normal string data
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
            }
        }

        httpPost.setEntity(entity);

        HttpResponse response = httpClient.execute(httpPost, localContext);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我希望这可以帮助您在正确的方向了一下。

I hope this helps you a bit in the right direction.

这篇关于发送使用HTTP POST图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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