Java GUI编程:swing实现上传tiff文件至hdfs功能
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Java GUI编程:swing实现上传tiff文件至hdfs功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                上傳tiff文件至hdfs
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.zxl</groupId><artifactId>swing-demo</artifactId><packaging>jar</packaging><version>1.0-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client --><dependency><groupId>org.apache.hadoop</groupId><artifactId>hadoop-client</artifactId><version>2.7.5</version></dependency></dependencies> </project>主類代碼
package com.zxl.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;/*** //TODO 上傳tiff文件至hdfs** @Description:* @Author: zhangxueliang* @Create: 2021/5/17 14:56* @Version: 1.0**/ public class Demo {private final static String HDFS_SERVER = "hdfs://192.168.101.119:9000";public static void main(String[] args) {// 創(chuàng)建 JFrame 實例JFrame frame = new JFrame("上傳tiff文件至hdfs");// Setting the width and height of frameframe.setSize(512, 512);frame.setLocation(400, 200);frame.setResizable(false);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//創(chuàng)建面板,這個類似于 HTML 的 div 標簽//我們可以創(chuàng)建多個面板并在 JFrame 中指定位置//面板中我們可以添加文本字段,按鈕及其他組件。JPanel panel = new JPanel();// 添加面板frame.add(panel);//調用用戶定義的方法并添加組件到面板placeComponents(panel);// 設置界面可見frame.setVisible(true);}private static void placeComponents(JPanel panel) {// 這邊設置布局為 nullpanel.setLayout(null);// 創(chuàng)建select按鈕JButton selectButton = new JButton("選擇本地TIFF文件");selectButton.setBounds(10, 80, 180, 25);panel.add(selectButton);selectButton.addActionListener(new ActionListener() {@Overridepublic 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 destPath = HDFS_SERVER + "/zxl/test/";//String destPath = HDFS_SERVER + "/zxl/test/" + name[name.length - 1] + "/";if (srcPath.isEmpty()) {System.out.println("請選擇本地路徑!");} else {try {copyFromLocalFile(srcPath, destPath);} catch (Exception e1) {e1.printStackTrace();}}System.out.println("Sucess!");}});}protected static void copyFromLocalFile(String srcPath, String destPath) throws Exception {System.setProperty("HADOOP_USER_NAME","root");FileSystem fs = null;FileSystem local = null;//讀取配置文件Configuration conf = new Configuration();//指定HDFS地址URI uri = new URI(HDFS_SERVER);fs = FileSystem.get(uri, conf);// 獲取本地文件系統(tǒng)local = FileSystem.getLocal(conf);//獲取文件目錄FileStatus[] listFile = local.globStatus(new Path(srcPath), new RegxAcceptPathFilter("^.*tif$"));//獲取文件路徑Path[] listPath = FileUtil.stat2Paths(listFile);//輸出文件路徑Path outPath = new Path(destPath);boolean result = fs.isDirectory(outPath);if (result == true) {//循環(huán)遍歷所有文件路徑for (Path p : listPath) {fs.copyFromLocalFile(p, outPath);}} else {fs.mkdirs(outPath);System.out.println("創(chuàng)建路徑: " + outPath);for (Path p : listPath) {fs.copyFromLocalFile(p, outPath);}}} }過濾器代碼
RegxAcceptPathFilter是用來將本地文件夾下非.tif文件進行過濾掉
package com.zxl.hdfs;import org.apache.hadoop.fs.Path; import org.apache.hadoop.fs.PathFilter; /** * //TODO RegxAcceptPathFilter是用來將本地文件夾下非.tif文件進行過濾掉 * @Description: * @Author: zhangxueliang * @Create: 2021/5/17 15:55 * @Version: 1.0 **/ public class RegxAcceptPathFilter implements PathFilter{private final String regex;public RegxAcceptPathFilter(String regex) {this.regex=regex;}@Overridepublic boolean accept(Path path) {boolean flag=path.toString().matches(regex);return flag;} }運行結果
上傳結果
總結
以上是生活随笔為你收集整理的Java GUI编程:swing实现上传tiff文件至hdfs功能的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Java GUI编程:swing创建窗体
 - 下一篇: Java GUI编程:swing JTr