Hi Gurus
I'm trying to post a jpg image to a web server. I've tested my PHP script at the server and I'm being able to upload an image using a form. Now I'm trying to make a Blackberry application to POST the image to the server using the same script however when I test the Java code, PHP tells me that NOTHING was POSTed, I'm not sure what I'm doing wrong but I'm doing something like this:
String mBoundary = "SUPSUPSUPSUP";
/* Preparar objeto a enviar */
InputStream mImagen = this.getClass().getResourceAsStream("sample.jpg");
byte[] mBytesPostear = IOUtilities.streamToBytes(mImagen);
HttpConnection mConexion = (HttpConnection) Connector.open("http://www.raspberry.com/script.php");
/* Preparar headers del POST. Es multiformulario con POST de un archivo */
mConexion.setRequestMethod(HttpConnection.POST);
mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_TYPE, HttpProtocolConstants.CONTENT_TYPE_MULTIPART_FORM_DATA + ";boundary=" + mBoundary);
mConexion.setRequestProperty(HttpProtocolConstants.HEADER_CONTENT_LENGTH, String.valueOf(mBytesPostear.length));
mConexion.setRequestProperty( "x-rim-transcode-content", "none" );
/* Preparar contenido de salida */
ByteArrayOutputStream mOutput = new ByteArrayOutputStream();
OutputStream mOS = mConexion.openOutputStream();
/* Escribir contenido */
String nuevaLinea = "\r\n";
String contDisp="Content-Disposition:form-data; name=\"foto\";filename=\"sample.jpg\"";
String contEnc = "Content-Transfer-Encoding: binary";
String type="Content-Type:image/jpeg";
mOutput.write(nuevaLinea.getBytes());
mOutput.write("--".getBytes());
mOutput.write(mBoundary.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(contDisp.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(type.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(contEnc.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(nuevaLinea.getBytes());
mOutput.write(mBytesPostear);
mOutput.write(nuevaLinea.getBytes());
mOutput.write("--".getBytes());
mOutput.write(mBoundary.getBytes());
mOutput.write("--".getBytes());
mOutput.write(nuevaLinea.getBytes());
/**********************/
/* Escribir el contenido */
mOS.write(mOutput.toByteArray());
mOutput.flush();
mOutput.close();
mOS.flush();
mOS.close();
/* Recibir respuesta del servidor */
InputStream mIS = mConexion.openInputStream();
int mLongitud = (int) mConexion.getLength();
if (mLongitud > 0) {
int mActual = 0;
int mBytesLeidos = 0;
byte[] mBytes = new byte[mLongitud];
while ((mBytesLeidos != mLongitud) && (mActual != -1)){
mActual = mIS.read(mBytes, mBytesLeidos, mLongitud - mBytesLeidos);
mBytesLeidos += mActual;
}
String mRespuesta = new String(mBytes);
System.out.println("Respuesta: " + mRespuesta);
}
I just tried to clone the header that is sent by Chrome when I use the form, I think they have the same information.
My PHP script first checks if something was posted, if nothing was posted, then it returns a message so I'm able to "consume" the script in the web server and I can see that the Blackberry device is uploading the data but the answer is that nothing was posted.
I think I'm sending the info to the server in a wrong format.
Any help would be greatly appreciated.
Thanks!