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
You would need to know the size of the incoming data to determine that.
I know!the Variable filesize contains the number of bytes of the file!MfGInno
> 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;
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
> Like that?Does it work? Then yes (it looks like it should work to me.)
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
> 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.
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
> 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.
~
How to do it in Java? :)MfGInno
> How to do it in Java? :) http://jakarta.apache.org/commons/fileupload/using.htmlRead the part about "Watching progress".~
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