I am trying to upload a wav file to server using j2me httpconnection. However I am not able to do it. It throws null pointer exception when it's writing the bytes to the server.
Sample is below.
Exception is thrown at os.write(postBytes);
line. Anyone with any idea? And the file I am uploading is test.pcm
.
public String send() throws Exception
{
form.append("Inside Send()");
bos = new ByteArrayOutputStream();
try
{
form.append("Making connections");
hc = (HttpConnection) Connector.open(url, Connector.READ_WRITE);
form.append("length: " + postBytes.length + "");
hc.setRequestProperty("Content-Length", "100573");
hc.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + getBoundaryString());
hc.setRequestMethod(HttpConnection.POST);
//Content-Length: 27
os = hc.openOutputStream();
form.append("Writing bytes to Server" + postBytes);
try {
os.write(postBytes);
os.flush();
form.append("Uploading done and closing output connection");
form.append("Waiting for response");
int ch;
is = hc.openInputStream();
while ((ch = is.read()) != -1)
{
bos.write(ch);
}
form.append("Response wrote to byte array");
//res = bos.toByteArray();
res = bos.toString();
}
catch(Exception e) {
form.append("Exception during write: " + e.getMessage());
}
}
catch(Exception e)
{
form.append("Expcetion caught1: " + e.getMessage());
//e.printStackTrace();
}
finally
{
try
{
if(bos != null)
bos.close();
if(is != null)
is.close();
if(hc != null)
hc.close();
}
catch(Exception e2)
{
form.append("Expcetion caught2: " + e2.getMessage());
}
}
return res;
}
My class that used to send files to Server: