FileStatus[] srcFileStatus = hdfs.listStatus(new Path(srcPath));执行到这句话的时候会报如下错:
java.lang.NullPointerException
at org.apache.hadoop.fs.Path.(Path.java:104)
at org.apache.hadoop.fs.Path.makeQualified(Path.java:440)
at org.apache.hadoop.hdfs.protocol.HdfsFileStatus.makeQualified(HdfsFileStatus.java:263)
at org.apache.hadoop.hdfs.DistributedFileSystem.listStatusInternal(DistributedFileSystem.java:802)
at org.apache.hadoop.hdfs.DistributedFileSystem.access$700(DistributedFileSystem.java:106)
at org.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:853)
at org.apache.hadoop.hdfs.DistributedFileSystem$18.doCall(DistributedFileSystem.java:849)
at org.apache.hadoop.fs.FileSystemLinkResolver.resolve(FileSystemLinkResolver.java:81)
at org.apache.hadoop.hdfs.DistributedFileSystem.listStatus(DistributedFileSystem.java:849)
at test.HdfsOperate.downloadFile(HdfsOperate.java:142)
at test.HdfsTest.main(HdfsTest.java:45)
代码:
public void downloadFile(String dstPath, String srcPath) throws Exception
{
Path path = new Path(srcPath);
Configuration conf = new Configuration();
FileSystem hdfs = path.getFileSystem(conf);
//hdfs = FileSystem.get(URI.create(srcPath), conf);
File rootfile = new File(dstPath);
if(!rootfile.exists()) rootfile.mkdirs();
hdfs.getFileStatus(path);
if (hdfs.isFile(path))
{
FSDataInputStream in = null;
FileOutputStream out = null;
try
{
in = hdfs.open(path);
File srcfile = new File(rootfile ,path.getName());
if(!srcfile.exists())srcfile.createNewFile();
out = new FileOutputStream(srcfile);
IOUtils.copyBytes(in, out, 4096, false);
}
finally
{
IOUtils.closeStream(in);
IOUtils.closeStream(out);
}
}
else
{
File dstDir = new File(dstPath);
if (!dstDir.exists())
{
dstDir.mkdirs();
}
FileStatus[] srcFileStatus = hdfs.listStatus(path);
Path[] srcFilePath = FileUtil.stat2Paths(srcFileStatus);
for (int i = 0; i < srcFilePath.length; i++)
{
String srcFile = srcFilePath[i].toString();
int fileNamePosi = srcFile.lastIndexOf("/");
String fileName = srcFile.substring(fileNamePosi + 1);
//String fileName = srcFilePath[i].getName();
downloadFile(dstPath + fileName, srcPath + fileName +"/");
}
}
}