JAVA HDFS File Upload & Download

Acet-Aminophen·2021년 6월 17일
1

인터넷에 String 값들을 Create 하거나 Read 하는 것들은 있는데 File 단위는 찾기 힘들더라고요.

    public boolean upload(byte[] localFileBytes, String remoteFilePath)
    {
        try
        {
            //대상 파일 경로 특정화 및 생성
            Path remotePath = getPathPath(remoteFilePath);
            FSDataOutputStream fos = fs.create(remotePath);
            
            //쓰기 시작
            fos.write(localFileBytes);
            fos.close();
            return true;
        }
        catch(Exception e)
        {
            return false;
        }
    }
    public boolean download(String localFilePath, String remoteFilePath)
    {
        try
        {
            Path path = getPathPath(remoteFilePath);
            FSDataInputStream fis = fs.open(path);
            File localFile = new File(localFilePath);
            FileOutputStream fos = new FileOutputStream(localFile);

            IOUtils.copy(fis,fos);

            fos.close();
            fis.close();
            return true;
        }catch( Exception e) { e.printStackTrace(); return false; }
    }

0개의 댓글