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);  
                }  
                  
            }  
        } 
原文地址:https://www.cnblogs.com/zsslll/p/6186568.html