java hdfs 下载_JAVA 从HDFS下载文件和文件夹到网页

本文展示了如何使用JAVA从HDFS下载单个文件和整个文件夹到本地。通过创建`HDFSClient`服务,实现了利用`FileSystem` API打开HDFS文件并将其作为流返回,以及将文件夹压缩成ZIP文件进行下载。在控制器中定义了对应的HTTP接口,允许用户通过网页访问下载链接。
摘要由CSDN通过智能技术生成

测试网页:

控制台调用:

@RestController

@EnableAutoConfiguration

public class HelloController {

//网页访问路径

@RequestMapping(value = "/download", method = {RequestMethod.GET})

public ModelAndView download() {

ModelAndView view = new ModelAndView();

view.setViewName("download");

return view;

}

//下载单文件

@RequestMapping(value = "/down", method = {RequestMethod.POST, RequestMethod.GET})

public ResponseEntitydown(HttpServletRequest request) throws Exception {

HttpHeaders headers = new HttpHeaders();

headers.add("Cache-Control", "no-cache, no-store, must-revalidate");

headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + "mp3");

headers.add("Pragma", "no-cache");

headers.add("Expires", "0");

headers.add("Last-Modified", new Date().toString());

headers.add("ETag", String.valueOf(System.currentTimeMillis()));

HDFSClient hdfsClient = new HDFSClient();

InputStream in = hdfsClient.down1("/DataOwner1/Folder_txt/file.mp3");

return ResponseEntity

.ok()

.headers(headers)

.contentLength(in.available())

.contentType(MediaType.parseMediaType("application/octet-stream"))

// .body(new ZipOutputStream(zipOutputStream));

.body(new InputStreamResource(in));

}

//下载文件夹

@RequestMapping(value = "/downDir", method = {RequestMethod.POST, RequestMethod.GET})

public ResponseEntitydownDir(HttpServletRequest request) throws Exception {

HttpHeaders headers = new HttpHeaders();

headers.add("Cache-Control", "no-cache, no-store, must-revalidate");

headers.add("Content-Disposition", "attachment; filename=" + System.currentTimeMillis() + ".zip");

headers.add("Pragma", "no-cache");

headers.add("Expires", "0");

headers.add("Last-Modified", new Date().toString());

headers.add("ETag", String.valueOf(System.currentTimeMillis()));

String cloudPath = "/DataOwner2/Folder1";

HDFSClient hdfsClient = new HDFSClient();

ByteArrayOutputStream zos = (ByteArrayOutputStream) hdfsClient.down2(cloudPath);

byte[] out = zos.toByteArray();

zos.close();

ResponseEntityresponse = new ResponseEntity<>(out, headers, HttpStatus.OK);

return response;

}

}

具体实现:

@Service

public class HDFSClient{

//单文件

public InputStream down1(String cloudPath) throws IOException, InterruptedException, URISyntaxException {

// 1获取对象

Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");

FSDataInputStream in = fs.open(new Path(cloudPath));

return in;

}

//多文件

public OutputStream down2(String cloudPath) throws IOException, InterruptedException, URISyntaxException {

// 1获取对象

Configuration conf = new Configuration();

FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.102:9000"), conf, "hadoop");

ByteArrayOutputStream out = new ByteArrayOutputStream();

ZipOutputStream zos = new ZipOutputStream(out);

compress(cloudPath, zos, fs);

zos.close();

return out;

}

public void compress(String baseDir, ZipOutputStream zipOutputStream, FileSystem fs) throws IOException {

FileStatus[] fileStatulist = fs.listStatus(new Path(baseDir));

//

System.out.println("basedir = " + baseDir);

String[] strs = baseDir.split("/");

String lastName = strs[strs.length - 1];

//zos = new ZipOutputStream(new FileOutputStream(zipFile));

for (int i = 0; i < fileStatulist.length; i++) {

String name = fileStatulist[i].getPath().toString().substring(25);

name = name.substring(name.indexOf("/"+lastName));

if (fileStatulist[i].isFile()) {

Path path = fileStatulist[i].getPath();

FSDataInputStream inputStream = fs.open(path);

System.out.println("fileStatulist[i].getPath().getName()" + fileStatulist[i].getPath().getName());

zipOutputStream.putNextEntry(new ZipEntry( name));

IOUtils.copyBytes(inputStream, zipOutputStream, 1024);

inputStream.close();// Folder2 1.m3p

} else {

System.out.println(fileStatulist[i].getPath().getName() + "/");

zipOutputStream.putNextEntry(new ZipEntry(name + "/"));

System.out.println("fileStatulist[i].getPath().toString().substring(25) = " + fileStatulist[i].getPath().toString().substring(25));

compress(fileStatulist[i].getPath().toString().substring(25), zipOutputStream, fs);

}

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值