views:

852

answers:

4

Hi

I am using the HttpConnection class of J2ME in my BlackBerry app to send data to a web server. I need to send the contents of an image in the body of the HTTP request.

This is what I do

  1. Get the bytes of the file in an array

  2. Open HTTP connection

  3. Set content type header as image/jpeg

  4. Get output stream of the connection

  5. Write the bytes to the output stream

  6. Close the output stream and connection

But the image is not uploaded to the server. What could be the problem?

Thanks.

EDIT - Adding code

HttpConnection conn = null;
OutputStream out = null;

try{
    conn = new HttpConnection(Connector.open(myURL));
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod(HttpConnection.POST);
    conn.setRequestProperty("Content-Disposition", "form-data");
    conn.setRequestProperty("Connection", "Keep-Alive");

    out = conn.openOutputStream;
    out.write(buffer, 0, buffer.length);
    conn.setRequestProperty("Content-Length", buffer.length);
    out.flush();
}
catch(Exception e){
    e.printStackTrace();
}
finally{
    if(out != null)
     out.close();

    if(conn != null){
     System.out.println("" + conn.getResponseCode());
     conn.close();
    }
}

EDIT

The same code, when I try it with a string, works fine and sends the string to the server. But it is still a problem with the image bytes.

+2  A: 

A few things that may be missing from your list:

  • use HttpConnection.setRequestMethod(HttpConnection.POST) between 2 and 3.
  • set content length with HttpConnection.setRequestProperty("Content-Length",...) between 5 and 6.
  • knowing the HTTP request response code can help debug your issues: call HttpConnection.getResponseCode() after you've closed the OutputStream but before you close the HttpConnection.
QuickRecipesOnSymbianOS
Thanks. Am already setting the request method. Tried setting the content length to see if it helps. But it doesn't. I am getting a response code of 200 though. Adding code if it helps
lostInTransit
+1  A: 

You most definitely need to set all headers before sending the POST data, including the Content-Length header.

Also, make sure you are sending headers valid for requests, and not response-only headers.

Ed Marty
Thanks Ed. As I mentioned in the other response's comment - I tried after setting all the headers. Still the image is not transferred...
lostInTransit
A: 

You'll need to encode the bytes (preferably Base-64) and send that string. The raw bytes aren't likely to be http safe.

Then on the server, you'll need to decode them back into a byte array and do whatever you were going to do with it (save as file, stuff into database, etc.)

DaveInMaine
+1  A: 

conn = new HttpConnection(Connector.open(myURL));

This line is wrong. Connection is a factory class that creates new Connection objects by looking it's appropriate protocol implementation.

HttpConnection conn = (HttpConnection) Connector.open(myURL);

The rest of the code seems ok. When you're POSTing, at minimun you need to define the content-type and content-length.

beffbernard