我想使用多方实体进行文件上传。我想上传音频文件和 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);
我设置了相同的标头,但是,如果我在wireshark中检查POST请求,我会得到这个(例如,没有带有xml的内容类型标头,但是我设置了它):
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。因为这行代码只将内容处置和内容类型放在消息中。