users@jersey.java.net

Re: [Jersey] Large file upload

From: Ersin Er <ersin.er_at_gmail.com>
Date: Wed, 24 Mar 2010 19:00:45 +0200

The following code works fine:

  @POST
  @Path("/upload")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  @Produces(MediaType.TEXT_PLAIN)
  public String upload(@FormDataParam("file") InputStream fileInpStr) throws
IOException {
      FileOutputStream fos = new FileOutputStream("upload.tgz");
      IOUtils.copy(fileInpStr, fos);
    return "Done.";
  }

Thanks Paul for both the answer and the cool work you're doing with Jersey.

On Wed, Mar 24, 2010 at 18:15, Paul Sandoz <Paul.Sandoz_at_sun.com> wrote:

> Hi Ersin,
>
> The use of FormParam with "multipart/form-data" has been deprecated
> (although i should log such a message to say that!)
>
> The recommended approach is to use the jersey-multipart module:
>
>
> https://jersey.dev.java.net/nonav/documentation/latest/user-guide.html#d4e1230
>
> and @FormDataParam:
>
>
> https://jersey.dev.java.net/nonav/apidocs/latest/contribs/jersey-multipart/com/sun/jersey/multipart/FormDataParam.html
>
> Large parts will be cached to disk.
>
> Paul.
>
>
> On Mar 24, 2010, at 4:38 PM, Ersin Er wrote:
>
> Hi,
>>
>> I have managed to upload file with a bunch of code like the following:
>>
>> @POST
>> @Path("/upload")
>> @Consumes("multipart/form-data")
>> @Produces("text/plain")
>> public String upload(@FormParam("file") File file) {
>> return "Done.";
>> }
>>
>> The first question is whether this is a correct way of uploading (big)
>> files?
>>
>> My second question is related to the problem I face with. Although this
>> code works fine for small files, I get heap memory exception for large files
>> although I have reserved more than enough amount of memory such as 2GB. I
>> get the following exception trace:
>>
>> javax.servlet.ServletException: Servlet execution threw an exception
>> </pre></p><p><b>root cause</b> <pre>java.lang.OutOfMemoryError: Java heap
>> space
>> java.util.Arrays.copyOf(Arrays.java:2882)
>>
>> java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:100)
>>
>> java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:515)
>> java.lang.StringBuilder.append(StringBuilder.java:189)
>>
>> com.sun.jersey.core.util.ReaderWriter.readFromAsString(ReaderWriter.java:159)
>>
>> com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.readFromAsString(AbstractMessageReaderWriterProvider.java:111)
>>
>> com.sun.jersey.core.impl.provider.entity.BaseFormProvider.readFrom(BaseFormProvider.java:62)
>>
>> com.sun.jersey.core.impl.provider.entity.FormProvider.readFrom(FormProvider.java:70)
>>
>> com.sun.jersey.core.impl.provider.entity.FormProvider.readFrom(FormProvider.java:57)
>>
>> com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:444)
>>
>> com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:453)
>>
>> com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider.processForm(FormDispatchProvider.java:93)
>>
>> com.sun.jersey.server.impl.model.method.dispatch.FormDispatchProvider$FormParameterProvider.getInjectableValues(FormDispatchProvider.java:105)
>>
>> com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:119)
>>
>> com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:147)
>>
>> com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
>>
>> com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
>>
>> com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
>>
>> com.sun.jersey.server.impl.uri.rules.ResourceObjectRule.accept(ResourceObjectRule.java:81)
>>
>> com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
>>
>> com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
>>
>> com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
>>
>> com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
>>
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
>>
>> com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
>> javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
>>
>> I get this exception when I try to upload a file of size about 300MB.
>>
>> One another note is that I do not yet use jersey-multipart and I do not
>> know whether it can help here.
>>
>> Any suggestions?
>>
>> --
>> Ersin
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>


-- 
Ersin ER
http://www.ersiner.net