Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
Posts: 17
Registered: ‎08-06-2012
My Device: Curve 9320
My Carrier: Vodaphone
Accepted Solution

Post Multiple Images

Hi,

 

Please Guide me where I am going wrong to post multiple images to the server using http connection along with other data in the request body.

 

 

My code snippet to send one image first (Not working):

-----------------------------------------------------------------------------

 

 InputStream mImagen = this.getClass().getResourceAsStream("avatar_default.png");      
 byte[]   imgData = IOUtilities.streamToBytes(mImagen);

 

String message1 = "";
           message1 += "AaB03x\r\n";
           message1 += "Content-Disposition: form-data; name=\"avatar\"; filename=\"avatar_default.png\"\r\n";
           message1 += "Content-Type: image/png\r\n";
           message1 += "\r\n";

 

 String reqStr = "http://myurl/updateProfileDetails.json?data1=smileys&data2=xxxxx";

 

s = (StreamConnection) Connector.open(reqStr+";deviceside=true",Connector.READ_WRITE);
   httpConn = (HttpConnection) s;

 httpConn.setRequestMethod(HttpConnection.POST);
  httpConn.setRequestProperty("Content-Disposition", "form-data");
  httpConn.setRequestProperty("Content-Type","multipart/form-data;boundary=AaB03x");



      httpConn.setRequestProperty("Content-Length", String.valueOf(imgData.length + message1.length()));     httpConn.setRequestProperty("Accept", "application/json");
    httpConn.setRequestProperty("Connection", "Keep-Alive");
     httpConn.setRequestProperty("Authorization", "Basic " + authHeader);


 os = httpConn.openOutputStream();
      
      os.write(message1.getBytes());
      os.write(imgData, 0, imgData.length);

      os.flush();
      os.close();

 

 

Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Post Multiple Images

Hi @sanghamitra_83


What error do you get?

 

E.

Contributor
Posts: 17
Registered: ‎08-06-2012
My Device: Curve 9320
My Carrier: Vodaphone

Re: Post Multiple Images

No Error, but the database is not updated. I mean probably the server is not receiving the image data to store.

Contributor
Posts: 17
Registered: ‎08-06-2012
My Device: Curve 9320
My Carrier: Vodaphone

Re: Post Multiple Images

After I set the Content-Length, If I try to check connection.getLength() it is returning -1 always. Why it can't find out the length. I guess that's why the request body is not proper.
Developer
Posts: 729
Registered: ‎05-04-2011
My Device: 9700

Re: Post Multiple Images

I suggest you check your requests with a sniffer.

 

That way you would know what was sent and what was the respond.

 

It might be that we are looking at the wrong direction.

 

E.

Trusted Contributor
Posts: 151
Registered: ‎06-17-2010
My Device: Curve 8900
My Carrier: Vodafone

Re: Post Multiple Images

Hi sanghamitra_83,

 

I think you have to send all your images via Httpmultipart.

 

check out this link and use that class in your application. i have already used this class to send one images to server, and it works fine.

 

_________________________________
feel free to press the kudos button on the left side to thank the user that helped you.
please mark posts as solved if you found a solution.

Contributor
Posts: 17
Registered: ‎08-06-2012
My Device: Curve 9320
My Carrier: Vodaphone

Re: Post Multiple Images

Hi

 

I have tried the same code before too. It works fine for other the data except the image.

it shows a success message as server response, but actually the image doesn't get stores in the database.

 

Can you guess where could be the issue.

 

Thanks,

Contributor
Posts: 17
Registered: ‎08-06-2012
My Device: Curve 9320
My Carrier: Vodaphone

Re: Post Multiple Images

Hi,

 

I realized that before sending the image base64encode was required.

 

Thanks for the advice anyway.