【问题标题】:Android: binary photo not sent while using multipartentityAndroid:使用多方时未发送二进制照片
【发布时间】:2014-01-29 07:49:38
【问题描述】:

这是我编写的代码,用于将二进制文件与一些字符串一起发送到 PHP Web 应用程序服务器。

public void doRegister(final String userEmail, final String userPass, final File userPhotoId) {
    HttpClient myHttpClient = new DefaultHttpClient();
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                HttpPost registerTry = new HttpPost(Constants.oAuthRegURL);
                MultipartEntity signupEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                signupEntity.addPart("email", new StringBody(userEmail, Charset.forName("UTF-8")));
                signupEntity.addPart("password", new StringBody(userPass, Charset.forName("UTF-8")));
                signupEntity.addPart("User[profilePicture]", new FileBody(userPhotoId, "image/jpeg"));
                registerTry.setEntity(signupEntity);
                HttpResponse signupHttpResp = myHttpClient.execute(registerTry);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }).start();
}

文件 userPhotoId 是通过相机 API 创建和返回的照片文件(我在 onActivityResult 中获取通过相机返回的数据并从中创建位图,然后从该位图创建文件对象等。 )

所以这里的问题是,照片​​不是以这种方式发送到服务器的。但是,如果我像这样删除 FileBody 部分中的 mimetype

signupEntity.addPart("User[profilePicture]", new FileBody(userPhotoId));

二进制文件/照片已正确发送。但由于 Web 应用程序需要安全性来检查传入文件以防止二进制恶意软件,我需要为实体设置 mimeType。那么谁能告诉我为什么在实体请求中使用 mimeType 时没有发送文件?

附言 我在项目的库中导入了 httpclient-4.3.2 、 httpmime-4.3.2 、 httpcore-4.3.1 并使用 SDK 19 进行编译。

【问题讨论】:

    标签: android httpclient multipartentity


    【解决方案1】:

    好吧,我只是想通了;在 MultipartEntityBuilder 中编写了整个内容;所以关于发送二进制照片的行将是:

    signupEntity.addBinaryBody("User[profilePicture]", userPhotoId, ContentType.create("image/jpeg"), ".profile.jpg");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 1970-01-01
      • 2013-03-07
      • 2015-05-24
      • 2021-09-26
      • 2016-10-17
      • 1970-01-01
      相关资源
      最近更新 更多