import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.URI;
import javax.swing.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
public class NetDisk extends JFrame{
public static String HDFS_Address = new String("hdfs://localhost:9000/user/Administrator/");
TextField file1 = new TextField(20);//C:\project\firsthadoop\src\WordCount.java
TextField file2 = new TextField(20);//hdfs://localhost:9000/user/Administrator/...
TextField file3 = new TextField(20);
JButton submit = new JButton("上传");
JButton chooser1 = new JButton("浏览");
JButton chooser2 = new JButton("浏览");
JButton chooser3 = new JButton("浏览");
JButton down = new JButton("下载");
JFileChooser jfc = new JFileChooser();
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
lisB btLis = new lisB();
/*
public NetDisk(){
setSize(500,400);
setTitle("网盘");
// p1.setLayout(new GridLayout(2,2));
chooser.addActionListener(new lisB());
p1.add(new JLabel("文件地址:"));
p1.add(file1);
p1.add(chooser);
p1.add(new JLabel("保存地址:"));
p1.add(file2);
add(p1,BorderLayout.NORTH);
submit.addActionListener(btLis);
down.addActionListener(btLis);
//add(submit,BorderLayout.CENTER);
p2.add(submit);
p2.add(down);
add(p2,BorderLayout.CENTER);
setVisible(true);
}
*/
public NetDisk(){
setSize(400,400);
setTitle("网盘");
Box basebox,box1,box2,box3;
chooser1.addActionListener(btLis);
chooser2.addActionListener(btLis);
chooser3.addActionListener(btLis);
file2.setText(HDFS_Address);
submit.addActionListener(btLis);
down.addActionListener(btLis);
box1 = Box.createVerticalBox();
box1.add(new JLabel("文件地址:"));
box1.add(Box.createHorizontalStrut(20));
box1.add(new JLabel("上传到:"));
box1.add(Box.createHorizontalStrut(20));
box1.add(new JLabel("下载到:"));
box2 = Box.createVerticalBox();
box2.add(file1);
box2.add(Box.createHorizontalStrut(20));
box2.add(file2);
box2.add(Box.createHorizontalStrut(20));
box2.add(file3);
box3 = Box.createVerticalBox();
box3.add(chooser1);
box3.add(Box.createHorizontalStrut(20));
box3.add(chooser2);
box3.add(Box.createHorizontalStrut(20));
box3.add(chooser3);
basebox = Box.createHorizontalBox();
basebox.add(box1);
basebox.add(Box.createHorizontalStrut(30));
basebox.add(box2);
basebox.add(Box.createHorizontalStrut(30));
basebox.add(box3);
setLayout(new FlowLayout());
add(basebox);
add(submit);
add(down);
setVisible(true);
}
class lisB implements ActionListener{
public void actionPerformed(ActionEvent e) {
FileSystem fs =null;
InputStream in = null;
OutputStream out = null;
String uri1 = file1.getText();//文件地址
String uri2 = file2.getText();//上传的位置
String uri3 = file3.getText();//下载的位置
Configuration conf = new Configuration();
if(e.getSource() == submit){
try {
in = new BufferedInputStream(new FileInputStream(uri1));
System.out.println("in is OK!");
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
fs = FileSystem.get(URI.create(uri2),conf);
System.out.println("fs is OK!");
out = fs.create(new Path(uri2));
System.out.println("out is OK!");
IOUtils.copyBytes(in, out, 4096,true);
System.out.println("UPLoad is OK!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(e.getSource()==chooser1 || e.getSource() == chooser2 || e.getSource() == chooser3){
int state = jfc.showOpenDialog(null);
File file = jfc.getSelectedFile();
String s = file.getPath();
if(e.getSource() == chooser1)
file1.setText(s);
else if(e.getSource() == chooser2)
file2.setText(s);
else if(e.getSource() == chooser3)
file3.setText(s);
System.out.println(s);
}
else if(e.getSource() == down){
try {
fs = null;
fs = FileSystem.get(URI.create(uri2),conf);
in = null;
in = fs.open(new Path(uri2));
// String s = new String("C:\\hadoopdown\\1.jpeg");
// FileSystem fsout = FileSystem.get(URI.create(uri3), conf);
out = new BufferedOutputStream(new FileOutputStream(uri3));
IOUtils.copyBytes(in,out,4096,true);
System.out.println("down is OK!");
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public static void main(String[] args){
new NetDisk();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
前往页