/**
*
* @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();
}
}
HDFS 文件下载 通过浏览器
最新推荐文章于 2024-05-12 20:29:05 发布