1、Code -> GitHub
https://github.com/liufengji/hadoop_hdfs.git
2、Code
@Test
public void putFileToHDFS() throws Exception{
// 1 创建配置信息对象
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://node1:9000"),configuration, "victor");
// 2 创建输入流
FileInputStream inStream = new FileInputStream(new File("e:/hello.txt"));
// 3 获取输出路径
String putFileName = "hdfs://node1:9000/user/victor/hello1.txt";
Path writePath = new Path(putFileName);
// 4 创建输出流
FSDataOutputStream outStream = fs.create(writePath);
// 5 流对接
try{
IOUtils.copyBytes(inStream, outStream, 4096, false);
}catch(Exception e){
e.printStackTrace();
}finally{
IOUtils.closeStream(inStream);
IOUtils.closeStream(outStream);
}
}