OutputStream - how many percent are sent?

I use the following construct to transfer data to the server:

byte[] buf =newbyte[5000];

int nread;

int navailable;

synchronized (in){

while((nread = in.read(buf, 0, buf.length)) >= 0){

//Transfer

out.flush();

out.write(buf, 0, nread);

out.flush();

}

}

buf =null;

How many percents are done in every cycle of the while-loop?

How to calculate?

Thank you very much!

With best regards.

Inno

[912 byte] By [Innocentusa] at [2007-11-27 0:46:23]
# 1
You would need to know the size of the incoming data to determine that.
jschella at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 2
I know!the Variable filesize contains the number of bytes of the file!MfGInno
Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 3

> I know!

> the Variable filesize contains the number of bytes of

> the file!

>

That isn't in the code you posted.

If you do have that then

1. Create a 'totalMoved' var set to zero.

2. In the loop increment 'totalMoved' with nread.

3. The percent is then....

double percentMoved = (double)totalMoved/filesize;

jschella at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 4

Like that?

while((nread = in.read(buf, 0, buf.length)) >= 0) {

//Transfer

out.flush();

out.write(buf, 0, nread);

out.flush();

total += nread; //How many bytes already sent?

percentage = (int)( ( total * 100.0 ) / filesize );

//System.out.println("STAT_ sent: "+total+" total: "+filesize);

if(oldpercent < percentage){

//System.out.println("%: " + percentage);

SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");

String uhrzeit = sdf.format(new Date());

System.out.println(uhrzeit+": Bytes sent: "+total);

//listener.setProgressStatus(percentage);

}

oldpercent = percentage;

}

}

buf = null;

With best regards!

Inno

Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 5
> Like that?Does it work? Then yes (it looks like it should work to me.)
jschella at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 6
No, sorry.It goes from 0% to 100% in _one_ second for a file of 2 MB (!)And after 100% it is still transfering data to the server!:-(MfGInno
Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 7

> No, sorry.

> It goes from 0% to 100% in _one_ second for a file of

> 2 MB (!)

> And after 100% it is still transfering data to the

> server!

Yes but that isn't what you asked. You asked....

>How many percents are done in every cycle of the while-loop?

Socket messages are handled by TCP/IP.

If you want to know the percentage on the server then the server is going to have to send that information to you.

jschella at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 8

If you want to know the percentage on the server then the server is going to have to send that information to you.

I saw many normal java-uploaders for doing a multipart-form-data-post

But when there is no other way: Please tell me how to do that with PHP on the serverside?

My provider doesnt provide jsp but php and perl.

With best regards.

Inno

Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 9

> But when there is no other way: Please tell me how to

> do that with PHP on the serverside?

This isn't the correct forum for that question. You may want to try [url=http://forums.devshed.com/]DevShed Developer Forums[/url], which has active PHP and Perl discussion boards.

~

yawmarka at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 10
How to do it in Java? :)MfGInno
Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 11
> How to do it in Java? :) http://jakarta.apache.org/commons/fileupload/using.htmlRead the part about "Watching progress".~
yawmarka at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...
# 12
Thank you!That looks great!But how can I replace my existing URLConnection with this new, better class?Can I do that without restructuring the whole code?I thank you!With best regardsMfGInno
Innocentusa at 2007-7-11 23:12:28 > top of Java-index,Java Essentials,Java Programming...