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

HttpConnection, Upload Image!

Dear Blackberry developers,

 

I'm just trying out a sample of uploading a picture to the server! The server part is JFM http://jfm.sourceforge.net/

 

My code is

 

HttpConnection conn = (HttpConnection) Connector.open("http://192.168.1.205:8082/jFM/path/JFM/Uploads/");
                                conn.setRequestProperty("Content-Type", "multipart/form-data");
                                conn.setRequestMethod(HttpConnection.POST);
                                conn.setRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");
                                conn.setRequestProperty("Content-Disposition", "form-data; filename=\"abc.jpg\"");
                                OutputStream out= conn.openOutputStream();
                                byte b[]=imageBytes;
                                out.write(b);
                                out.close();
                                Dialog.inform("Resp Code: "+conn.getResponseCode());

 

imageBytes is what I capture from the camera using getSnapShot() method!!

 

When I run the application in BB Simulator 9000, I get a response code of 200 (ie, Http Ok).. I've sniffed the packets, it shows that the data has been sent to the server, the server inturn responses me, yes I have recieved the file, indeed!

 

I wonder why the hell file is not present in the serverside .. Someone tell me if there is some request property that I need to set on!!

 

Thanks in advance!

 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Where are you setting the content length, since this is a POST?

 

How did you get the bytes from the image? Is this encoded using EncodedImage?

 

Are you sure about the "Content-Type" setting? Shouldn't this be one of the image MIME types?

 

Did you check the server-side logs?

 

Developer
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

I'm not setting any content length! I could see that in the sniffed packets, I'm sure, its taken automatically according to the file we send!! Those bytes are directly got from VideoControl.getSnapShot method!

 

Do I really need to Encode the image? I guess the image is encoded and given when using the getSnap method in VideoControl...

 

Unfortunately, the serverside logs doesn't give much info .. If the file upload is successful, we get a -1 in new line in that catalina.out log ..

 

-1 is not coming when using my application.. I'll also try to use a file from the mass storage and let u all know if it works..

 

Well, can u or anyone post a working code on this? I've found a swing desktop application in this scenario and when interfaced with this jfm, works fine ..

 

I'm really stuck .. Expecting quick replies Smiley Wink

 

Thanks

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

If you tell the server it is encoded form data, and you haven't told it what you really mean by

form data, it will probably do something like this,

 

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

 

 

Did you point your working app at a dummy server so you could see what it sends? Perl makes a dummy

server easy and you can point your desktop app to 127.0.0.1 and make the same post request.

 

I was going to download all the RFC's and search with scripts or load into postgresql but I don't have

the time to unzip 88M right now.

 

Sorry, but I really need to make this quote better known as it is too funny to ignore,

http://www.rfc-editor.org/download.html

 

"However, users of Microsoft Windows often have difficulty displaying vanilla ASCII text files with the correct pagination."

 

 

You can't display TEXT files properly? ROFL

 

 

 

 

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

You have to set the "Content-Length" field for a post. I would start there.

 

 

Developer
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

Since I'm a newbie, I don't know what are the properties that I need to include .. Ok listen .. I've changed something today and the code is

 

