Question

I am trying to send image from my midlet to an HTTP server. images are converted into byte and sent to server using http multipart/form-data request format.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

When the image size is less than around 500Kb then the code works fine, but when the size is greater than it shows: Uncaught exception java.lang.OutOfMemoryError. I tried using Java ME SDK 3.0 and Nokia S40 5th edition FP1. Any help is greatly appreciated. Thanks for looking

I used the following class file: click here

Was it helpful?

Solution

Being forced to read the whole file into memory with the first `getFileBytes(), in order to transmit in one piece, is most likely what's running the system out of memory.

Find a way to read about 100K, transmit it, then read another 100, until the whole file is done.

The HttpMultipartRequest class's constructor as written allows only for the transmission of the file as one single object. Even though it's an implementation of the MIME multipart content protocol, it is limited to the case of transmitting just one part:

The class can be modified to allow sending multiple parts. Have a look at the protocol specification RFC1341, especially the example half-way through.

With these three lines together as they are in the constructor, the whole file is sent in one part;

bos.write(boundaryMessage.getBytes());
bos.write(fileBytes);
bos.write(endBoundary.getBytes());

But in the multipart case, there needs to be multiple boundaries, before the endBoundary:

 for(bytes=getMoreFileBytes(); ! bytes.empty; bytes=getMoreFileBytes()){
        bos.write(boundaryMessage.getBytes());
        bos.write(bytes);
    }
    bos.write(endBoundary.getBytes());

As a quick fix, let the constructor open the file and read it 100k at a time. It already receives a fileName parameter.

The PHP script on the other end, should reassemble the original file from the pieces.

OTHER TIPS

I am not very familiar with the forum rules, I tried to comment your answer but it shows negative.

Okay.. Now I am getting java.io.IOException: Persistent connection dropped after first chunk sent, cannot retry

previously I tried to use application/x-www-form-urlencoded request type with Base64 encoding using kidcandy's code here: http://forums.sun.com/thread.jspa?threadID=538500

This code divides the imagedata in chunks to avoid 'Persistent connection drop' problem and creates connection with the server using 'for' loop. The problem is maximum chunk size maybe only 500-700 bytes. So to send a 100kb image the code needs to create and close connection 200 times, I tried to run this on nokia 5310 phone, it behaves like it is hibernating... so it is not useful.

  1. Now should I use that for loop for 'multipart/form-data' request?
  2. What is the maximum chunk size for this type of request?
  3. Or any other idea? Regards
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top