ITPub博客

首页 > IT职业 > IT图书 > java实现文件的下载的方法概述

java实现文件的下载的方法概述

IT图书 作者:zybing 时间:2021-09-09 09:07:13 0 删除 编辑
 /** 
     * 下载  realName:文件名
     */  
    public static void download(HttpServletRequest request,HttpServletResponse response,String realName) throws Exception {  
        response.setContentType("application/zip");  
        //设置内容作为附件下载  fileName有后缀,比如1.jpg  
        response.setHeader("Content-Disposition", "attachment; filename="+realName); 
        BufferedInputStream bis = null;  
        BufferedOutputStream bos = null;  
        String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "uploadfile/";  
        String downLoadPath = ctxPath+realName;  
        bis = new BufferedInputStream(new FileInputStream(downLoadPath));  
        bos = new BufferedOutputStream(response.getOutputStream());  
        byte[] buff = new byte[2048];  
        int bytesRead;  
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
            bos.write(buff, 0, bytesRead);  
        }  
        bis.close();  
        bos.close();  
    } 

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/855/viewspace-2799690/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-06-02

  • 博文量
    103
  • 访问量
    348205