Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
filiussoft
Posts: 106
Registered: ‎06-18-2009
My Device: Bold

Upload Files / HTTPConnection / Outputstream / multipart form-data

Hi,

 

I'm working on a app that needs to upload some data including images to a server.

It's pretty much working except when the total of the data + images exceeds 1MB. Everytime that happens, I simply dont receive the files that exceed the 1MB -> meaning I still get my other data (json) and any files before the 1MB mark was hit.

 

Now I checked the backend piece and there is no limitation to the 1MB. Using the same backend method but through a website, I have uploaded files that were multiple MBs large and had no problem so it must be on the BlackBerry end.

 

I'm basiclally pumping everything into the OutputStream. The os variable is of type OutputStream. I tried setting the content-length parameter after I write everyting to the stream but that throws an exception. Apparently you can only set the headers before starting to put things into the output stream.

 

I'm uploading the data + images as form data. The content-type is "multipart/form-data".

No matter what the actual content lengh of all the bytes written to the output stream is, when it is above 1MB, the content-length that I receive on the server end is always 1048576 (1MB).

 

Has anyone experienced this before? Do I need to use a different kind of outputstream? Such as DataOutputStream or MIMEOutputStream? 

 

I appreciate any pointers on how I can accomplish uploads / outputstreams above 1MB.

 

Below is the code that I'm using.

 

 

String boundary = "--------------------" + System.currentTimeMillis();
c.setRequestMethod(HttpConnection.POST);

c.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
os = c.openOutputStream();

data = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"jsonData\";\r\n\r\n";
os.write(data.getBytes());
os.write(jsonString.getBytes());

int numFiles = _files.size();
for (int i = 0; i < numFiles; ++i)
{
	// Image filename
	String filename = (String) _files.elementAt(i);

	FileConnection file = (FileConnection) Connector.open("file://" + filename);
	if (file.exists())
	{
		InputStream in = file.openInputStream();
		byte[] imageData = new byte[(int) file.fileSize()];

		int numRead = in.read(imageData);
		String actualFilename = filename.substring(filename.lastIndexOf('/') + 1);

		if (numRead == file.fileSize())
		{
			// Between
			data = "\r\n--" + boundary + "\r\n";
			os.write(data.getBytes());

			data = "Content-Disposition: form-data; name=\"file" + (i + 1) + "\"; filename=\"" + actualFilename + "\"\r\nContent-Type: image/jpg\r\n\r\n";
			os.write(data.getBytes());
			os.write(imageData);
		}

	}
}

// End
data = "\r\n--" + boundary + "--\r\n";
os.write(data.getBytes());

os.flush();

 

 

----
If this helped you, press the thumbs up button (on the far right - '+' thumb icon)
If this post solved the issue, click "Accept as Solution"
Please use plain text.
Developer
peter_strange
Posts: 19,608
Registered: ‎07-14-2008
My Device: Not Specified

Re: Upload Files / HTTPConnection / Outputstream / multipart form-data

The download limits are fairly well documented, and I know that Mark Sohm has documented some upload limits, but I can't find them.  You might like to search around.  From memory I believe the limits are similar to the download ones - in other words the data limits are per connection irrespective of the direction of data flow. 

 

Anyway, it does not surprise me that something in the system will break the connection when the connection exceeds 1MB.  That is a lot of data for a wireless transfer.  I would recommend that you chunk the data up and send it to the server in pieces, this make it easier to restart too.

 

If you want a chunk size that I find works with all connection methods - I would use 64 KB. 

Please use plain text.
Contributor
CAMOBAP
Posts: 10
Registered: ‎04-22-2013
My Device: Torch 9800

Re: Upload Files / HTTPConnection / Outputstream / multipart form-data

Today I faced with the problem described in topic.

 

Peter,

Which technique you can advice for uploading video (~10 Mb) to HTTP server that not support Transfer-Encoding: chunked (ngixn server)?

 

Thanks,

Alex

Please use plain text.