J2ME: Upload image (jpg/png) to the server via HTTP connection


Phone: Motorola i870

Application allows the user to capture an image. Then it sends two different files to the server using HTTP post. The first one goes through without any problem but the one with the image file does not go through. I want to send that image file as an attachment. Here is my source. It is very urgent. Really appreciate if you be a help in any way.

///Image file --- which does not work.
// Establish file names
            String sName = m_midlet.getPgcFile().getName();
            if (sName.toLowerCase().endsWith(".pgc")) {
                  sName = sName.substring(0, sName.length() - 4);
Date date = new Date(System.currentTimeMillis());
            String sDateString = DateUtils.getPaddedDateString(date, '-');
            String sTimeString = DateUtils.getPaddedTimeString(date, '-');
            String sBaseFileName = sFilePrefix + "_" + sCustId + "_" + m_midlet.getPhoneNumber() +
                  "_" + sDateString + "_" + sTimeString;
            String sPgcFileName = sBaseFileName + ".pgc";
            String sJpgFileName = sBaseFileName + ".jpg";

                  //sContentDisposition = "Content-Disposition: form-data; name=\"" +
                  //            sName + "_jpg\"; filename=\"" + sJpgFileName + "\"";
                        sContentDisposition = "Content-Disposition: form-data; name=\"" +
                              sName + "_jpg\"; filename=\"" + sJpgFileName + "\"";
                  dos.write(m_midlet.getPgcFile().getAttachedImage()); //This is the array of byes of captured image.

////////////////PGC file ---- the one which works..

            String sContentDisposition = "Content-Disposition: form-data; name=\"" +
                        sName + "_pgc\"; filename=\"" + sPgcFileName + "\"";
            String sType = "Content-Type: application/octet-stream";

Thanks in advance.
Who is Participating?
There is important information missing here. First all all, where are you sending the file TO? You said a server, but web servers don't just receive image files and then know what to do with them, you have to be directing your POST request to a particular script that receives the file and knows what to do with it. Second, what class is "dos" an instance of? (I'm assuming it's DataOutputStream.) How did you instantiate it? When you say you want to send your image file as an attachment, what exactly do you mean by that? Usually when we say "attachment" we're talking about email, but we're dealing with HTTP here, not email.

Your way of generating the HTTP headers is somewhat unusual in Java, so I'm not clear on what you have in mind. A more typical way to do it is shown here:


Also, it's not clear what your Content-Type is for the image files -- is sType set to "application/octet-stream" for the image file? For debugging purposes, what you might try is printing out the request to a file so you can inspect it manually and make sure the HTTP request that you're generating looks correct.
     This is M. The Header Content-Length, Content Type is Missing  Header is missing in your code.

All the headers will be parsed on the server side code(JSP or any other server side script)  and the operation will take place based on the content received on the server side.

  I am not sure abt the server side code you have written . But be sure the method getAttachedImage() returns the image properly by checking it directly on the mobile once . if its fine . Then all the consequences will work fine.

Let me know if you have any more problems
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.