commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Becke <be...@u.washington.edu>
Subject Re: Using PutMethod to upload a stream via http to a url
Date Thu, 16 Oct 2003 04:03:05 GMT
Hi Toby,

The problem is that the PutMethod doesn't have any content.  Content  
must be set prior to calling client.executeMethod(), using either  
setRequestBody(String) or setRequestBody(InputStream).  If you will be  
generating content dynamically you will need to either buffer it to  
disk/memory and then send it, or use a PipedInputStream  
<http://java.sun.com/j2se/1.4.1/docs/api/java/io/PipedInputStream.html>.

Mike

On Wednesday, October 15, 2003, at 09:39 PM, Toby Williamson wrote:

> I am trying to write a file to a url, using http put.
>
> I don't want to upload an existing file from my filesystem, rather put  
> a
> string (actually several, that could be very large and are of unknown
> length, so streaming is required...) into a file at a given url.
> When I try to do this, it creates the file on the webserver, but it  
> has no
> content what so ever.
>
> Here is my current code, any ideas anyone?
>
>       String url = "http://localhost/a.xml";
>       HttpClient client = new HttpClient();
>       method = new PutMethod(url);
>       method.setPath(url);
>
> method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHU 
> NKED);
>       method.setUseExpectHeader(true);
>       method.setRequestHeader(STRING_ContentType, STRING_textXml);
>
>       client.setHostConfiguration(method.getHostConfiguration());
>
>       int statusCode = -1;
>       int attempt = 0;
>
>       while (statusCode == -1 && attempt < 3) {
>         try {
>           statusCode = client.executeMethod(method);
>         }
>         catch (HttpRecoverableException e) {
>           //retry
>         }
>         catch (IOException e) {
>           //fatal
>         }
>       }
>
>       connection =
> client.getHttpConnectionManager().getConnection(method.getHostConfigura 
> tion());
>
>       os = new  
> ChunkedOutputStream(connection.getRequestOutputStream());
>
>       // create a PrintStream and put data to the web server
>       urlOut = new PrintStream(os, true);
>
> followed by several urlOut.write(String)'s, then:
>
>       //Release the connection.
>       ((ChunkedOutputStream) os).writeClosingChunk();
>       urlOut.flush();
>       urlOut.close();
>
>       os.flush();
>       os.close();
>
>       method.releaseConnection();
>
>
> Toby
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
>


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


Mime
View raw message