response 下载文件

        String basePath = "D://test.json";
        String filename = basePath.substring(basePath.lastIndexOf("\\") + 1);  
          
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));  
        InputStream in = null;  
        OutputStream out = null;  
        try {  
            in = new FileInputStream(basePath);  
            int len = 0;  
             byte[] buffer = new byte[1024];  
             out = response.getOutputStream();  
            while((len = in.read(buffer)) > 0) {  
                out.write(buffer,0,len);  
            }  
              
        }catch(Exception e) {  
            throw new RuntimeException(e);  
        }finally {  
            if(in != null) {  
                try {  
                    in.close();  
                }catch(Exception e) {  
                    throw new RuntimeException(e);  
                }  
                  
            }  
        } 

 

posted @ 2016-12-16 13:51  xiaoss  阅读(328)  评论(0编辑  收藏  举报