Android Apache MultipartEntity:无法添加内容类型等额外字段

时间:2013-07-25 13:50:54

标签: android apache multipartentity

我正在尝试使用addField方法向Multipart HTTP POST添加额外字段,但是当我使用WireShark捕获数据包时,我看不到它的效果。可能是什么问题?

private void upload3(File file) {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url + "?recname=" + fileName);
        MultipartEntity entity = new MultipartEntity();

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

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


        try {
            File f = new File(  file_path);

            FileBody body = new FileBody(f);
            FormBodyPart fbp = new FormBodyPart( "file", body );
            fbp.addField("Content-Type", "audio/mp4");

            entity.addPart(fbp);


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

        httpPost.setEntity(entity);
        }

1 个答案:

答案 0 :(得分:2)

好的,我找到了答案。问题是,当你创建一个新的MultipartEntity时,它会生成一个随机边界,但我的服务器正在等待我自己的边界。所以我只需要将MultipartEntity构造函数更改为:

String boundary = "---------------------------Yout own boundary";
MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, boundary, null);