【问题标题】:upload image from android to php server将图像从android上传到php服务器
【发布时间】:2011-10-15 18:19:30
【问题描述】:

我正在尝试将照片从 android 设备上传到 php 网站。 为此,我使用MultipartEntity 编码。

为此,我将added 作为external jar file httpmime-4.1-beta1.jar 加入我的项目。

我要上传到 php 网站的是存储在 SDcard 上的 image。 为此,我在我的代码中执行以下操作:

HttpPost httppost = new HttpPost("....the link to the webiste...");

        MultipartEntity reqEntity = new MultipartEntity();
        StringBody sb=new StringBody("....");


        File file=new File("/sdcard/image.jpg");
        FileBody bin = new FileBody(file);
        reqEntity.addPart("android",bin);
        reqEntity.addPart("id", sb);

        httppost.setEntity(reqEntity);

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity resEntity = response.getEntity();


       if (resEntity != null) {
            String page = EntityUtils.toString(resEntity);
            System.out.println("PAGE :" + page);
        }

但问题在于,来自 php 服务器的响应始终是断开的链接。

接下来我想尝试的是使用

 MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

但不幸的是,我导入的 jar 没有 HttpMultipartMode.BROWSER_COMPATIBLE 的类。因此,如果您能指出正确的方向,我将不胜感激 - 我还应该导入什么才能使其正常工作....或者我应该如何将图像上传到服务器。 我必须说服务器是为这样上传照片而构建的:

method="post" enctype="multipart/form-data"
                      name="form" target="_self" id="form">
                    <input type="hidden" name="p" value="a" />

谢谢!

【问题讨论】:

标签: php android upload image


【解决方案1】:

我建议不要使用beta 库。你应该使用apache-mime4j-0.6

您还需要httpmime-4.0.1

这些必须是您的导入:

      import org.apache.http.HttpResponse;
      import org.apache.http.client.ClientProtocolException;
      import org.apache.http.client.HttpClient;
      import org.apache.http.client.entity.UrlEncodedFormEntity;
      import org.apache.http.client.methods.HttpGet;
      import org.apache.http.client.methods.HttpPost;
      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;
      import org.apache.http.impl.client.DefaultHttpClient;
      import org.apache.http.message.BasicNameValuePair;

【讨论】:

  • 现在它告诉我:org.apache.james.mime4j.message.SingleBody 类型无法解析。它在以下行从所需的 .class 文件中间接引用: MultipartEntity reqEntity = new MultipartEntity();reqEntity.addPart("android",bin); reqEntity.addPart("id", sb);
  • Am importat deja librariile alea, am adaugat zeci de jar uri si tot imi da eroare la linia respectiva!
  • 也导入该类,然后导入 org.apache.james.mime4j.message.SingleBody;
  • Crezi ca o recunoaste?Nu exists clasa respectiva in librarii!:)
猜你喜欢
  • 2011-02-02
  • 2015-03-29
  • 2015-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
相关资源
最近更新 更多