gpt4 book ai didi

Android Multipart HTTP Post 不发送文件的 MIME 类型

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:03:53 24 4
gpt4 key购买 nike

试图弄清楚我的编码有什么问题。我关注了来自 here 的博客文章.

我设法获得了将文件实际上传到 PHP 网络服务的代码。然而,出于某种原因,虽然我已经为文件明确设置了 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;
}

非常感谢:)

最佳答案

我遇到了同样的问题,刚刚解决了它。

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

当我改变这一行时:

MultipartEntity mp = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

MultipartEntity mp = new MultipartEntity();

然后 mime 类型设置正确,服务上传成功。

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

关于Android Multipart HTTP Post 不发送文件的 MIME 类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3482674/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com