11

试图弄清楚我的编码有什么问题。我从这里关注了一篇博客文章。

我设法获得了将文件实际上传到 PHP Web 服务的代码。然而,出于某种原因,尽管我明确地为文件设置了 MIME 类型,PHP 显示 MIME 只是一个空白字符串,因此被拒绝。

这是我的编码:

public String SendPost(String fn, String bid, String caption, String uid, String APIKey, String postHash) 
        throws ParseException, ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

    HttpPost httppost = new HttpPost(UrbURL);

    Log.i("POSTFN", fn);
    Log.i("POSTFN", bid);
    Log.i("POSTFN", caption);
    Log.i("POSTFN", uid);
    Log.i("POSTFN", APIKey);
    Log.i("POSTFN", postHash);

    String postAuth = uid + postHash;
    postAuth = md5(postAuth);
    postAuth = postAuth.substring(0, 16);
    //Log.i("POSTAUTH", postAuth);

    MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    /*File tempImg = new File(fn);
    FileBody bin = new FileBody(tempImg, "image/jpg");*/
    mp.addPart("business_photo", new FileBody(new File(fn), "image/jpg"));

    //StringBody s = new StringBody(bid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("business_id", new StringBody(bid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(caption, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("photo_caption", new StringBody(caption, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(uid, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("user_id", new StringBody(uid, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("apikey", new StringBody(APIKey, "text/plain", Charset.forName( "UTF-8" )));

    //s = new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" ));
    mp.addPart("auth", new StringBody(postAuth, "text/plain", Charset.forName( "UTF-8" )));

    httppost.setEntity(mp);

    String response = EntityUtils.toString( httpclient.execute( httppost ).getEntity(), "UTF-8" );

    httpclient.getConnectionManager().shutdown();

    return response;
}

非常感谢之前:)

4

6 回答 6

5

我有同样的问题,只是修复了它。

我发现在HttpMultipartMode.BROWSER_COMPATIBLE使用图像时,使用 会阻止在我的请求中设置正确的 mimeType ByteArrayBody。我认为这可能与您遇到的问题相同。

当我改变这一行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity mp = new MultipartEntity();

然后 mime 类型设置正确并且服务上传工作。

我看到人们用它BROWSER_COMPATIBLE来解决另一个问题,但希望你不需要它。

于 2010-10-28T19:21:08.583 回答
2

将 HttpClient 从 4.1 版更新到 4.4 版时,我也遇到了同样的问题。删除HttpMultipartMode.BROWSER_COMPATIBLE 或替换其他替代选项都没有帮助。

对我来说需要最小更改的解决方案是filename在构造时明确指定 a FileBody,即替换

new FileBody(file, mimeType)

new FileBody(file, filename, mimeType, charset)

至于 HttpClient 4.4 版本,原因可能是new FileBody(file, mimeType)分配nullfilename字段而不是使用file.getName(). 然后,在执行formatMultipartHeaderHttpMultipart,如果在模式下filename不为空,则检查将要格式化的部分。BROWSER_COMPATIBLE如果为 null,则忽略整个主体。

于 2015-03-29T21:18:22.057 回答
1

我在发布 3 个 FileBodies 和 1 个 mime 信息时遇到了同样的问题,除非我只发送一个文件体,否则从未收到 mime(以 json 格式)。我总是先添加文件体,这导致了我的问题。我在一开始就通过 addpart mime 颠倒了顺序,并且在 php 服务器端正确接收了包括 mime 在内的每个信息。

于 2011-09-07T21:48:42.603 回答
1

我们这里有同样的问题。使用HttpMultipartMode.BROWSER_COMPATIBLE删除了正在发送的正确 mimetype。

不幸的是,使用“正常”MultipartEntity()不是我们的选择,因为这样 Rails 后端会弄乱请求处理。

有没有办法自己设置多部分的内容类型并使用HttpMultipartMode.BROWSER_COMPATIBLE

于 2010-11-25T11:13:27.587 回答
1

除了包含 jars 文件,您还可以使用在 android 中定义但声明为内部的 http 多部分类,因此您不能直接使用它们,您必须从android 源代码下载它们并将它们添加到您的项目中。在使用它们之前必须进行一些基本的修改,没有什么困难(更改包名称,删除所有记录器调用)。它非常适合我。我希望这可以帮助某人,

于 2011-11-22T18:57:20.653 回答
1

因此,尽管上述所有答案确实有助于正确解决问题。

但只是删除HttpMultipartMode.BROWSER_COMPATIBLE不会解决它。

您可以通过这样做来设置 mime 类型

ByteArrayBody bab = new ByteArrayBody(params.getByteArray("data"), "image/jpeg" , "image.jpg");

注意我正在使用最新的重新打包的 android 客户端http://code.google.com/p/httpclientandroidlib/支持分段上传等。

于 2012-12-12T17:09:13.230 回答