1 30 import java.io.File ; 31 import java.io.FileInputStream ; 32 import org.apache.commons.httpclient.HttpClient; 33 import org.apache.commons.httpclient.HttpStatus; 34 import org.apache.commons.httpclient.methods.PostMethod; 35 36 41 public class UnbufferedPost { 42 43 public static void main(String [] args) throws Exception { 44 if (args.length != 1) { 45 System.out.println("Usage: ChunkEncodedPost <file>"); 46 System.out.println("<file> - full path to a file to be posted"); 47 System.exit(1); 48 } 49 HttpClient client = new HttpClient(); 50 51 PostMethod httppost = new PostMethod("http://localhost:8080/httpclienttest/body"); 52 53 File file = new File (args[0]); 54 httppost.setRequestBody(new FileInputStream (file)); 55 httppost.setRequestContentLength((int)file.length()); 56 57 client.executeMethod(httppost); 58 59 if (httppost.getStatusCode() == HttpStatus.SC_OK) { 60 System.out.println(httppost.getResponseBodyAsString()); 61 } else { 62 System.out.println("Unexpected failure: " + httppost.getStatusLine().toString()); 63 } 64 httppost.releaseConnection(); 65 } 66 } 67 | Popular Tags |