Subversion Repositories javautils

Rev

Blame | Last modification | View Log | RSS feed

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