博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot Hadoop HDFS目录文件下载
阅读量:4129 次
发布时间:2019-05-25

本文共 2742 字,大约阅读时间需要 9 分钟。

@RequestMapping(value = "/downDir", method = {RequestMethod.POST, RequestMethod.GET})    public ResponseEntity
downDir(@RequestParam("dirPath") String dirPath, HttpServletRequest request) throws Exception { HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); // 指定返回的zip文件名,这里就直接用时间戳了,可以自己选择 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())); ByteArrayOutputStream zos = downloadDirectory(dirPath); byte[] out = zos.toByteArray(); zos.close(); ResponseEntity
response = new ResponseEntity<>(out, headers, HttpStatus.OK); return response; } //压缩目录 public void compress(String dirPath, ZipOutputStream zipOutputStream, FileSystem fs) throws IOException { // 要下载的目录dirPath FileStatus[] fileStatulist = fs.listStatus(new Path(dirPath)); for (int i = 0; i < fileStatulist.length; i++) { String fileName = fileStatulist[i].getPath().getName(); if (fileStatulist[i].isFile()) { Path path = fileStatulist[i].getPath(); FSDataInputStream inputStream = fs.open(path); zipOutputStream.putNextEntry(new ZipEntry(fileName)); IOUtils.copyBytes(inputStream, zipOutputStream, 1024); inputStream.close(); } else { zipOutputStream.putNextEntry(new ZipEntry(fileName+ "/"));//因为是目录,所以加上/ 就会自动分层 // 这里的substring是因为这个返回的是hdfs://192.168.1.100:9000XXXXX/xxx/xx一大堆, // 而我们要的是/data/img类似这样的目录。 compress(fileStatulist[i].getPath().toString().substring(25), zipOutputStream, fs); } } } //下载一个目录 public ByteArrayOutputStream downloadDirectory(String dirPath) throws IOException, InterruptedException, URISyntaxException { Configuration conf = new Configuration(); FileSystem fs = FileSystem.get(new URI("hdfs://192.168.1.100:9000"), conf, "hadoop"); ByteArrayOutputStream out = new ByteArrayOutputStream(); ZipOutputStream zos = new ZipOutputStream(out); compress(dirPath, zos, fs); zos.close(); return out; }

其中还遇到一些问题,由于流没有关闭,导致下载的zip文件解压的时候是损坏的。所以流记得关很重要。

参考

https://blog.csdn.net/u010366748/article/details/78615004
https://blog.csdn.net/qq_33872191/article/details/84977588
https://blog.csdn.net/liu16659/article/details/79200775
https://www.cnblogs.com/walker-/p/9768834.html
https://blog.csdn.net/qq_41848006/article/details/85786908

你可能感兴趣的文章
poj 1976 A Mini Locomotive (dp 二维01背包)
查看>>
MODULE_DEVICE_TABLE的理解
查看>>
db db2_monitorTool IBM Rational Performace Tester
查看>>
postgresql监控工具pgstatspack的安装及使用
查看>>
【JAVA数据结构】双向链表
查看>>
【JAVA数据结构】先进先出队列
查看>>
Objective-C 基础入门(一)
查看>>
Flutter Boost的router管理
查看>>
iOS开发支付集成之微信支付
查看>>
C++模板
查看>>
【C#】如何实现一个迭代器
查看>>
【C#】利用Conditional属性完成编译忽略
查看>>
VUe+webpack构建单页router应用(一)
查看>>
Node.js-模块和包
查看>>
(python版)《剑指Offer》JZ01:二维数组中的查找
查看>>
Spring MVC中使用Thymeleaf模板引擎
查看>>
PHP 7 的五大新特性
查看>>
深入了解php底层机制
查看>>
PHP中的stdClass 【转】
查看>>
XHProf-php轻量级的性能分析工具
查看>>