JPEGEncodedImage jImg = JPEGEncodedImage.encode(image, 100); Dialog.inform(jImg.toString()); byte b[]=jImg.getData(); ByteArrayInputStream bais = new ByteArrayInputStream(b); HttpConnection conn = (HttpConnection) Connector.open("http://192.168.1.205:8082/jFM/path/JFM/Uploads/"); Dialog.inform("Connection Established with the host: "+conn.getHost()); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=--gc0p4Jq0M2Yt08jU534c0p--"); conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty("User-Agent", "BlackBerry9000/4.6.0.92 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/-1"); conn.setRequestProperty("Accept", "application/vnd.rim.html,text/html,application/xhtml+xml,application/vnd.wap.xhtml+xml,text/vnd.sun.j2me.app-descriptor,image/vnd.rim.png,image/jpeg,application/x-vnd.rim.pme.b,application/vnd.rim.ucs,image/gif;anim=1,application/vnd.rim.css;v=1,text/css;media=screen,application/vnd.wap.wmlc;q=0.9,application/vnd.wap.wmlscriptc;q=0.7,text/vnd.wap.wml;q=0.7,*/*;q=0.5"); conn.setRequestProperty("Content-Disposition", "form-data; name=\"Mine\"; filename=\"Abc.jpg\""); conn.setRequestProperty("MIME-Type", "Image/Jpeg"); conn.setRequestProperty("Content-Length", ""+b.length); OutputStream out= conn.openOutputStream(); int nread; int total = 0; byte[] buf = new byte[50000]; while((nread = bais.read(buf, 0, buf.length)) >= 0) { out.write(buf, 0, nread); total += nread; } out.close(); out.flush(); Dialog.inform("Resp Code: "+conn.getResponseCode());

 


 

I've also changed the serverside to log the events! It says that it got a request and it also says that Yes boss, I can understand that u've sent a multipart content!! Well, but the file writing part in the server side is not happening! I'm sure I'm missing some request parameters .. I could not figure out since I'm not familiar with http protocol as for now .. I've started reading it ..

 

Well, can someone help me at this instance .. I've got a swing application namely ClientHttpRequest2, I've interfaced it with the server-side application and it works like a charm...

 

Thanks

 

--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Essentially you are asking us "what does my server want?" If you send binary and it is expecting

ASCII, the first EOF like character could tell it "all done."

If you send your post request to your own code, you have a chance to do whatever you want.

Otherwise, you need to figure out what format the handler expects given the post you are making.

Usually this has something to do with file extensions of type headers.

 

Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Here is another issue with your code:

 

Dialog.inform("Connection Established with the host: "+conn.getHost())

 

This line does not belong here.

 

Either a) you are executing the HTTP connection in the UI Event thread (which is not recommended and will not work on a real device), or b) the dialog will not work because you are not in the UI event thread.

 

Either way....

Developer
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection, Upload Image!

[ Edited ]

I'm not doing it as a real world application! I'll create event thread when putting in the real device! I will manage those issues later on.. I know it's not a big deal, I can wrap these codes inside a thread and start with an instance of that thread!! As for now I just wanna know how my application should send a multipart post request! What are the request parameters I need to include..

 

When I sniffed the packets, when posting from a mozilla web browser, I got the following

 

 

POST /jFM/path/JFM/Uploads/ HTTP/1.1
Host: 192.168.1.205:8082
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://192.168.1.205:8082/jFM/path/JFM/Uploads/
Cookie: JSESSIONID=8207CD49452BEB622C7414FD13D4E66A
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 238360

 

When I sniffed packets with that swing application I told, I got this

 

 

POST /jFM/path/JFM/Uploads/ HTTP/1.1
Content-Type: multipart/form-data; boundary=----------------------------1u5se59sblrj0-1hgge6ul9mzir-i3l9b2k63m9b
Cache-Control: no-cache
Pragma: no-cache
User-Agent: Java/1.6.0_11
Host: 192.168.1.205:8082
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
Content-Length: 84132

 

See there is no much parameters sent when uploading using the swing application! But the fortunate thing is it's getting uploaded..But my BB code doesn't do this Smiley Surprised .. I'm sure I'm missing something, and I was unable to reverse engineer since I don't know much about this problem!!!

 

Can anyone please post some working BB code in this..

 

Thanks

 

 

Message Edited by mantaker on 01-31-2009 06:00 PM
--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: HttpConnection, Upload Image!

Did you look at the rest of the packets that presumably contain data?

At least one issue is the encoding of that data and any possible compression.

Without knowing anything about your server, you suspect the headers should match the data.

 

http://tools.ietf.org/id/draft-ietf-html-fileupload-01.txt

 

search this for "encoding"

 

http://tools.ietf.org/rfc/rfc2616.txt

 

or maybe see this?

 

http://svn.tools.ietf.org/svn/wg/httpbis/draft-ietf-httpbis/00/draft-ietf-httpbis-p3-payload-00.txt