HDFS下载多个文件并压缩
public static void downFileFromHDFS(List<String> list, HttpServletResponse response, String chinese, String excelName, String fileType){
InputStream in = null;
ZipOutputStream zos = null;
OutputStream outputStream =null;
FileSystem fs = null;
try {
String newLine = System.getProperty("line.separator");
Configuration configuration = new Configuration();
fs = FileSystem.get(new URI(HDFS_URI), configuration, "xcloud");
String str = excelName + ".zip";
response.setHeader("Content-Disposition", "attachment;filename=" + str);
response.setContentType("application/zip");
outputStream = response.getOutputStream();
zos = new ZipOutputStream(outputStream);
zos.setEncoding("UTF-8");
byte b[] = chinese.getBytes("GBK");
byte b1[] = newLine.getBytes();
for (int i = 0; i < list.size(); i++) {
in = fs.open(new Path("/你的文件地址/"+list.get(i)));
String name =list.get(i);
byte[] buffer = new byte[1024];
int len = 0;
ZipEntry entry = new ZipEntry(name);
zos.putNextEntry(entry);
zos.write(b,0,b.length);
zos.write(b1,0,b1.length);
while ((len = in.read(buffer)) != -1 ) {
String s = new String(buffer,0,len,"UTF-8");
byte b2[] = s.getBytes("GBK");
zos.write(b2, 0, b2.length);
}
zos.flush();
in.close();
zos.closeEntry();
}
} catch (Exception e) {
log.info("downFileFromHDFS err:",e);
}finally{
if(in!=null){
try {
in.close();
} catch (IOException e) {
log.info("downFileFromHDFS error:",e);
}
}
if(fs!=null){
try {
fs.close();
} catch (IOException e) {
log.info("downFileFromHDFS error:",e);
}
}
if(zos!=null){
try {
zos.close();
} catch (IOException e) {
log.info("downFileFromHDFS err:",e);
}
}
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
log.info("downFileFromHDFS error:",e);
}
}
}
}