• 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;

            System.out.println(url);

            try { // try

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

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

                  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();
                  out.write(postVar.getBytes());


            } 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
0
Addictive
Asked:
Addictive
  • 2
  • 2
1 Solution
 
rrzCommented:
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");

0
 
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?
0
 
rrzCommented:
I use multi-part.  For example, see  
http://developer.sonyericsson.com/site/global/techsupport/tipstrickscode/java/p_post_javafile_webserver.jsp     
But, your way would be easier(if it works).  It is a lot of work to construct the multi-part request.       rrz
0
 
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.
0

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