tags:

views:

47

answers:

1

While I am uplodaing multiple images in j2me to servlet, I am able to get first image in servlet but not able get second image. is something wrong in my code? could you please suggest

This is my code.

// For First Image uploading

String message1 = ""; message1 += "-----------------------------4664151417711" + CrLf; message1 += "Content-Disposition: form-data; name=\"image1\"; filename=\"" + FILE1 + "\"" + CrLf; message1 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;

        // the image is sent between the messages ni the multipart message.

        String message2 = "";
        message2 += CrLf + "-----------------------------4664151417711--" + CrLf;               

        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
        // might not need to specify the content-length when sending chunked data.
        // conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));

        System.out.println("open os");
        os = conn.openOutputStream();

        System.out.println(message1);
        os.write(message1.getBytes());




        // SEND THE IMAGE
        int index = 0;
        int size = 1024;
        do{
            System.out.println("write:" + index);
            if((index+size)>imgData1.length){
                size = imgData1.length - index; 
            }
            os.write(imgData1, index, size);
            index+=size;
            progress(imgData1.length, index); // update the progress bar.

        }while(index<imgData1.length);

// For Second Image uploading

message1 = ""; message1 += "-----------------------------4664151417711" + CrLf; message1 += "Content-Disposition: form-data; name=\"image2\"; filename=\"" + FILE2 + "\"" + CrLf; message1 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;

        // the image is sent between the messages ni the multipart message.

        message2 = "";
        message2 += CrLf + "-----------------------------4664151417711--" + CrLf;               

        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");
        // might not need to specify the content-length when sending chunked data.
        // conn.setRequestProperty("Content-Length", String.valueOf((message1.length() + message2.length() + imgData.length)));


        System.out.println(message1);
        os.write(message1.getBytes());




        // SEND THE IMAGE
        int index = 0;
        int size = 1024;
        do{
            System.out.println("write:" + index);
            if((index+size)>imgData2.length){
                size = imgData2.length - index; 
            }
            os.write(imgData2, index, size);
            index+=size;
            progress(imgData2.length, index); // update the progress bar.

        }while(index<imgData2.length);
A: 

Its working fine If I modify the code as below

conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=---------------------------4664151417711");

   os = conn.openOutputStream(); 
   os.write(message1.getBytes()); 

// For First Image uploading

String message1 = ""; message1 += "-----------------------------4664151417711" + CrLf; message1 += "Content-Disposition: form-data; name=\"image1\"; filename=\"" + FILE1 + "\"" + CrLf; message1 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;

     os.write(message1.getBytes());


    // SEND THE IMAGE 
    int index = 0; 
    int size = 1024; 
    do{ 
        System.out.println("write:" + index); 
        if((index+size)>imgData1.length){ 
            size = imgData1.length - index;  
        } 
        os.write(imgData1, index, size); 
        index+=size; 
        progress(imgData1.length, index); // update the progress bar. 

    }while(index<imgData1.length); 

// For Second Image uploading

String message2 = ""; message2 += "-----------------------------4664151417711" + CrLf; message2 += "Content-Disposition: form-data; name=\"image2\"; filename=\"" + FILE2 + "\"" + CrLf; message2 += "Content-Type: image/jpeg" + CrLf; message1 += CrLf;

     os.write(message2.getBytes());


    // SEND THE IMAGE      
    //int index = 0;      
    int size = 1024;      
    do{      
        System.out.println("write:" + index);      
        if((index+size)>imgData2.length){      
            size = imgData2.length - index;       
        }      
        os.write(imgData2, index, size);      
        index+=size;      
        progress(imgData2.length, index); // update the progress bar.      

    }while(index<imgData2.length);      

String message3 = "\r\n-----------------------------4664151417711--\r\n";
os.write(message3.getBytes());

Venkat