0

I am trying to send an image to server. I converted the image to a byte array. I then tried the code from "HTTP Post multipart file upload in Java ME" on the Nokia developer forums.

I also tried the code from "HTTP POST and passing parameters in URLs" on the BlackBerry forums.

I am passing parameters and getting response code 200. But image is not sent to the server. I am stuck on this.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
Hetal
  • 11
  • 3
  • 1
    It would be useful to include a small, relevant section of the code you are using. Response code 200 is success, so the server was able to handle the request. – Michael Donohue Oct 17 '11 at 23:57

1 Answers1

1
try{

FileConnection fis=(FileConnection)Connector.open(filename);
InputStream inputStream = fis.openInputStream();

ByteArrayOutputStream bos=new ByteArrayOutputStream();
int buffersize=1024;
byte[] buffer=new byte[buffersize];
int length=0;
while((length=inputStream.read(buffer))!=-1)
{
    bos.write(buffer,0,length);
}
byte[] imagedata=bos.toByteArray();

HttpConnection conn = (HttpConnection) Connector.open(Url, Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE,                    
    HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA
                        + ";boundary=" + boundary);

    conn.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH,
                String.valueOf(imagedata.length));
conn.setRequestProperty("x-rim-transcode-content", "none");

ByteArrayOutputStream out = new ByteArrayOutputStream();
OutputStream finalOut = conn.openOutputStream();

String newLine = "\r\n";
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write(newLine.getBytes());
String contDisp = "Content-Disposition:form-data;  
    name=\"file\";filename=\"Image.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type = "Content-Type:image/jpeg";
out.write(contDisp.getBytes());
out.write(newLine.getBytes());
out.write(type.getBytes());
out.write(newLine.getBytes());
out.write(contEnc.getBytes());
out.write(newLine.getBytes());
out.write(newLine.getBytes());
out.write(imagedata);
out.write(newLine.getBytes());
out.write("--".getBytes());
out.write(boundary.getBytes());
out.write("--".getBytes());
out.write(newLine.getBytes());
finalOut.write(out.toByteArray());

out.flush();
out.close();

finalOut.flush();
finalOut.close();
InputStream instream=conn.openInputStream();
int ch=0;
StringBuffer buffesr=new StringBuffer();
while((ch=instream.read())!=-1)
{
    buffesr.append((char)ch);
}



catch (Exception e) {
    // TODO: handle exception
}
V.J.
  • 9,492
  • 4
  • 33
  • 49
  • Try this i have successfully pass image to the server with this code. – V.J. Oct 17 '11 at 12:03
  • Hello Vijay, Thanks for your answer, but I am not able to understand that which URL we have to passed? can u give me the example for that? Thanks. – Hetal Oct 17 '11 at 13:11
  • Your server address where you want to upload your image. Example: http://www.********.com/............... – V.J. Oct 17 '11 at 13:16
  • at where you want to upload image give me your address i will set in this code. – V.J. Oct 17 '11 at 13:16
  • This is the server path: http://74.208.77.106/jm/testing/FILENAME.jpeg. And what is boundry? Give me that string. Do I need to pass parameters in URL? – Hetal Oct 17 '11 at 13:20
  • from where u get the image. from device then first you get the image from the file and then pass the file to the Url. – V.J. Oct 17 '11 at 13:29
  • Hey I tried. Now I am getting the response that 414 Request-URI Too Large. Image is created on server, but size is 0 KB. So no preview available. – Hetal Oct 18 '11 at 11:49