黑马程序员技术交流社区

标题: 文件下载之小记 [打印本页]

作者: hejinzhong    时间: 2014-9-12 21:23
标题: 文件下载之小记
本帖最后由 hejinzhong 于 2014-9-12 21:31 编辑

  1. public class DownLoadServlet extends HttpServlet {

  2.         public void doGet(HttpServletRequest request, HttpServletResponse response)
  3.                         throws ServletException, IOException {
  4.                 doPost(request, response);
  5.         }

  6.         public void doPost(HttpServletRequest request, HttpServletResponse response)
  7.                         throws ServletException, IOException {
  8.                
  9.                 String fileName = "Xxx";
  10.                
  11.                 /*
  12.                  * 这里:是利用http协议为ISO-8859-1和计算机系统为GBK编码为基础
  13.                  */
  14.                 fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");
  15.                
  16.                 //这个是提示浏览器进行下载操作
  17.                 response.setHeader("content-disposition","attachment;filename="+fileName);
  18.                
  19.                 /*进行文件传输,这里将文件默认放在当前项目目录下
  20.                  * 根据下面源码可知,返回值为传递字节数
  21.                  */
  22.                
  23.                 IOUtils.copy(this.getServletContext().getResourceAsStream(fileName),
  24.                                 response.getOutputStream());
  25.                
  26.                
  27.                 /*
  28.                          这里是直接将文件读取流中的内容写入服务器的反馈流中
  29.                         
  30.                          private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
  31.                          private static final int EOF = -1;
  32.                         public static int copy(InputStream input, OutputStream output)
  33.                                          throws IOException {
  34.                         long count = copyLarge(input, output);
  35.                         if (count > Integer.MAX_VALUE) {
  36.                             return -1;
  37.                         }
  38.                         return (int) count;
  39.                     }
  40.                    
  41.                     public static long copyLarge(InputStream input, OutputStream output)
  42.                             throws IOException{
  43.                                 return copyLarge(input, output, new byte[DEFAULT_BUFFER_SIZE]);
  44.                     }
  45.                        
  46.                         public static long copyLarge(InputStream input, OutputStream output, byte[] buffer)
  47.                             throws IOException {
  48.                         long count = 0;
  49.                         int n = 0;
  50.                         while (EOF != (n = input.read(buffer))) {
  51.                             output.write(buffer, 0, n);
  52.                             count += n;
  53.                         }
  54.                         return count;
  55.                     }
  56.                  */
  57.         }

  58. }
复制代码






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2