java- apache http客户端查询有关提交文件作为多部分发布请求的一部分

时间:2012-01-28 15:11:58

标签: java apache-httpcomponents

我正在尝试使用最新的Apache HTTP Client(v4.x)发送多部分POST请求 - 随文档提供的示例代码提供以下代码示例(稍作修改)以发出POST请求 - < / p>

        FileBody bin = new FileBody(new File(args[0]));
        StringBody comment = new StringBody("A binary file of some kind");
        MultipartEntity reqEntity = new MultipartEntity();
        reqEntity.addPart("bin", bin);
        reqEntity.addPart("comment", comment);
        httppost.setEntity(reqEntity);

我感到困惑的是,如果我要添加多个文件,那么在代码中

   reqEntity.addPart("bin", bin);

第一个字符串代表什么?它是作为多部分帖子的一部分发送的文件的名称吗?

1 个答案:

答案 0 :(得分:1)

多部分 表单 请求可以有多个部分,每个部分都有一个名称(类似于常规表单请求)。在给定名称的情况下,可以在服务器端使用此名称来检索特定部件。 RFC 2388中提供了详细信息:

    3. Definition of multipart/form-data

       The media-type multipart/form-data follows the rules of all multipart
       MIME data streams as outlined in [RFC 2046].  In forms, there are a
       series of fields to be supplied by the user who fills out the form.
       Each field has a name. Within a given form, the names are unique.

       "multipart/form-data" contains a series of parts. Each part is
       expected to contain a content-disposition header [RFC 2183] where the
       disposition type is "form-data", and where the disposition contains
       an (additional) parameter of "name", where the value of that
       parameter is the original field name in the form. For example, a part
       might contain a header:

            Content-Disposition: form-data; name="user"