HDFS讀取二進(jìn)制文件

gradle依賴

//hadoop
compile("org.apache.hadoop:hadoop-client:3.3.1")

HdfsUtil類

public class HdfsUtil
{
    private FileSystem fileSystem;

    public HdfsUtil()
    {
        init();
    }

    private void init()
    {
        Configuration configuration = new Configuration();
        try
        {
            fileSystem = FileSystem.get(new URI("hdfs://IP:PORT"), configuration, "op");
        }
        catch (IOException e)
        {
            log.error(e.getMessage());
            log.error("HDFS連接異常");
        }
        catch (InterruptedException e)
        {
            log.error(e.getMessage());
            log.error("HDFS中斷異常");
        }
        catch (URISyntaxException e)
        {
            log.error(e.getMessage());
            log.error("URI語法異常");
        }
    }
}

讀取二進(jìn)制文件方法類

public byte[] downloadFile(String path)
    {
        byte[] b = null;
        try
        {
            //獲取文件
            FSDataInputStream fsDataInputStream = fileSystem.open(new Path(path));
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fsDataInputStream);
            //設(shè)置byte長(zhǎng)度
            int len = bufferedInputStream.available();
            b = new byte[len];
            //賦值
            bufferedInputStream.read(b);
            bufferedInputStream.close();
            fsDataInputStream.close();
            fileSystem.close();
        }
        catch (IOException e)
        {
            log.error(e.getMessage());
            log.error("獲取HDFS文件失敗");
        }
        return b;
    }

提供下載接口

    @GetMapping("URL")
    public ResponseEntity<Object> downFileHdfs(@PathVariable String fileName)
    {
        HdfsUtil hdfsUtil = new HdfsUtil();
        byte[] content = hdfsUtil.downloadFile("path"+fileName);
        if (content == null)
        {
            return null;
        }
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", String.format("attachment;filename=%s",fileName));
        headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        ResponseEntity<Object> responseEntity = ResponseEntity.ok()
                .headers(headers)
                //設(shè)置長(zhǎng)度
                .contentLength(content.length)
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                //因?yàn)榉椒ㄊ欠盒?,所以直接傳?                .body(content);
        return responseEntity;
    }
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容