@RequestMapping("/download")
public void download(HttpServletResponse response,@RequestParam("path") String fileName) throws Exception{
String downFileName=fileName.substring(fileName.lastIndexOf("/"),fileName.length());
response.setContentType("application/force-download");
response.addHeader("Content-Disposition","attachment;fileName="+downFileName+".zip");
dowloadHdfsDir(response.getOutputStream(),fileName);
}
private void dowloadHdfsDir(ServletOutputStream out, String dir) throws Exception{
// System.setProperty("HADOOP_HOME","/Users/hadoop-2.10.0");
// System.setProperty("HADOOP_USER_NAME","hdfs");
// Configuration conf = new Configuration();
String thisPath=this.getClass().getClassLoader().getResource("").getPath();
System.setProperty("java.security.krb5.conf",thisPath+"krb5.conf");
System.setProperty("hadoop.home.dir",thisPath);
UserGroupInformation.loginUserFromKeytab("user",thisPath+"user.keytab");
Configuration conf = new Configuration();
conf.set("hadoop.security.authectication","Kerberos");
FileSystem fs = FileSystem.get(new URI("hdfs://hdfs01:8020/"), conf, "hdfs");
FileStatus files[] = fs.listStatus(new Path(dir));
ZipOutputStream zipOutputStream=new ZipOutputStream(out);
for (FileStatus file : files) {
if(file.isFile()) {
System.out.println(file.getPath());
InputStream in = fs.open(file.getPath());
zipOutputStream.putNextEntry(new ZipEntry(file.getPath().getName()));
IOUtils.copyBytes(in, zipOutputStream, 4096);
in.close();
}
}
out.flush();
zipOutputStream.close();
}
springboot下载hdfs目录文件并压缩为zip
最新推荐文章于 2024-07-08 16:49:39 发布