• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10573
  • Last Modified:

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
  • 2
  • 2
1 Solution
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?
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
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.

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now