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
mantaker
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

[ Edited ]

Hey guys,

 

I've done it on my own...

 

The fix is you gotta use HttpProtocolConstants class present under the package net.rim.device.api.io.http for setting the setRequestProperty in HttpConnection...

 

By the way, The Content info has to be go as a byte stream .. Lemme paste a generalized piece of code..

 

 

String boundary = "Some_Unique_Text_Also_Alphanumeric";

 

HttpConnection conn = (HttpConnection) Connector.open("Web_Application_URL", Connector.READ_WRITE);conn.setRequestMethod(HttpConnection.POST);

 

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA+";boundary="+boundary);

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(Bytes_Of_Image_To_Be_Uploaded.length));

conn.setRequestProperty( "x-rim-transcode-content", "none" );

 

ByteArrayOutputStream out = new ByteArrayOutputStream();

OutputStream finalOut = conn.openOutputStream();

 

String newLine = "\r\n";

out.write(newLine.getBytes());

out.write("--".getBytes());

out.write(boundary.getBytes());

out.write(newLine.getBytes());

String contDisp="Content-Disposition:form-data; name=\"Image.jpg\";filename=\"Image.jpg\"";

String contEnc = "Content-Transfer-Encoding: binary";

String type="Content-Type:image/jpeg";

out.write(contDisp.getBytes());

out.write(newLine.getBytes());

out.write(type.getBytes());

out.write(newLine.getBytes());

out.write(contEnc.getBytes());

out.write(newLine.getBytes());

out.write(newLine.getBytes());

out.write(Bytes_Of_Image_To_Be_Uploaded);

out.write(newLine.getBytes());

out.write("--".getBytes());

out.write(boundary.getBytes());

out.write("--".getBytes());

out.write(newLine.getBytes());

finalOut.write(out.toByteArray());

 

out.flush();

out.close();

 

finalOut.flush();

finalOut.close();

 

 Thanks ..

 

Message Edited by mantaker on 02-12-2009 10:37 PM
--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Ah - I see you finally took my advice from the first response and set the content length!

 

:smileyhappy:

 

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(Bytes_Of_Image_To_Be_Uploaded.length));

 

 

Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

[ Edited ]

But Mr. RexDoug :smileywink:

 

 

conn.setRequestProperty("Content-Length", ""+Bytes_Of_Image_To_Be_Uploaded.length);

didn't work out for me :smileyvery-happy:

 

 

Rather,

 

conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(Bytes_Of_Image_To_Be_Uploaded.length));

worked .. :-/

 

 

Well, no one told me to send those Content information as bytes :smileysad:

 

Thanks anyway for all who've participated in this thread ..

 

 

---------------THREAD LOCKED-------------

Message Edited by mantaker on 02-13-2009 06:16 PM
--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
smokingfly
Posts: 39
Registered: ‎03-24-2009
My Device: Not Specified

Re: HttpConnection, Upload Image!

Hi,

 

I tried to use your code.

 

It is not throwing any exception but it is somehow not uploading the image.

 

Can you please explain whats the purpose of "boundary" and what exactly i have to put in "name" parameter in "Content-Disposition:form-data".

 

I know this is bit demanding but i have tried many method but i am unable to upload a file.

 

Pls suggest.

 

 

 

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

I recommend that you start a new Thread, with a complete description of your problem.  Point to this Thread if it is relevant. 

Please use plain text.
Developer
smokingfly
Posts: 39
Registered: ‎03-24-2009
My Device: Not Specified

Re: HttpConnection, Upload Image!

I am talking about the above code which is marked as solved. Thats why i have put this question in this thread.

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Makes no difference.

 

The general rule is one problem = one Thread. 

 

Solved Threads are solved and do not contain any problem.  People generally don't look at them unless they also want the solution. 

 

If you want people to look at your problem, start a new Thread.  If you are using code from this Thread, provide a link to this Thread in your question. 

 

Regarding your question, I suggest you review the http headers to understand how boundary and Content-Disposition are used.  I've done a quick Google to find these links that might help:

 

http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html

http://www.ietf.org/rfc/rfc2388.txt

 

These are not Blackberry specific headers. 

Please use plain text.
New Developer
david_schwartz_bb
Posts: 137
Registered: ‎02-04-2010
My Device: Tour 9630
My Carrier: verizon

Re: HttpConnection, Upload Image!

do you have a complete sample code for uploading images?

thanks

Please use plain text.
Contributor
ducaannalise
Posts: 14
Registered: ‎10-13-2010
My Device: Blackberry 9550 Simultor

Re: HttpConnection, Upload Image!

Hi, 

 

Would you be so kindly and explain in detail how you managed to upload a file through the bberry app onto a server. I am trying to follow your example, but don't seem to make it work!

 

 

Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

@duccannalise, I'd be happy to help you! But where did you get stuck? Have you understood how(Format, etc) your server-side component wants the file? There can be some set of rules that your server side might expect from client.


Cheers,

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.