J2ME image upload to http server

Posted on 2007-10-16
Last Modified: 2013-11-24
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);

                  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
Question by:Addictive
    LVL 27

    Expert Comment

    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");


    Author Comment

    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?
    LVL 27

    Accepted Solution

    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

    Author Comment

    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.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Suggested Solutions

    Title # Comments Views Activity
    fix34  challenge 9 70
    nested interface 5 29
    hasOne  challenge 59 63
    maven java path setting 5 31
    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now