downloading a large file
Below code to download file in jsp application.
What change would give the positive results for downloading a large file.
In below code what would be the value of
[XXXXX]
byte[] buff = new byte[XXXXX];
if I want to download a very large size file about 1000 KB.
response.setContentType(getResponseContent(strFileExtension));
response.setHeader("Content-Disposition","attachment;filename="+strFileNameToDownload);
response.setContentLength(new Long(file.length()).intValue());
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[4 * 1024];
int bytesRead;
while (-1!=(bytesRead=(bis.read(buff,0,buff.length)))) {
bos.write(buff,0,bytesRead);
}
bos.flush();
# 1
It depends.
Add a timer and play with different sizes. Also keep the memory usage in view.
If the file is 1MB then a buffer of 1MB is the fastest, but also most memory consuming (it will take 1MB of memory space).
By the way, setting a buffer size on BufferedInputStream and BufferedOutputStream is completely meaningless. It's already internally buffered with a default buffersize of 8KB.
# 2
Thanks for information.
Actually i have problem in downloading the file.
I get below exception on server side when i download file.
File is downloading without any problem in client browser.
But i get the following error on Server Console
java.lang.IllegalStateException: getOutputStream() has already been called for this response
Any suggestion /reasons for the problem.
# 3
You can send only one response at once. If you're sending any more response (redirect, forward, confirmation page, etc), then you'll get this exception.
You may find this article useful however: http://balusc.xs4all.nl/srv/dev-jep-fil.html