码迷,mamicode.com
首页 > Web开发 > 详细

IOUtils方式上传下载文件

时间:2018-10-16 02:04:54      阅读:1005      评论:0      收藏:0      [点我收藏+]

标签:package   out   int   err   except   oop   class   data   size   

package com.css.hdfs04;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.junit.Before;
import org.junit.Test;

/**
 * IOUtils方式上传下载文件
 */
public class HdfsIo {
    Configuration conf = null;
    FileSystem fs =null;
    @Before
    public void init() throws IOException, InterruptedException, URISyntaxException {
        // 1.加载配置
        conf = new Configuration();
        // 2.构造客户端
        fs = FileSystem.get(new URI("hdfs://192.168.146.132:9000/"), conf, "root");
    }
    
    /**
     * 文件上传HDFS
     */
    @Test
    public void putFileToHDFS() throws IllegalArgumentException, IOException{
        // 1.获取输入流
        FileInputStream fis = new FileInputStream(new File("c:/hello.txt"));
        // 2.获取输出流
        FSDataOutputStream fos = fs.create(new Path("/hello.txt"));
        // 3.流的拷贝
        IOUtils.copyBytes(fis, fos, conf);
        // 4.关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }
    
    /**
     * 文件下载HDFS
     */
    @Test
    public void getFileFromHDFS() throws IllegalArgumentException, IOException{
        // 1.获取输入流
        FSDataInputStream fis = fs.open(new Path("/hello"));
        // 2.获取输出流
        FileOutputStream fos = new FileOutputStream(new File("c:/hello"));
        // 3.流的对拷
        IOUtils.copyBytes(fis, fos, conf);
        // 4.关闭资源
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
    }
}

 

IOUtils方式上传下载文件

标签:package   out   int   err   except   oop   class   data   size   

原文地址:https://www.cnblogs.com/areyouready/p/9795442.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!