Hi,
I am trying to upload files from a mobile device. The code is as under:
FileConnection fc = (FileConnection)Connector.open("file://localhost/" + path);
if(!fc.exists()) {
fc.create();
}
fc.setReadable(true);
InputStream input = fc.openInputStream();
//Creates an input stream from File Connection.
HttpConnection hc = null;
InputStream is = null;
hc = (HttpConnection) Connector.open(url);
OutputStream dout = hc.openOutputStream();
int read;
byte[] buffer = new byte[1024];
// reading from the input stream and writing to the output stream.
while ((read = input.read(buffer,0,1024)) != -1) {
System.out.println("writing");
dout.write(buffer, 0, read);
}
Now, the above code works very well with the emulator. It could upload files upto 1 mb.
However, when i transfered it to N95, and tried connecting over the operator's gateway, the request reached the server, but the server could not read any bytes of data from the ServletInputStream.
Now, when i connected the n95 over my wireless LAN, the code worked just fine.
Here is the server code for reading the Streams:
System.out.println("invoked");
try
{
ServletInputStream sis = request.getInputStream();
int l;
byte abyte1[] = new byte[512];
ByteArrayOutputStream binArray = new ByteArrayOutputStream();
while((l = sis.read(abyte1)) != -1)
{
System.out.println("Reading Servlet input stream"+l);
binArray.write(abyte1, 0, l);
}
String path = AppConstants.LOCAL_CONTEXT_PATH+"//upldDOC001.mp3";
OutputStream targetFile = new FileOutputStream(path);
//System.out.println(new String(binArray.toByteArray()));
targetFile.write(binArray.toByteArray());
targetFile.close();
System.out.println("File written");
}catch(Exception e)
{
e.printStackTrace();
}
Again, if some one has a robust code to upload files without causing an out of memory, please help !!
Thanks,
Sanket

Reply With Quote

