import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
public class MainFrame extends JFrame implements ActionListener{
private JButton sendFileBtn;
private JButton exitBtn;
private final int port = 2233;
private int x;
private int y;
public MainFrame() {
// TODO Auto-generated constructor stub
this.setTitle("小型局域网点对点文件传输程序1.0");
this.setSize(300, 200);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
x = (Toolkit.getDefaultToolkit().getScreenSize().width - this.getSize().width) / 2;
y = (Toolkit.getDefaultToolkit().getScreenSize().height - this.getSize().height) / 2;
this.setLocation(x, y);
sendFileBtn = new JButton("发送文件");
sendFileBtn.addActionListener(this);
String pcIP = new String();
// 获取本机IP地址
try {
InetAddress ia = InetAddress.getLocalHost();
//pcName = ia.getHostName();
pcIP = ia.getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exitBtn = new JButton(" 退出 ");
exitBtn.addActionListener(this);
// 布局
JPanel p = new JPanel(new GridLayout(4,1));
p.add(new JLabel(" 本机IP地址:"+pcIP));
p.add(new JLabel(" 默认发送端口:"+port));
p.add(new JLabel(" 接受文件存放到程序根目录下"));
Panel p1 = new Panel();
p1.add(sendFileBtn);
p1.add(exitBtn);
p.add(p1);
this.add(p);
this.setVisible(true);
ReceiveFile rf = new ReceiveFile(2233, this);
Thread server = new Thread(rf);
server.start();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MainFrame mf = new MainFrame();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource() == sendFileBtn){
// 选择要发送的文件
SendDialog sd = new SendDialog(this, port, x + 20, y + 20);
}
else if(e.getSource() == exitBtn){
System.exit(0);
}
}
}
/**
* @(#)receiveFile.java 1.82 99/03/18
*
* Copyright (c) 1994-1999 Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*/
import java.io.*;
import java.net.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
/**
* 一个用于接收的类
*
* @version 1.82 2013-11-25
* @author Zhang Zhenjia
*/
public class ReceiveFile implements Runnable{
int port; // 服务端口
int count = 0; // 用于统计客户端数目
ServerSocket serSocket; // 服务器Socket
Socket socket;
JFrame frame;
public ReceiveFile(int port, JFrame frame) {
// TODO Auto-generated constructor stub
this.frame = frame;
this.frame = frame;
try {
serSocket = new ServerSocket(port);
System.out.println("服务器:开始监听2233端口");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* 软件常驻进程,监听端口
*
*
*/
public void run(){
while (true) {
System.out.println("服务器:进程开启,接受客户端");
byte[] b = new byte[1024];
try {
socket = serSocket.accept();
InputStream receiveFile = socket.getInputStream(); // 接收文件流
DataInputStream getMsg = new DataInputStream(receiveFile); // 接受消息
DataOutputStream sendMsg = new DataOutputStream(socket.getOutputStream()); //发送消息
String fileName = getMsg.readUTF();
int n = JOptionPane.showConfirmDialog(frame, "是否接"+fileName+"(IP:"
+ socket.getRemoteSocketAddress() + ")文件");
if (n == 0) {
sendMsg.writeUTF("yes");
File file = new File("\\Transmission\\1"+fileName);
FileOutputStream writeFile = new FileOutputStream(file); // 本地文件写入流
int len;
while ((len = receiveFile.read(b)) != -1) {
writeFile.write(b, 0, len); // 写入文件
}
System.out.println("服务器:接受文件完毕");
writeFile.flush();
writeFile.close();
} else {
System.out.println("服务器:拒绝");
sendMsg.writeUTF("no");
}
receiveFile.close();
getMsg.close();
sendMsg.close();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
// closePort();
}
}
}
/**
*
* 关闭端口,关闭服务器
*
*/
public void closePort(){
try {
serSocket.close();
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("receive.jpg");
ReceiveFile rf = new ReceiveFile(2233, null);
Thread t = new Thread(rf);
t.start();
}
}
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
public class SendDialog extends JDialog implements ActionListener{
int port; // 默认端口
String ip = new String("127.0.0.1");// IP
File file = null;
JTextField ipField = new JTextField(ip,22);// IP地址输入栏
JTextField pathField = new JTextField(18);// 文件路径输入栏
JButton browseBtn = new JButton("...");// 浏览按钮
JButton sendBtn = new JButton("发送");// 发送按钮
JButton cancelBtn = new JButton("取消");// 取消按钮
JLabel label = new JLabel(" ");// 状态栏按钮,可换成进度条
public SendDialog(JFrame frame, int port, int x, int y) {
// TODO Auto-generated constructor stub
super(frame,"发送文件",true);
this.port = port;
this.setBounds(x, y, 320, 150);
this.setResizable(false);
// 设置tf2文本框不可编辑
pathField.setEditable(false);
browseBtn.addActionListener(this);
sendBtn.addActionListener(this);
// sendBtn.setEnabled(false);
cancelBtn.addActionListener(this);
JPanel p = new JPanel();
p.add(new JLabel("对方IP:"));
p.add(ipField);
p.add(new JLabel(" 文件:"));
p.add(pathField);
p.add(browseBtn);
p.add(sendBtn);
p.add(cancelBtn);
p.add(label);
this.add(p);
this.setVisible(true);
}
// 检测输入IP地址的合法性
private boolean chkIP(String ip){
String regex = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
+ "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";
// 判断ip地址是否与正则表达式匹配
if (ip.matches(regex)) {
return true;
}else{
return false;
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
/**
* ...浏览
*
* 获取文件名
*
*/
if(e.getSource() == browseBtn){
JFileChooser jfc = new JFileChooser();
int returnVal = jfc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = jfc.getSelectedFile();
pathField.setText(file.getPath());
}
}
/**
* ...发送
*
*/
else if(e.getSource() == sendBtn){
if(chkIP(ipField.getText()) && (file != null)){
SendFile sf = new SendFile(ip, port, file);
if(sf.sendFile()){
JOptionPane.showMessageDialog(this, "发送完毕");
}else{
JOptionPane.showMessageDialog(this, "对方拒绝接收");
}
}
else if(file == null){
JOptionPane.showMessageDialog(null, "您没有选择文件");
}
else{
JOptionPane.showMessageDialog(null, "您输入的IP地址有误。");
}
}
/**
* ...取消
*
*/
else if(e.getSource() == cancelBtn){
this.dispose();
}
}
}
/**
* @(#)SendFile.java 1.82 99/03/18
*
* Copyright (c) 1994-1999 Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
* All rights reserved.
*
* This software is the confidential and proprietary information of Sun
* Microsystems, Inc. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Sun.
*/
import java.io.*;
import java.net.*;
/**
* 一个用于文件发送的类
*
* @version 1.82 2013-11-25
* @author Zhang Zhenjia
*/
public class SendFile {
String IP = null;// 远程的IP字符串
File file = null;// 欲发送的文件
int port;// 远程的服务端口
Socket socket; // 客户端Socket
public SendFile(String IP, int port, File file) {
// TODO Auto-generated constructor stub
this.IP = IP;
this.port = port;
this.file = file;
}
public boolean sendFile() {
boolean state = false;
// 与服务端建立连接
try {
socket = new Socket(IP, port);
System.out.println("客户端:连接"+IP+":"+port);
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
byte[] b = new byte[1024]; // 包的大小
try {
FileInputStream readFile = new FileInputStream(file); // 本地文件读取流
OutputStream sendStream = socket.getOutputStream(); // 发送文件流
DataOutputStream sendMsg = new DataOutputStream(sendStream); //发送消息
DataInputStream getMsg = new DataInputStream(socket.getInputStream()); //接受消息
System.out.println("客户端:"+file.getName()+" . "+file.length());
sendMsg.writeUTF(file.getName());
if(getMsg.readUTF().equals("yes")){
int len; // 文件长度
while((len = readFile.read(b)) != -1){
sendStream.write(b, 0, len); // 开始发送文件
}
System.out.println("客户端:发送完毕");
state = true;
}else{
System.out.println("客户端:服务器拒绝接收");
//JOptionPane.showMessageDialog(null, "对方拒绝接受");
}
readFile.close();
sendStream.flush();
sendStream.close();
socket.close();
System.out.println("客户端:进程结束");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e.getMessage());
e.printStackTrace();
}
return state;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("G:\\Transmission\\example.jpg");
SendFile sf = new SendFile("127.0.0.1", 2233, file);
sf.sendFile();
}
}