RSS
热门关键字:  数据挖掘  数据仓库  商业智能  人工智能  搜索引擎

[FW:] Writing big files to ServletOutputStream

来源: 作者:unkonwn 时间:2005-12-18 点击:

Problem when writing big files to ServletOutputStream

I am using the following code to write files to servlet output stream. The problem is the browser is not downloading the entire file when the file size grows above 10 MB. Any help would be greatly helpful.

FileInputStream fis = new FileInputStream(new File(fileName));
response.setHeader("Content-Disposition:", "attachment;filename=" + fileName );

数据挖掘研究院


BufferedInputStream bis= new BufferedInputStream(fis);
ServletOutputStream sos = response.getOutputStream();
byte[] buffer = new byte[5000];
response.setHeader("Content-Length:", String.valueOf (bis.available()));
while (true) {
   int bytesRead = bis.read(buffer, 0, buffer.length);
   if (bytesRead < 0)
   break;
   sos.write(buffer, 0, bytesRead);
}
fis.close();
sos.flush();
sos.close(); 数据挖掘研究院

     Message #141567 Mark as noisy Mark as noisy Mark as noisy Post reply Post reply Post reply Go to top Go to top Go to top

Problem when writing big files to ServletOutputStream

Posted By: Krishnan Muthusubramanian on October 06, 2004 @ 11:45 AM in response to Message #141542 0 replies in this thread
response.setHeader("Content-Length:", String.valueOf (bis.available()));
The "available" method of InputStream does not return the number of bytes that avilable :). Check the Java Doc for this method.

Try new File(fileName).length() instead
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
匿名?