Question

I want to do a file upload with multiparty entity. I want to upload an audio file and an XML file. I want to set some headers to the image and to the XML part too. this is how I do it:

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

I set the same headers, however, if I check the POST request in wireshark, I get this (There is no Content-Type header with xml for example, however I set it):

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--

Here is my complete function:

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

                }
            });
        }
    }
Was it helpful?

Solution

For whoever struggles with this, the solution was to change HttpMultipartMode.BROWSER_COMPATIBLE. Because this line of code only puts the Content-Disposition and Content-Type in the message.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top