gpt4 book ai didi

android - MultipartEntity:无法为 StringBody 设置 header

转载 作者:行者123 更新时间:2023-11-29 17:55:44 25 4
gpt4 key购买 nike

我想使用多方实体进行文件上传。我想上传一个音频文件和一个 XML 文件。我也想为图像和 XML 部分设置一些标题。我是这样做的:

// adding the audio file
File f = new File(file_path);

FileBody body = new FileBody(f);
FormBodyPart fbp = new FormBodyPart("file", body);
fbp.addField("Content-Disposition", "form-data");
fbp.addField("name", "\"file\"");
fbp.addField("filename", "\"" + fileName + "\"");
fbp.addField("Content-Type", "audio/mp4");
fbp.addField("Content-Transfer-Encoding", "binary");

entity.addPart(fbp);

// adding the XML file
String xml= createAudioInfoXML("test", 6, (int) file.length());
StringBody strBody = new StringBody(xml);
FormBodyPart fbp2 = new FormBodyPart("file2",strBody);
fbp2.addField("Content-Disposition", "form-data");
fbp2.addField("name", "\"file2\"");
fbp2.addField("filename", "\"" + fileName + "\"");
fbp2.addField("Content-Type", "text/xml");
fbp2.addField("Content-Transfer-Encoding", "binary");

entity.addPart(fbp2);

我设置了相同的 header ,但是,如果我在 wireshark 中检查 POST 请求,我会得到这个(例如,没有带有 xml 的 Content-Type header ,但是我设置了它):

POST /upload?recname=2d53352d-c840-48c7-b314-0fc324561ca9 HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------------This is the boundary
Content-Length: 25059
Host: notes.verba.com
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)

-----------------------------This is the boundary
Content-Disposition: form-data; name="file"; filename="1382452301277.wav"
Content-Type: application/octet-stream

... some data...

-----------------------------This is the boundary
Content-Disposition: form-data; name="file2"

<?xml version="1.0">
<rec>
<name>test</name>
<length>6</length>
<size>24620</size>
<date>2013.10.15. 16:42</date>
</rec>
-----------------------------This is the boundary--

这是我的完整功能:

private void upload3(File file) {

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url + "?recname=" + guid);

String boundary = "---------------------------This is the boundary";

MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE, boundary, null);

httpPost.addHeader("Content-Type", "multipart/form-data; boundary="
+ boundary);

try {

// adding the audio file
File f = new File(file_path);

FileBody body = new FileBody(f);
FormBodyPart fbp = new FormBodyPart("file", body);
fbp.addField("Content-Disposition", "form-data");
fbp.addField("name", "\"file\"");
fbp.addField("filename", "\"" + fileName + "\"");
fbp.addField("Content-Type", "audio/mp4");
fbp.addField("Content-Transfer-Encoding", "binary");

entity.addPart(fbp);

// adding the XML file
String xml= createAudioInfoXML("test", 6, (int) file.length());

//entity.addPart("xml", new StringBody(xml,"application/xml",Charset.forName("UTF-8")));

StringBody strBody = new StringBody(xml);
FormBodyPart fbp2 = new FormBodyPart("file2",strBody);
fbp2.addField("Content-Disposition", "form-data");
fbp2.addField("name", "\"file2\"");
fbp2.addField("filename", "\"" + fileName + "\"");
fbp2.addField("Content-Type", "text/xml");
fbp2.addField("Content-Transfer-Encoding", "binary");

entity.addPart(fbp2);


} catch (Exception e) {
e.printStackTrace();
}

httpPost.setEntity(entity);

try {

HttpResponse response = httpclient.execute(httpPost);

BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
StringBuilder builder = new StringBuilder();

for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}

reader.close();
message = builder.toString();

} catch (ClientProtocolException e) {
e.printStackTrace();
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Error during the upload.",
Toast.LENGTH_LONG).show();

}
});
} catch (IOException e) {
e.printStackTrace();
activity.runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(activity, "Error during the upload.",
Toast.LENGTH_LONG).show();

}
});
}
}

最佳答案

对于遇到此问题的人,解决方案是更改 HttpMultipartMode.BROWSER_COMPATIBLE。因为这行代码只是把Content-Disposition和Content-Type放到了消息中。

关于android - MultipartEntity:无法为 StringBody 设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19605103/

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