📄 67.txt
字号:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
// 网络文件传输 类定义
public class FileTransfer
{
// 方法入口
public static void main(String[] args)
{
// 构建对象
FileTransfer file = new FileTransfer();
// 传输文件 逻辑控制
file.Transfer();
}
// 静态常量、变量设置
// 面板大小]
public static final int WIDTH = 600;
public static final int HEIGHT = 85;
// 联网配置: 端口号;发送方IP;发送接收标志;传输字节流大小
private static final int PORT = 8987 ;
private static Socket socket;
private static String address ;
private static boolean isSend;
private static String path;
private static final int TRANSFER_SIZE = 8000;
//控件变量
private static JTextField field ;
private static JFrame frame;
public static final Font font = new Font("宋体",Font.PLAIN,14);
// 控件设置
private static String title;
private static String version;
private String [] button;
// 构造方法 初始化变量
public FileTransfer()
{
title = "文件传输";
version = "1.0版本";
String[] b = {"选择/保存文件","开始发送/接受"};
button = b;
address = "192.168.0.32";
path = "";
}
// 传输文件 逻辑控制--------- 实现-----
public void Transfer()
{
// 定义视图
frame = new JFrame();
frame.setSize(WIDTH,HEIGHT);
frame.setLocation(300,200);
frame.setTitle(title+version);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 添加文件选择框
field = new JTextField(26);
field.setFont(new Font("宋体",Font.PLAIN,18));
// 添加文件选择按钮
JButton buttonSelect = new JButton(button[0]);
buttonSelect.setFont(font);
// 添加发送按钮
JButton buttonSend = new JButton(button[1]);
buttonSend.setFont(font);
// 添加控件 到面板
JPanel panel = new JPanel();
Border border = BorderFactory.createEtchedBorder();
panel.setBorder(border);
JLabel label = new JLabel("路径: ");
label.setFont(font);
panel.add(label);
panel.add(field);
panel.add(buttonSelect);
panel.add(buttonSend);
frame.getContentPane().add(panel,BorderLayout.CENTER);
// 添加监听器
ActionListener action = new buttonActionListener(field);
buttonSelect.addActionListener(action);
buttonSend.addActionListener(action);
// 创建 菜单条
menuBar menu = new menuBar();
frame.setJMenuBar(menu);
// 视图显示
frame.setVisible(true);
}
// 监听器实现
class buttonActionListener implements ActionListener
{
// 文本框 变量
JTextField field;
// 通过构造方法传递文本框 引用变量
public buttonActionListener(JTextField field)
{
this.field = field;
}
// 监听事件
public void actionPerformed(ActionEvent e)
{
String str = e.getActionCommand();
if(str.equals(button[0]))
path = openOrSaveFile();
if(path!=""&&str.equals(button[1]))
// 启动发送接收线程,调用sendOrAcceprFile method
new Thread()
{
public void run()
{
sendOrAcceprFile();
}
}.start();
}
}
// 按钮事件
// 打开/保存文件
public String openOrSaveFile()
{
// 根据发送/接收请求,分别使用打开/保存文件对话框,取得打开/保存路径
FileDialog fd ;
if(isSend)
fd =new FileDialog(frame,"打开文件对话框",FileDialog.LOAD);
else
fd=new FileDialog(frame,"打开文件对话框",FileDialog.SAVE);
fd.setVisible(true);
// 路径显示在 文本域中
String path = fd.getDirectory()+fd.getFile();
field.setText(path);
return path;
}
// 开始发送/接受 文件
public void sendOrAcceprFile()
{
// 取得文件
File file = new File(path);
// file.length();
// 如果是发送
if(isSend)
{
try
{
// 建立网络 输出流
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
// 建立文件读入流
DataInputStream fin = new DataInputStream(
new BufferedInputStream(
new FileInputStream(file)));
int str = fin.read();
while(str!=-1)
{
out.writeInt(str);
str = fin.read();
}
out.writeInt(-1);
// 关闭流
fin.close();
out.close();
}catch(IOException e){}
}
// 如果是接收
else
{
try
{
// 建立网络输入流
DataInputStream in =new DataInputStream(socket.getInputStream());
// 建立文件‘输出流
DataOutputStream fout = new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(file)));
int str = in.readInt();
while(str!=-1)
{
fout.writeByte(str);
str = in.readInt();
}
// 关闭流
in.close();
fout.close();
}catch(IOException e){}
}
// 关闭socket
try
{
socket.close();
}catch(IOException e){}
}
// 设置对方IP
public static void setHost()
{
address = JOptionPane.showInputDialog("设置对方IP:");
}
// 发送传输一个文件的请求
// 打开一个端口,等待客户端连接
public static void sendRequest()
{
ServerSocket s=null;
try
{
// 创建ServerSocket,等待客户连入
s = new ServerSocket(PORT);
field.setText("等待接收.......");
socket = s.accept();
field.setText("已同意接收.....");
}catch(IOException e){}
// 设置 发送/接收 标志
isSend = true;
// 关闭 socket
try{
s.close();
}catch(IOException e)
{
e.printStackTrace();
}
}
// 接受传送文件的请求
public static void acceptRequest()
{
try
{
// 建立Sodket,接收请求
socket = new Socket(address,PORT);
field.setText("清选择保存路径..");
}catch(IOException e){}
// 设置 发送/接收 标志
isSend = false;
}
}
// 菜单 类
class menuBar extends JMenuBar implements ActionListener
{
public menuBar()
{
// 设置菜单条 发送端和接受端统一
JMenu option= new JMenu ("设置");
option.setFont(FileTransfer.font);
// 开启ServerSocket,等待连接
JMenuItem newLink = new JMenuItem (menuName[0]);
option.add(newLink);
option.addSeparator();
// 接受方设置对方IP
JMenuItem setHost = new JMenuItem (menuName[1]);
option.add(setHost);
// 连接到对方
JMenuItem linkServer = new JMenuItem (menuName[2]);
option.add(linkServer);
// 菜单 增加监听器
newLink.addActionListener(this);
setHost.addActionListener(this);
linkServer.addActionListener(this);
// 加入菜单
this.add(option);
}
// 菜单名称
private static String[] menuName = {"发送传送文件请求","设置对方IP","接受请求"};
// 配置菜单监听器
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();
// 如果菜单menuName[0] 被按下
if(s.equals(menuName[0]))
new Thread()
{
public void run()
{
FileTransfer.sendRequest();
}
}.start();
// 如果菜单menuName[1] 被按下
if(s.equals(menuName[1]))
FileTransfer.setHost();
// 如果菜单menuName[2] 被按下
if(s.equals(menuName[2]))
FileTransfer.acceptRequest();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -