Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.net.URI;
Import org.apache.hadoop.conf.Configuration;
Import Org.apache.hadoop.fs.FileSystem;
Import Org.apache.hadoop.fs.Path;
Import Org.apache.hadoop.io.IOUtils;
Import org.apache.hadoop.util.Progressable;
public class UploadFile {
public static void Main (string[] args) {
try {
String localsrc = "E://temp/117227.jpg";
String DST = "hdfs://ip:8020/tmp/hadoop-xiaohu/117227.jpg";
InputStream in = new Bufferedinputstream (new FileInputStream (LOCALSRC));
Configuration conf = new configuration ();
FileSystem fs = Filesystem.get (Uri.create (DST), conf);
OutputStream out = fs.create (new Path (DST), new progressable () {
public void Progress () {
System.out.print (".");
}
});
Ioutils.copybytes (in, out, 4096, true);
SYSTEM.OUT.PRINTLN ("Success");
} catch (Exception e) {
E.printstacktrace ();
}
}
}
After successful upload, open 50070 view via browser, select
Browse Directory
The file list is displayed.
This article is from "Learning God linux--star Moon Love" blog, please be sure to keep this source http://xuegodxingyue.blog.51cto.com/5989753/1904018
Uploading files to the HDFs file system via Javaapi