java實現文件下載

xb68 9年前發布 | 5K 次閱讀 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;">可不設置</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();  
        }  


 本文由用戶 xb68 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!