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

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

问题描述

我想使用Http Post将图像从Android客户端发送到Django服务器。图像从画廊中选择。目前,我正在使用列表值对,将必要的数据发送到服务器,并在JSON中接收来自Django的响应。图像可以使用相同的方法(使用嵌入在JSON响应中的图像的URL)?

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)?

另外这是一个更好的方法:远程访问图像而不从服务器下载图像,或将它们下载并存储在位图数组中并在本地使用它们?图像数量少(<10),体积小(50 * 50 dip)。

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).

任何解决这些问题的教程都将非常感谢。

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.

推荐答案

我将假设您知道要上传的图像的路径和文件名。使用 image 作为键名,将此字符串添加到您的 NameValuePair 中。

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(目前 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请求的映像。以下代码显示了如何执行此操作的示例:

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天全站免登陆