브라우저를 통해 HDFS 파일 다운로드

2522 단어 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 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 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();
    }

}

좋은 웹페이지 즐겨찾기