file download time
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Please UseCodeTags.
Venkatesh Mullapati
Greenhorn
Posts: 2
posted 15 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
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
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 |













