J2ME image upload to http server

cliche...but how i am going to upload image from J2ME to a web server via http? i have a server side php script that is expecting imageData as a post variable. if i use simple text like String postVar = "imageData=" + "adsadas" it works. but if i use the actual byte arrya it simply doenst work! can any one please help me?

here is the code i m (trying) to use

      public void uploadImage(byte[] imageData, String url) {

            HttpConnection hc = null;
            InputStream in = null;
            OutputStream out = null;


            try { // try

                  String postVar = "imageData=" + new String(Base64.encode(imageData));

                  hc = (HttpConnection)Connector.open(url);

                  hc.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.1");
                  hc.setRequestProperty("Content-Language", "en-US" );
                  hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                  hc.setRequestProperty("Content-Length", Integer.toString( postVar.length()));

                  out = hc.openOutputStream();

            } catch (IOException ioe) {
                  StringItem stringItem = new StringItem(null, ioe.toString());
                  System.out.println("Error" + ioe.toString());
            } // end of catch

            finally {
                  try {
                        if (out != null) out.close();
                        if (in != null) in.close();
                        if (hc != null) hc.close();
                  catch (IOException ioe) {}
            } // end of try

      } // end of uploadImage

Base64 is a class i m using to do B64 encoding. the problem is
Who is Participating?
I use multi-part.  For example, see  
But, your way would be easier(if it works).  It is a lot of work to construct the multi-part request.       rrz
I am not an expert here, but I was wondering. Instead of using  
>hc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
you could try using  
hc.setRequestProperty("Content-Type", "application/octet-stream");

AddictiveAuthor Commented:
i tried that too!
please correct me if i am wrong!
i was thinkin, by this byte[] imageData i am sending "Image" object data, dont think the actual image. a possible solution could be sending it through RMI call. but the server doesnt have java, its a strict php server. any ideas?
AddictiveAuthor Commented:
i think there is a bug in WTK 2.5. if i use http 1.1 the "chunk" doesnt work. but if i use http 1.0 it works. so far i know chunk is a 1.1 thing. may be its fixed in wtk2.5.1 or later. i have to check.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.