• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

file download time

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67759
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags.
 
Venkatesh Mullapati
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
It will give me the powers of the gods. Not bad for a tiny ad:
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic