📄 filesender.java
字号:
//发送文件, 选择发送文件路径, 判断文件的大小,各种信息的显示
package dssclient;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.net.ServerSocket;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.WindowConstants;
import signature.Fhash;
import signature.SignandVer;
import zipfile.ZipFiles;
import encryption.FileDES;
public class FileSender extends JFrame implements ActionListener, Runnable
{
static FileSender theApp = new FileSender("文件传送");
Container content = getContentPane();
JPanel panel = new JPanel(new GridLayout(0,1));
JTextField ipTextField = new JTextField("localhost",20);
JTextField portTextField = new JTextField("4000",5);
JTextField listeningPortTextField = new JTextField("4000",8);
JButton fileButton = new JButton("选择传输文件路径");
JButton sendButton = new JButton("发送");
JButton pauseButton = new JButton("暂停");
JButton cancelButton = new JButton("退出");
JButton desButton = new JButton("加密");
JButton sgButton = new JButton("签名");
JButton savebutton = new JButton("保存");
JButton hashbutton = new JButton("计算摘要");
JLabel toSendNameLabel = new JLabel("");
JLabel listenStatusLabel = new JLabel(" ");
ServerSocket listeningSocket = null;
int listenFlag = 0;
static final long MAGIC_NUMBER = 3576541657654657413L;
boolean idle = true;
monitor goOn = new monitor(true);
int recCount = 0;
JFileChooser openFiles = new JFileChooser();
JFileChooser saveFiles = null;
File toSend = null;
File sgkey = null;
File sourceFileName = null;
final int bufferSize = 8192;
Thread sendThread = null;
String ip;
FileDES fileDes = new FileDES();
SignandVer fileSig = new SignandVer();
Fhash fileHash = new Fhash();
String hashfileName = "D:\\soft\\DSF\\hash.txt" ;
String signfileName = "D:\\soft\\DSF\\sign.txt" ;
String prakry = "D:\\soft\\DSF\\prakey.der" ;
String CertName = "D:\\soft\\DSF\\UserCert.pem" ;
String cryptfile = "D:\\soft\\DSF\\cryptfile.txt" ;
public FileSender(String his_ip)
{
try
{
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
}
catch(Exception e)
{
handleException(e.toString());
}
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setTitle(his_ip);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
content.setLayout(new FlowLayout());
addWindowListener(new WindowHandler());
ipTextField.selectAll();
ipTextField.setText(ip);
ipTextField.setToolTipText("这是接收者的地址.");
portTextField.setToolTipText("这是接收者的端口.");
listeningPortTextField.setToolTipText("监听端口.");
Dimension size = new Dimension(90,18);
sendButton.setEnabled(false);
sendButton.addActionListener(this);
sendButton.setPreferredSize(size);
sendButton.setToolTipText("发送选中的文件!");
pauseButton.setEnabled(false);
pauseButton.addActionListener(this);
pauseButton.setPreferredSize(size);
pauseButton.setToolTipText("暂停/重新开始传送过程.");
JPanel addressPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
addressPanel.add(new JLabel("地址: "));
addressPanel.add(ipTextField);
addressPanel.add(new JLabel("端口: "));
addressPanel.add(portTextField);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(sendButton);
JPanel namePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel sizePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel listenPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
listenPanel.add(listeningPortTextField);
listenPanel.add(listenStatusLabel);
panel.add(addressPanel);
panel.add(namePanel);
desButton.setText("DES加密");
namePanel.add(desButton);
desButton.addActionListener(this);
hashbutton.setText("计算摘要");
namePanel.add(hashbutton);
hashbutton.addActionListener(this);
sgButton.setText("数字签名");
namePanel.add(sgButton);
sgButton.addActionListener(this);
savebutton.setText("文件压缩");
namePanel.add(savebutton);
savebutton.addActionListener(this);
panel.add(sizePanel);
fileButton.addActionListener(this);
sizePanel.add(fileButton);
fileButton.setPreferredSize(size);
fileButton.setToolTipText("选择一个要发送的文件.");
sizePanel.add(toSendNameLabel);
panel.add(buttonsPanel);
cancelButton.setEnabled(false);
cancelButton.addActionListener(this);
cancelButton.setPreferredSize(size);
cancelButton.setToolTipText("退出传送.可恢复");
buttonsPanel.add(cancelButton);
content.add(panel);
pack();
new Thread(this).start();
}
public void actionPerformed(ActionEvent evt)
{
Component whichButton = (Component)evt.getSource();
if(whichButton==fileButton)
{
if(openFiles.APPROVE_OPTION==openFiles.showOpenDialog((Component)content))
{
toSend = openFiles.getSelectedFile();
toSendNameLabel.setText("file: "+toSend.getPath());
sendButton.setEnabled(true);
FileSender.theApp.pack();
}
}
else if(whichButton==desButton)
{
if(openFiles.APPROVE_OPTION==openFiles.showOpenDialog((Component)content))
{
String passWord="ZYWX1234"; //工作密匙
sourceFileName = openFiles.getSelectedFile();
fileDes.FileDes(sourceFileName.getPath(), cryptfile, passWord, true);
toSendNameLabel.setText("file: "+sourceFileName.getPath());
sendButton.setEnabled(true);
FileSender.theApp.pack();
}
}
else if(whichButton==hashbutton)
{
fileHash.fhash(sourceFileName.getPath(), hashfileName);
}
else if(whichButton==sgButton)
{
if(openFiles.APPROVE_OPTION==openFiles.showOpenDialog((Component)content))
{
sgkey = openFiles.getSelectedFile();
sgkey.getPath();
fileSig.TransFormat(sgkey.getPath(), prakry);
FileSender.theApp.pack();
}
}
else if(whichButton==savebutton)
{
fileSig.sig(hashfileName, prakry, signfileName);
ZipFiles zipfiles = new ZipFiles();
zipfiles.Zipfiles();
}
if(whichButton==sendButton)
{
try
{
sendThread = new Thread(new filesend(toSend,ip));
ipTextField.setText(ip);
sendThread.start();
sendButton.setEnabled(false);
fileButton.setEnabled(false);
}
catch(Exception e)
{
FileSender.theApp.handleException(e.toString());
}
}
}
public void run()
{
Toolkit theKit = theApp.getToolkit();
Dimension scrSize = theKit.getScreenSize();
theApp.setLocation(scrSize.width/4,scrSize.height/4);
theApp.setVisible(true);
while(idle)
{
try
{
Thread.sleep(10000);
FileSender.theApp.pack();
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
void handleException(String whatHappened)
{
JOptionPane.showMessageDialog((Component)content,whatHappened,"",JOptionPane.INFORMATION_MESSAGE);
}
void otherSideCanceled()
{
JOptionPane.showMessageDialog((Component)content,"对方退出了.","",JOptionPane.INFORMATION_MESSAGE);
}
void fileSent()
{
JOptionPane.showMessageDialog((Component)content,"文件传送成功!.","",JOptionPane.INFORMATION_MESSAGE);
FileSender.theApp.dispose();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -