View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.examples;
28  
29  import java.io.File;
30  
31  import org.apache.hc.client5.http.classic.methods.HttpPost;
32  import org.apache.hc.client5.http.entity.mime.FileBody;
33  import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder;
34  import org.apache.hc.client5.http.entity.mime.StringBody;
35  import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
36  import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
37  import org.apache.hc.client5.http.impl.classic.HttpClients;
38  import org.apache.hc.core5.http.ContentType;
39  import org.apache.hc.core5.http.HttpEntity;
40  import org.apache.hc.core5.http.io.entity.EntityUtils;
41  
42  /**
43   * Example how to use multipart/form encoded POST request.
44   */
45  public class ClientMultipartFormPost {
46  
47      public static void main(final String[] args) throws Exception {
48          if (args.length != 1)  {
49              System.out.println("File path not given");
50              System.exit(1);
51          }
52          try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
53              final HttpPost httppost = new HttpPost("http://localhost:8080" +
54                      "/servlets-examples/servlet/RequestInfoExample");
55  
56              final FileBody bin = new FileBody(new File(args[0]));
57              final StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
58  
59              final HttpEntity reqEntity = MultipartEntityBuilder.create()
60                      .addPart("bin", bin)
61                      .addPart("comment", comment)
62                      .build();
63  
64  
65              httppost.setEntity(reqEntity);
66  
67              System.out.println("executing request " + httppost);
68              try (final CloseableHttpResponse response = httpclient.execute(httppost)) {
69                  System.out.println("----------------------------------------");
70                  System.out.println(response);
71                  final HttpEntity resEntity = response.getEntity();
72                  if (resEntity != null) {
73                      System.out.println("Response content length: " + resEntity.getContentLength());
74                  }
75                  EntityUtils.consume(resEntity);
76              }
77          }
78      }
79  
80  }