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.HttpClients;
37  import org.apache.hc.core5.http.ContentType;
38  import org.apache.hc.core5.http.HttpEntity;
39  import org.apache.hc.core5.http.message.StatusLine;
40  
41  /**
42   * Example how to use multipart/form encoded POST request.
43   */
44  public class ClientMultipartFormPost {
45  
46      public static void main(final String[] args) throws Exception {
47          if (args.length != 1) {
48              System.out.println("File path not given");
49              System.exit(1);
50          }
51          try (final CloseableHttpClient httpclient = HttpClients.createDefault()) {
52              final HttpPost httppost = new HttpPost("http://httpbin.org/post");
53  
54              final FileBody bin = new FileBody(new File(args[0]));
55              final StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);
56  
57              final HttpEntity reqEntity = MultipartEntityBuilder.create()
58                      .addPart("bin", bin)
59                      .addPart("comment", comment)
60                      .build();
61  
62  
63              httppost.setEntity(reqEntity);
64  
65              System.out.println("executing request " + httppost);
66              httpclient.execute(httppost, response -> {
67                  System.out.println("----------------------------------------");
68                  System.out.println(httppost + "->" + new StatusLine(response));
69                  final HttpEntity resEntity = response.getEntity();
70                  if (resEntity != null) {
71                      resEntity.writeTo(System.out);
72                  }
73                  System.out.flush();
74                  return null;
75              });
76          }
77      }
78  
79  }