@RequestMapping(value = "/downDir", method = {RequestMethod.POST, RequestMethod.GET})
public ResponseEntity<byte[]> 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<byte[]> 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