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

Posts: 21
Registered: ‎02-11-2009
My Device: Not Specified
Accepted Solution

HttpConnection upload image to server

Hi All,

I am new to accesing server and I am trying to send an image file to my server using HttpConnection class. I have searched this forum for the same but couldn't figure out my mistake. My requirement is to post an image file to the server application running in Tomcat. Here is my code to access the server. The server is expecting the image in the request. I am not sure how this POST method works.

When i debug, i am getting the http response code as "500". I replaced the URL as and i could get the response code as "200" which is success. Please look at the code for server also.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int length =request.getContentLength(); log ("file name:"+length); String type =request.getContentType(); log ("content type:"+type); ServletInputStream in = request.getInputStream(); log ("input stream:"+in); String path = request.getContentType(); log ("szname:"+path); File f1 = new File(path); log("File Name:" + f1.getName()); log("Path:" + f1.getPath()); log("Abs Path:" + f1.getAbsolutePath()); }


.Client Code:

OutputStream out = null; InputStream in = null; String URL = ""; try { in = this.getClass().getResourceAsStream("/icons.png"); byte[] postData = IOUtilities.streamToBytes(in); int len = postData.length; conn = (HttpConnection) Connector.open(URL); conn.setRequestMethod(HttpConnection.POST); int respCode = conn.getResponseCode(); if (respCode == HttpConnection.HTTP_OK) { conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,String.valueOf(postData.length)); conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,HttpProtocolConstants.CONTENT_TYPE_IMAGE_PNG); out = conn.openOutputStream(); out.write(postData); }

Please Help...

Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: HttpConnection upload image to server

You need to set the content length (all request headers, actually) and also write the post data before you call getResponseCode(). Take a look at the documentation for javax.microedition.io.HttpConnection--there's some sample code there for doing a POST. Although the sample code doesn't set the content-length header, you should, since your server code relies on it.

Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Posts: 1,477
Registered: ‎12-30-2008
My Device: Torch 9810

Re: HttpConnection upload image to server

Refer this.. I was successful in uploading to the server ..





Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Posts: 21
Registered: ‎02-11-2009
My Device: Not Specified

Re: HttpConnection upload image to server

Hi Ted,


Thanks for ur Help. Your suggestion solved my problem.