|
package com.kaige123;
/**
* @author 凯哥
*/
public class FileFrame extends JFrame {
private JPanel panel = new JPanel();
private JTextField textField = new JTextField();
public String port = ""; // 端口号
private JLabel daaaaaaviLabel = new JLabel();
private JButton button = new JButton();
private JButton button_1 = new JButton();
private JPanel panel_1 = new JPanel();
public JProgressBar progressBar = new JProgressBar();
private JLabel label = new JLabel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileFrame frame = new FileFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FileFrame() {
super();
try {
new FileServer(this).start();
// 获得本机IP地址 InetAddress.getLocalHost().getHostAddress()
label.setText("本机IP:" +
InetAddress.getLocalHost().getHostAddress()
+ " 端口:" +
this.port);
} catch (Exception e1) {
javax.swing.JOptionPane.showMessageDialog(this, "对不起端口占用了!");
System.exit(0);// 强制关闭程序
}
setTitle("凯哥学堂-java小案例-文件传递");
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 338, 209);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
panel.setBorder(new TitledBorder(null, "文件传递",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
panel.setBounds(10, 10, 310, 78);
getContentPane().add(panel);
daaaaaaviLabel.setText("请选择文件地址");
daaaaaaviLabel.setBounds(10, 22, 228, 18);
panel.add(daaaaaaviLabel);
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
// 文件选择窗口
JFileChooser jf = new JFileChooser();// java给我们提供文件选择窗口
jf.setFileSelectionMode(JFileChooser.FILES_ONLY);// 这个设置的是选择什么
// 文件 还夹子
jf.showOpenDialog(FileFrame.this);
String selectFilePath = jf.getSelectedFile().getPath();// 把你选择的文件
// 路径得到
daaaaaaviLabel.setText(selectFilePath);
}
});
button.setText("...");
button.setBounds(244, 17, 56, 28);
panel.add(button);
textField.setText("192.168.1.109:5555");
textField.setBounds(10, 50, 162, 22);
panel.add(textField);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
new FileClient(FileFrame.this, new File(daaaaaaviLabel
.getText()), textField.getText()).start();
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
button_1.setText("发送");
button_1.setBounds(178, 49, 122, 25);
panel.add(button_1);
panel_1.setBorder(new TitledBorder(null, "进度",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
panel_1.setLayout(null);
panel_1.setBounds(10, 98, 310, 61);
getContentPane().add(panel_1);
progressBar.setBounds(10, 25, 290, 24);
panel_1.add(progressBar);
label.setBounds(0, 166, 332, 18);
getContentPane().add(label);
//
}
}
package com.kaige123;
/**
*
* @author 凯哥
*
*/
public class FileFrame extends JFrame {
private JPanel panel = new JPanel();
private JTextField textField = new JTextField();
public String port = ""; // 端口号
private JLabel daaaaaaviLabel = new JLabel();
private JButton button = new JButton();
private JButton button_1 = new JButton();
private JPanel panel_1 = new JPanel();
public JProgressBar progressBar = new JProgressBar();
private JLabel label = new JLabel();
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileFrame frame = new FileFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public FileFrame() {
super();
try {
new FileServer(this).start();
// 获得本机IP地址 InetAddress.getLocalHost().getHostAddress()
label.setText("本机IP:" +
InetAddress.getLocalHost().getHostAddress()
+ " 端口:" +
this.port);
} catch (Exception e1) {
javax.swing.JOptionPane.showMessageDialog(this, "对不起端口占用了!");
System.exit(0);// 强制关闭程序
}
setTitle("凯哥学堂-java小案例-文件传递");
setResizable(false);
getContentPane().setLayout(null);
setBounds(100, 100, 338, 209);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setLayout(null);
panel.setBorder(new TitledBorder(null, "文件传递",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
panel.setBounds(10, 10, 310, 78);
getContentPane().add(panel);
daaaaaaviLabel.setText("请选择文件地址");
daaaaaaviLabel.setBounds(10, 22, 228, 18);
panel.add(daaaaaaviLabel);
button.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
// 文件选择窗口
JFileChooser jf = new JFileChooser();// java给我们提供文件选择窗口
jf.setFileSelectionMode(JFileChooser.FILES_ONLY);// 这个设置的是选择什么
// 文件 还夹子
jf.showOpenDialog(FileFrame.this);
String selectFilePath = jf.getSelectedFile().getPath();// 把你选择的文件
// 路径得到
daaaaaaviLabel.setText(selectFilePath);
}
});
button.setText("...");
button.setBounds(244, 17, 56, 28);
panel.add(button);
textField.setText("192.168.1.109:5555");
textField.setBounds(10, 50, 162, 22);
panel.add(textField);
button_1.addActionListener(new ActionListener() {
public void actionPerformed(final ActionEvent e) {
try {
new FileClient(FileFrame.this, new File(daaaaaaviLabel
.getText()), textField.getText()).start();
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
button_1.setText("发送");
button_1.setBounds(178, 49, 122, 25);
panel.add(button_1);
panel_1.setBorder(new TitledBorder(null, "进度",
TitledBorder.DEFAULT_JUSTIFICATION,
TitledBorder.DEFAULT_POSITION, null, null));
panel_1.setLayout(null);
panel_1.setBounds(10, 98, 310, 61);
getContentPane().add(panel_1);
progressBar.setBounds(10, 25, 290, 24);
panel_1.add(progressBar);
label.setBounds(0, 166, 332, 18);
getContentPane().add(label);
//
}
}
package com.kaige123;
/**
* 用来接收文件的
* @author 凯哥
*
*/
public class FileServer extends Thread {
private FileFrame ff = null;
private ServerSocket server = null;
public FileServer(FileFrame ff) throws Exception {
this.ff = ff;
// 随机安排端口号
ff.port = (5000 + ((int) (Math.random() * 1000))) + "";
server = new ServerSocket(Integer.parseInt(ff.port));
}
public void run() {
while (true) {
try {
Socket socket = server.accept();// 等待客户端给你发送文件 !
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
byte[] b = new byte[1024];
int len = in.read(b);
String clientMsg = new String(b, 0, len);// 字节转换成文字
String fileName = clientMsg.split("\t")[0];
long fileSize = Long.parseLong(clientMsg.split("\t")[1]);
// 提醒客户端有数据发送过来
JOptionPane.showMessageDialog(ff, socket.getInetAddress()
.getHostAddress()
+ " 给你传"
+ fileName
+ " 大小:"
+ fileSize + " 您要接收吗?");
out.write(1);
out.flush();
// 问一下客户把文件要存储在哪里
JFileChooser fileSave = new JFileChooser();
fileSave.setFileSelectionMode(JFileChooser.FILES_ONLY);
fileSave.showSaveDialog(ff);
File saveFilePath = fileSave.getSelectedFile();
ff.progressBar.setMaximum((int) (fileSize / 10000));
long size = 0;
// 把文件输出到本地
FileOutputStream fileOut = new FileOutputStream(saveFilePath);
while ((len = in.read(b)) != -1) {
fileOut.write(b, 0, len);
size += len;
ff.progressBar.setValue((int) (size / 10000));
}
socket.close();
fileOut.close();
javax.swing.JOptionPane.showMessageDialog(ff, "接收成功!");
} catch (Exception e) {
}
}
}
}
工程目录
/fff/src
com.kaige123
/fff/src/com/kaige123/FileClient.java
/fff/src/com/kaige123/FileFrame.java
/fff/src/com/kaige123/FileServer.java
/fff/forms-1.2.0.jar |
|