Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file download time

 
Venkatesh Mullapati
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 = fis.read( buff )) >= 0 )
sos.write( buff, 0, bytesRead );
fis.close();
sos.close();
}

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.

Regards,
Venkatesh
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#currentTimeMillis()

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.
http://faq.javaranch.com/java/CodeBarnSimpleStream
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
Venkatesh Mullapati
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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? ...

Regards,
Venkatesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic