HDFS 文件下载 通过浏览器

/**
 *
 * @param hdfsPath hdfs 服务器路径
 * @param pathName hdfs 文件夹路径
 * @param response
 */
public static void downloadHdfs(String hdfsPath,String pathName,HttpServletResponse response) {

    //"hdfs://SZ-GADZ050200-NN:8020"
    ///user/lingz/ai/69/model/csv"
    List<String> list = new ArrayList();
    Configuration conf = new Configuration();
    URI uri = URI.create(hdfsPath);
    FileSystem hdfs = null;
    Path path = new Path(pathName);
    try {
        hdfs = FileSystem.get(uri, conf, "hdfs");
        FileStatus[] files = hdfs.globStatus(path);
        for (FileStatus file : files) {
            if (file.isDirectory()) {
                RemoteIterator<LocatedFileStatus> iterator = hdfs.listFiles(file.getPath(), false);
                while (iterator.hasNext()) {
                    LocatedFileStatus fileStatus = iterator.next();
                    Path fullPath = fileStatus.getPath();
                    System.out.println(fullPath);
                    FileSystem fs = FileSystem.get(new URI(hdfsPath), new Configuration());
                    InputStream in = fs.open(new Path(fullPath.toString()));
                    //copy到本地磁盘
//                        OutputStream out = new FileOutputStream(downloadPath);
//                        IOUtils.copyBytes(in, out, 4096, true);
                    //copy到本地磁盘end
                    String name = fullPath.getName();
                    //3.设置content-disposition响应头控制浏览器以下载的形式打开文件
                    response.setHeader("content-disposition", "attachment;filename=" + name);
                    //4.获取要下载的文件输入流
//                        InputStream in = new FileInputStream(fullPath.toString());
                    int len = 0;
                    //5.创建数据缓冲区
                    byte[] buffer = new byte[1024];
                    //6.通过response对象获取OutputStream流
                    OutputStream out = response.getOutputStream();
                    //7.将FileInputStream流写入到buffer缓冲区
                    while ((len = in.read(buffer)) > 0) {
                        //8.使用OutputStream将缓冲区的数据输出到客户端浏览器
                        out.write(buffer, 0, len);
                    }
                    in.close();



                }
            } else {
                System.out.println(file.getPath());
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (URISyntaxException e){
        e.printStackTrace();
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值