posts - 54,  comments - 1,  trackbacks - 0
 /**
  * 实现下载
  *
  * 
@param response
  * 
@param headName: 默认文件名
  * 
@param fileUrl :目标文件URL
  * 
@throws Exception
  *
*/

 
public void downLoad(HttpServletResponse response, String headName,
   String fileUrl) 
throws Exception {

  
if (headName.endsWith(".jpg"|| (headName.endsWith(".gif"))) {
   
//no need setting content type and header
  }
 else if (headName.endsWith(".txt")) {
   response.setContentType(
"text;charset=GB2312");
   response.setHeader(
"Content-disposition""attachment; filename="
     
+ headName);
  }
 else {
   response.setContentType(
"application/doc;charset=GB2312");
   response.setHeader(
"Content-disposition""attachment; filename="
     
+ headName);
  }


  String fileURL 
= fileUrl;
  
if (fileURL == null)
   
return;

  
try {
   File file 
= new File(fileUrl);
   FileInputStream bis 
= new FileInputStream(file);
   OutputStream bos 
= response.getOutputStream();

   
byte[] buff = new byte[1024];
   
int readCount = 0;
   
int i = 0;
   readCount 
= bis.read(buff);
   
while (readCount != -1{
    bos.write(buff, 
0, readCount);
    readCount 
= bis.read(buff);
   }

   
if (bis != null)
    bis.close();
   
if (bos != null)
    bos.close();

  }
 catch (Exception e) {
   e.printStackTrace();
   
throw e;
  }

 }



posted on 2005-11-24 17:25 ZhuJun 阅读(332) 评论(0)  编辑  收藏 所属分类: 开发手记

只有注册用户登录后才能发表评论。


网站导航:
 

蜀中人氏,躬耕于珠海

<2005年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

常用链接

留言簿(2)

随笔分类(71)

随笔档案(54)

博客

文档

站点

论坛

搜索

  •  

积分与排名

  • 积分 - 49838
  • 排名 - 981

最新评论

阅读排行榜