users@jersey.java.net

Uploading files <was> Re: [Jersey] _at_GET and _at_Context HttpServletRequest

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Thu, 25 Jun 2009 09:49:59 +0200

Hi,

Your client code looks fine.

Hard to tell from the exception what is going on. I suspect the server
closed the connection while Jersey was streaming the request. However,
HttpURLConnection is like to buffer the request entity, so it can
perform a retry.

Are there any logs on the server side indicating an error?

Paul.

On Jun 25, 2009, at 6:17 AM, Ronak Patel wrote:

> Hi All,
>
> Has anyone tried to use the JAX-RS Jersey client to upload a file to
> a REST Web Service?
>
> I'm doing the following:
>
> HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter(userName,
> password);
>
> Client.client = Client.create();
> client.addFilter(authFilter);
> client.setFollowRedirects(true);
>
> final WebResource uriResource = client.resource(uri);
>
> final ClientResponse response = uriResource.entity(new File("/home/
> ronak/test.tif"), "image/tif").post(ClientResponse.class);
>
> I'm getting an exception uploading the file as so:
>
> Root Exception stack trace:
> java.io.IOException: Error writing request body to server
> at sun.net.www.protocol.http.HttpURLConnection
> $StreamingOutputStream.che
> ckError(HttpURLConnection.java:2637)
> at sun.net.www.protocol.http.HttpURLConnection
> $StreamingOutputStream.wri
> te(HttpURLConnection.java:2620)
> at
> com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutp
> utStream.java:87)
> at
> com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writ
> eTo(AbstractMessageReaderWriterProvider.java:73)
> at
> com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FilePro
> vider.java:99)
> at
> com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FilePro
> vider.java:62)
> at
> com.sun.jersey.api.client.TerminatingClientHandler.writeRequestEntity
> (TerminatingClientHandler.java:317)
> at
> com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invok
> e(URLConnectionClientHandler.java:179)
> at
> com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle
> (URLConnectionClientHandler.java:126)
> at
> com.sun.jersey.api.client.filter.HTTPBasicAuthFilter.handle(HTTPBasic
> AuthFilter.java:69)
> at com.sun.jersey.api.client.Client.handle(Client.java:397)
> at
> com.sun.jersey.api.client.WebResource.handle(WebResource.java:557)
> at com.sun.jersey.api.client.WebResource.access
> $300(WebResource.java:69)
>
> at com.sun.jersey.api.client.WebResource
> $Builder.post(WebResource.java:4
> 91)
>
> Am I doing something wrong?
>
> Thanks for your help!!
>
> Ronak
>
>