hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roland Weber <ossf...@dubioso.net>
Subject Re: Uploading a File using Http clien
Date Wed, 19 Sep 2007 17:54:23 GMT
bhadu wrote:
> Look at my code...............First servlet...........
> 
> 
>            String urlString = "http://localhost:8787//testone//MyServlet";
> 		
> 	   		
> 			HttpClient client = new HttpClient();
> 	        PostMethod postMethod = new PostMethod(urlString);
> 	        post(postMethod);

So here you are setting the entity of the POST method
to a multipart entity.

> 	        client.getParams().setParameter("http.useragent", "My Browser");
> 	        postMethod.addParameter("test","testvalue");
> 	        NameValuePair[] nm = {new NameValuePair("test","test")};
> 	        postMethod.setRequestBody(nm);

And here you replace that with "test=test".

An entity _is_ the request body. You can have only one.

cheers,
  Roland

> 	        int statusCode1 = client.executeMethod(postMethod);
> 	        System.out.println(" status >>> "+statusCode1);
> 	        System.out.println("statusLine>>>" + postMethod.getStatusLine());
> 	        postMethod.releaseConnection();
> 	        
> 
> 	}
> 
> 	private void post(PostMethod postMethod) throws FileNotFoundException
> 	{
> 		 File f = new File("C:\\error.txt");
> 		 System.out.println("file exit = "+f.exists());
> 		Part[] parts = {
>                 new FilePart(f.getName(), f)
>             };
> 	
> postMethod.addRequestHeader("Content-type","multipart/form-data;boundary="+f.length());
> 		postMethod.addRequestHeader("content-disposition","form-data;
> name="+f.getName());
> 		postMethod.setRequestEntity(
>                 new MultipartRequestEntity(parts, postMethod.getParams())
>                 );
> 	}
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: httpclient-user-help@jakarta.apache.org


Mime
View raw message