Link to home
Start Free TrialLog in
Avatar of velosumdev
velosumdev

asked on

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

Hi,

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

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


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

dos.write(FileSender.PREFIX_BYTES);
            dos.write(sBoundary.getBytes());
            dos.write(FileSender.NEW_LINE_BYTES);
            String sContentDisposition = "Content-Disposition: form-data; name=\"" +
                        sName + "_pgc\"; filename=\"" + sPgcFileName + "\"";
            dos.write(sContentDisposition.getBytes());
            dos.write(FileSender.NEW_LINE_BYTES);                
            String sType = "Content-Type: application/octet-stream";
            dos.write(sType.getBytes());
            dos.write(FileSender.NEW_LINE_BYTES);
            dos.write(FileSender.NEW_LINE_BYTES);
            dos.write(m_midlet.getPgcFile().getData());
            dos.write(FileSender.NEW_LINE_BYTES);


Thanks in advance.
Praj
ASKER CERTIFIED SOLUTION
Avatar of lhbiltw
lhbiltw

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of muthu_soft
muthu_soft

Hai,
   
     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