java实现文件下载详解编程语言

文件下载

输出内容包含 

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;">可不设置   
            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();   
        }  

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/10882.html

(0)
上一篇 2021年7月19日 10:31
下一篇 2021年7月19日 10:31

相关推荐

发表回复

登录后才能评论