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

Posted on 2006-05-15
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
    LVL 1

    Accepted Solution

    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

         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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    sumHeights2  challenge 7 51
    count8 challlenge 13 67
    Arduino EDI - Programming language 3 29
    Installed softare without GUID 6 22
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    761 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