Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

Posted on 2006-05-15
Medium Priority
Last Modified: 2008-02-07

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.
Question by:velosumdev

Accepted Solution

lhbiltw earned 1500 total points
ID: 16687458
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.

Expert Comment

ID: 16687967
     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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Six Sigma Control Plans
Loops Section Overview

580 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