Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to avoid OutOfMemoryError when uploading a large file using Jersey client
    text
    copied!<p>I am using Jersey client for http-based request. It works well if the file is small but run into error when I post a file with size of 700M:</p> <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:2786) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:94) at sun.net.www.http.PosterOutputStream.write(PosterOutputStream.java:61) at com.sun.jersey.api.client.CommittingOutputStream.write(CommittingOutputStream.java:90) at com.sun.jersey.core.util.ReaderWriter.writeTo(ReaderWriter.java:115) at com.sun.jersey.core.provider.AbstractMessageReaderWriterProvider.writeTo(AbstractMessageReaderWriterProvider.java:76) at com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FileProvider.java:103) at com.sun.jersey.core.impl.provider.entity.FileProvider.writeTo(FileProvider.java:64) at com.sun.jersey.multipart.impl.MultiPartWriter.writeTo(MultiPartWriter.java:224) at com.sun.jersey.multipart.impl.MultiPartWriter.writeTo(MultiPartWriter.java:71) at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:300) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204) at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147) at com.sun.jersey.api.client.Client.handle(Client.java:648) at com.sun.jersey.api.client.WebResource.handle(WebResource.java:680) at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:568) at TestHttpRequest.main(TestHttpRequest.java:42) </code></pre> <p>here is my code:</p> <pre><code>ClientConfig cc = new DefaultClientConfig(); Client client = Client.create(cc); WebResource resource = client.resource("http://localhost:8080/JerseyWithServletTest/helloworld"); FormDataMultiPart form = new FormDataMultiPart(); File file = new File("E:/CN_WXPPSP3_v312.ISO"); form.field("username", "ljy"); form.field("password", "password"); form.field("filename", file.getName()); form.bodyPart(new FileDataBodyPart("file", file, MediaType.MULTIPART_FORM_DATA_TYPE)); ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, form); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload