
Originally Posted by
thiagobrunoms
Hello,
Which kind of connection are u working with!? Web Services or HTTP connection?! This is not an error of the data reading (such as the code u put here). Could u please put here the code that opens and sends the data?!
[]'s
I m working with http connection
My code is as follows:
Code:
private byte[] httpConn() {
HttpConnection conn = null;
OutputStream os = null;
InputStream is = null;
int ch;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] res = null;
try {
System.out.println("url:" + URL);
conn = (HttpConnection) Connector.open(URL,Connector.READ_WRITE);
conn.setRequestMethod(HttpConnection.POST);
InputStream Imgis = getClass().getResourceAsStream(FILE);
byte[] imgData = new byte[Imgis.available()];
Imgis.read(imgData);
String message1 = "";
message1 += CrLf;
message1 += "--AaB03x" + CrLf;
message1 += "Content-Disposition: form-data; name='userfile'; filename='image.jpg'" + CrLf;
message1 += "Content-Type: image/jpeg" + CrLf;
message1 += "Content-transfer-encoding: base64" + CrLf;
message1 += CrLf;
String message2 = "";
message2 += CrLf + "--AaB03x--"+ CrLf;
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) > imgData.length) {
size = imgData.length - index;
}
os.write(imgData, index, size);
index += size;
progress(imgData.length, index); // update the progress bar.
} while (index < imgData.length);
System.out.println("written:" + index);
System.out.println(message2);
os.write(message2.getBytes());
os.flush();
System.out.println("open is");
long len=conn.getLength();
byte[] data = new byte[512];
is = conn.openInputStream();
do {
System.out.println("READ");
len = is.read(data);
if (len > 0) {
System.out.println(new String());
}
} while (len > 0);
os.write(data, 0, 512);
System.out.println("DONE");
}catch(IOException e){
e.printStackTrace();
}finally{
System.out.println("Close connection");
try{
os.close();
}catch(Exception e){}
try{
is.close();
}catch(Exception e){}
try{
conn.close();
}catch(Exception e){}
}
return res;
}
please help me with the solution...
Thanks for replying..