This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Servlets and the fly likes file download time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "file download time" Watch "file download time" New topic

file download time

Venkatesh Mullapati

Joined: Aug 05, 2009
Posts: 2
Hi All,

I have written a method in a sevlet that called and exicuted when we click on the download URL from the broser. Here is the code ...

static private void downloadFile( HttpServletRequest req, HttpServletResponse res,User user, Connection conn, File file,
String filename ) throws FileNotFoundException, IOException
res.setContentType( "application/octet-stream" );
res.setContentLength( (int) file.length() );
res.setStatus( 200 );
byte[] buff = new byte[32768];
int bytesRead;
FileInputStream fis = new FileInputStream( file );
ServletOutputStream sos = res.getOutputStream();
Audit.log( Audit.AUDIT_DOWNLOADSWAPS, "Downloading '" + file.getName() + "'." );
while( (bytesRead = buff )) >= 0 )
sos.write( buff, 0, bytesRead );

Now i need to track the total download time of the file and log it into a app log file.
How can i track the download time of a file in this code? Please help me.


Ben Souther

Joined: Dec 11, 2004
Posts: 13410


Grab the time before you start, and after you're done.
Do the math.

You might want to add a finally block and put your code to close that input stream you've created.
Otherwise, you could end up with a lock on that file that doesn't go away until your app stops.

We have some sample code in our code barn that streams file data from a servlet.

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63874

Please UseCodeTags.

[Asking smart questions] [About Bear] [Books by Bear]
Venkatesh Mullapati

Joined: Aug 05, 2009
Posts: 2
Hi Ben,

Thanks for your reply.

I tried the same way what you have suggested. I took System.currentTimeMillis() as a Long type variable before the line FileInputStream fis = new FileInputStream( file ) and after the line sos.close(). It is showing the difference is 0.

I think sos.write( buff, 0, bytesRead ) is just writing the file content to the ServletOutputStream, i.e browser. However it won't be the actual download functionality. Once we click on the URL in a browser, downloading the file is browser specific and it would pop-up a download dialog window and start downloading from the source. I just wanted to capture exactly this downloading time.

Is it possible from the servlet? ...

I agree. Here's the link:
subject: file download time
It's not a secret anymore!