1

我想用多方实体进行文件上传。我想上传一个音频文件和一个 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的Content-Type标头,但是我设置了它):

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();

                }
            });
        }
    }
4

1 回答 1

3

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

于 2013-10-28T19:58:35.530 回答