java代码实现文件在hdfs上的上传与下载

public class MyReadAndWrite {
    static FileSystem fs;
    static {
        try {
            fs=FileSystem.get(new URI("hdfs://192.168.56.122:9000"),new Configuration());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }

从hdfs上下载文件

  public void read(){//下载
        try {
            InputStream is = fs.open(new Path("/mydemo/hmm/user103.log"));
            OutputStream os = new FileOutputStream("d://user103.log");
            IOUtils.copyBytes(is,os,4096,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

往hdfs上上传文件

 public void write(){//上传
        try {
            OutputStream os = fs.create(new Path("/mydemo/hmm/user103.log"));
            InputStream is = new FileInputStream("d://user103.log");
            IOUtils.copyBytes(is,os,4096,true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

往hdfs上上传文件

   public static void main(String[] args) {
        MyReadAndWrite mrw = new MyReadAndWrite();
        //mrw.read();
        mrw.write();
    }

你可能感兴趣的:(java代码实现文件在hdfs上的上传与下载)