views:

17

answers:

2

Hi,

I need to split an audio or large image file in J2ME before uploading it. How can i split/ break a file in to pieces in JavaME.

Regards,

Imran

A: 

What API for file loading do you use?

1)If FileConnection API you can load data by blocks. There is no problem in this case.

2)If you use Class.getResourceAsStream( String pathInsideJar ) you will have problems. Most of KVMs load resource fully before returning control to your code. So I see one way - to split big file into several small files before creating jar.

Donz
Hi, Thanks for reply, I am using FileConnection API. Can you please send me a sample code. When i upload a Post a large file to the web application gives error.
Imran Iqbal
A: 

DataInputStream dis = FileConnection.getDataInputStream();

byte[] buffer = new byte[2048];

int count;

int total = 0;

Vector v = new Vector();

ByteArrayOutputStream baos = new ByteArrayOutputStream();

while( ( count = dis.read( buffer ) ) >= 0 )

{

total += count;

baos.write( buffer, 0, count );

if( total > 100000 )

{

baos.close();

byte[] data = baos.toByteArray();

v.addElement( data );

baos = new ByteArrayOutputStream();
}

}

So you will have Vector of several byte arrays. And you can send it one by one.

Or read all file data into one byte array and post this data by parts with shifting start position of sending data.

Donz