HDFS下载多个文件并压缩

该代码片段展示了如何从HDFS批量下载多个CSV文件,并将它们压缩成ZIP格式。在循环中,文件从UTF-8转为GBK编码。整个过程涉及配置HDFS连接、设置响应头、创建ZipOutputStream以及读取和写入文件内容。
摘要由CSDN通过智能技术生成
HDFS下载多个文件并压缩
/**
     * Chinese表头,文件格式为csv,其他文件不需要可以删除
     * list为多个文件名
     * where循环中将UTF-8格式转成GBK
     * @param list
     * @param response
     * @param chinese
     * @param excelName
     * @param fileType
     */
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;
                //创建zip实体(一个文件对应一个ZipEntry)
                ZipEntry entry = new ZipEntry(name);
                zos.putNextEntry(entry);
                //文件流循环写入ZipOutputStream
                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);
                }
            }
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值