细心!用心!耐心!

吾非文人,乃市井一俗人也,读百卷书,跨江河千里,故申城一游; 一两滴辛酸,三四年学业,五六点粗墨,七八笔买卖,九十道人情。

BlogJava 联系 聚合 管理
  1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
 1 package  com.stt.doss.common.util.upload;
 2
 3 import  java.io.BufferedOutputStream;
 4 import  java.io.FileInputStream;
 5 import  java.io.FileNotFoundException;
 6 import  java.io.IOException;
 7 import  java.io.UnsupportedEncodingException;
 8
 9 import  javax.servlet.http.HttpServletResponse;
10
11 import  com.stt.doss.util.Convertor;
12
13 /**
14  * <p>
15  * Title:
16  * </p>
17  * <p>
18  * Description:处理下载文件工具类
19  * </p>
20  * <p>
21  * Copyright: Copyright (c) 2006
22  * </p>
23  * <p>
24  * Company: stt
25  * </p>
26  * 
27  *  @author  william
28  *  @version  1.0
29   */

30 public   class  DownloadHelper  {
31
32      /**
33      * 下载文件
34      * 
35      *  @param  response
36      *  @param  fileName
37      *  @param  filePath
38       */

39      public   static   void  downloadFile(HttpServletResponse response, String fileName,
40             String fileAllPath)  {
41
42         StringBuffer sb  =   new  StringBuffer( 150 );
43         sb.append( " attachment;  filename= " );
44         sb.append(Convertor.iso2gbk(fileName));
45
46          if  ( null   !=  fileAllPath  &&  fileName  !=   null {
47             FileInputStream fis  =   null ;
48             BufferedOutputStream bos  =   null ;
49              try   {
50                 response
51                         .setContentType( " application/x-msdownload;charset=GB2312 " );
52
53                 response.setHeader( " Content-Disposition " new  String(sb
54                         .toString().getBytes(),  " ISO-8859-1 " ));
55                 fis  =   new  FileInputStream(Convertor.iso2gbk(fileAllPath));
56                 bos  =   new  BufferedOutputStream(response.getOutputStream());
57                  byte [] buffer  =   new   byte [ 2048 ];
58                  while  (fis.read(buffer)  !=   - 1 {
59                     bos.write(buffer);
60                 }

61                 bos.write(buffer,  0 , buffer.length);
62                  //  fis.close();
63                  //  bos.close();
64             }
  catch  (UnsupportedEncodingException e)  {
65                  //  TODO Auto-generated catch block
66                 e.printStackTrace();
67                  throw   new  RuntimeException();
68             }
  catch  (FileNotFoundException e)  {
69                  //  TODO Auto-generated catch block
70                 e.printStackTrace();
71                  throw   new  RuntimeException();
72             }
  catch  (IOException e)  {
73                  //  TODO Auto-generated catch block
74                 e.printStackTrace();
75                  throw   new  RuntimeException();
76             }
  finally   {
77                  try   {
78                      if  (fis  !=   null {
79                         fis.close();
80                     }

81                      if  (bos  !=   null {
82                         bos.close();
83                     }

84                 }
  catch  (IOException e)  {
85                      //  TODO Auto-generated catch block
86                     e.printStackTrace();
87                      throw   new  RuntimeException();
88                 }

89
90             }

91         }

92
93     }

94 }

95
posted on 2007-03-16 15:26 张金鹏 阅读(192) 评论(0)  编辑  收藏 所属分类: core java中的一些数据结构的处理

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


网站导航: