wood burning stoves 2.0*
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 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "file download time" Watch "file download time" New topic
Author

file download time

Venkatesh Mullapati
Greenhorn

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 = 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


Venkatesh
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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


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

Joined: Jan 10, 2002
Posts: 60077
    
  65

Please UseCodeTags.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Venkatesh Mullapati
Greenhorn

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? ...

Regards,
Venkatesh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file download time
 
Similar Threads
Sending A File From An Applet To A Servlet
Display PDF in IE from byte array using Servlet
file download using servlet and showing progress
Excel file - download
how to safe send file?