java实现文件下载

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

文件下载

输出内容包含 

1.文件内容:content

2. 输出类型 contentType : application/msword

3 文件长度: contentLength: content.length

4.文件名称:

    /** 
         * 下载文件 
         * @param request 
         * @param response 
         * @throws IOException 
         * @throws InterruptedException 
         */  
        public void downloadDoc(HttpServletRequest request,HttpServletResponse response) throws IOException, InterruptedException  
        {  
            String id=request.getParameter("id")==null?"0":request.getParameter("id");  
            DocumentAtt documentAtt=documentAttDao.findById(id); //业务对象根据实际情况修改  
            byte [] content=documentAtt.getFiles();  
            OutputStream os=response.getOutputStream();  
            InputStream is=new  ByteArrayInputStream(content);   
            response.setContentType(documentAtt.getFiletype());//<span style="font-family: Arial, Helvetica, sans-serif;">可不设置</span>  
            response.setContentLength(content.length);//可不设置  
            response.setHeader("Content-Disposition","attachment;filename="+new String(documentAtt.getName().getBytes("GBK"),"ISO-8859-1"));  
            byte[] buffer = new byte[4000];  
            int length;  
            while((length = is.read(buffer)) != -1){  
                  os.write(buffer,0,length);  
            }  
                
            is.close();  
            os.close();  
        }