commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Toby Williamson" <TWilliam...@tacitgroup.com>
Subject Using PutMethod to upload a stream via http to a url
Date Thu, 16 Oct 2003 01:39:55 GMT




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_CHUNKED);
      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.getHostConfiguration());

      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


Mime
View raw message