前言
小白一枚,本文简单实现了批量上传遥感影像至HDFS,所以没有实现窗体简单的关闭、缩小、取消等功能。重申这只是简单demo!话不多说直接上代码。
@MarcusPlus
package com.hdfs;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URI;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
public class Demo {
public static void main(String[] args) {
JFrame frame = new JFrame("Demo");
frame.setSize(512, 512);
frame.setLocation(400, 200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JButton selectButton = new JButton("Select Local File");
selectButton.setBounds(10, 80, 80, 25);
panel.add(selectButton);
selectButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jf.setDialogTitle("请选择要上传的文件夹...");
jf.showDialog(null,null);
String srcPath=jf.getSelectedFile().getAbsolutePath()+"/*";
String[] name=jf.getSelectedFile().getAbsolutePath().split("/");
String dstPath="hdfs://127.0.0.1:9000/user/test/"+name[name.length-1]+"/";
if(srcPath.isEmpty()){
System.out.println("请选择本地路径!");
}else
{
try {
listFile(srcPath, dstPath);
} catch (Exception e1) {
e1.printStackTrace();
}
}
System.out.println("Sucess!");
}
});
}
protected static void listFile(String srcPath, String dstPath) throws Exception{
FileSystem fs = null;
FileSystem local = null;
Configuration conf=new Configuration();
URI uri=new URI("hdfs://127.0.0.1:9000");
fs=FileSystem.get(uri,conf);
local=FileSystem.getLocal(conf);
FileStatus[] listFile=local.globStatus(new Path(srcPath), new RegxAcceptPathFilter("^.*tif$"));
Path[] listPath=FileUtil.stat2Paths(listFile);
Path outPath=new Path(dstPath);
boolean result = fs.isDirectory(outPath);
if(result==true){
for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}
}else{
fs.mkdirs(outPath);
System.out.println("创建路径: "+outPath);
for(Path p:listPath){fs.copyFromLocalFile(p, outPath);}
}
}
}
@MarcusPlus
package com.hdfs;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
public class RegxAcceptPathFilter implements PathFilter{
private final String regex;
public RegxAcceptPathFilter(String regex) {
this.regex=regex;
}
@Override
public boolean accept(Path path) {
boolean flag=path.toString().matches(regex);
return flag;
}
}
解释
RegxAcceptPathFilter是用来将本地文件夹下非.tif文件进行过滤掉,以便顺利建立遥感影像库。虽然界面很丑,但是毕竟是个简单demo,别吐槽我啦~