📄 filereceiver.java
字号:
//接收文件, 选择保存路径, 判断文件的大小,各种信息的显示
package dssclient;
import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import signature.SignandVer;
import encryption.FileDES;
import zipfile.Unzip;
public class FileReceiver extends JFrame implements ActionListener, Runnable{
String cryptfile = "D:\\soft\\YSF\\cryptfile.txt" ;
String targetfile = "D:\\soft\\YSF\\targetfile.txt";
String hashfileName = "D:\\soft\\YSF\\hash.txt" ;
String signfileName = "D:\\soft\\YSF\\sign.txt" ;
String CertName = "D:\\soft\\YSF\\UserCert.pem" ;
JFileChooser openFiles = new JFileChooser();
SignandVer fileSig = new SignandVer();
File decompressionFiles = null;
Container content = getContentPane();
JButton decompressionButton = new JButton("文件解压");
JButton decryptionButton = new JButton("DES解密");
JButton authenticationButton = new JButton("签名验证");
JButton stopButton = new JButton("停止传输");
ServerSocket listeningSocket = null;
int listenFlag = 0;
JFileChooser saveFiles = null;
File toSave = null;
static final int bufferSize = 8192;
static final long MAGIC_NUMBER = 3576541657654657413L; //用来表示常量
Thread t;
public FileReceiver(){
super("接收文件");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
stopButton.addActionListener(this);
decompressionButton.addActionListener(this);
decryptionButton.addActionListener(this);
authenticationButton.addActionListener(this);
JPanel panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
panel.add(decompressionButton);
panel.add(decryptionButton);
panel.add(authenticationButton);
panel.add(stopButton);
setContentPane(panel);
setSize(200,100);
}
public void actionPerformed(ActionEvent e) {
Component whichButton = (Component)e.getSource();
if(whichButton==stopButton){
try {
listeningSocket.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
else if(whichButton==decompressionButton){
if(openFiles.APPROVE_OPTION==openFiles.showOpenDialog((Component)content))
{
decompressionFiles = openFiles.getSelectedFile();
Unzip unzip = new Unzip();
unzip.UnZip(decompressionFiles.getPath());
}
}
else if(whichButton==decryptionButton){
FileDES fileDes = new FileDES();
String passWord="ZYWX1234";
fileDes.FileDes(cryptfile, targetfile, passWord, false);
}
else if(whichButton==authenticationButton){
fileSig.veriSig(hashfileName, signfileName, CertName);
}
}
public void start(){
t=new Thread(this);
t.start();
}
public void run() {
try {
listeningSocket = new ServerSocket(4000);
listenFlag = 1;
while(listenFlag == 1)
{
Socket serverSocket = listeningSocket.accept();
new Thread(new filereceive(serverSocket,this)).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
String save(String fileName)
{//0
saveFiles = new JFileChooser(toSave==null?null:toSave.getPath());
saveFiles.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = saveFiles.showDialog((Component)content,"Save");
if(result==JFileChooser.APPROVE_OPTION)
{//1
toSave = new File(saveFiles.getSelectedFile(),fileName);
if(toSave.exists()&&toSave.isFile())
{//2
Object[] options = {"覆盖","恢复","改变目录"};
int answer = JOptionPane.showOptionDialog(
(Component)content,toSave.getName()+" 存在.",
"文件存在",JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE,null,options,options[2]);
switch(answer)
{//3
case JOptionPane.YES_OPTION:
return saveFiles.getSelectedFile().getPath(); //real return;
case JOptionPane.NO_OPTION:
return "**恢复**"+saveFiles.getSelectedFile().getPath();
default:
return save(fileName);
}//3
}//2
else
return saveFiles.getSelectedFile().getPath(); //real return;
}//1
else
return "Canceled";
}//0
static String size(float size){ //文件大小
if(size==0)
return size+"Bytes";
if(size==1)
return size+"Byte";
if(size<=1024)
return size+"Bytes";
if(size<=1048576)
return Math.round(size/1024)+"kB";
return (float)Math.round(size*10/1048576)/10+"MB";
}
void fileReceived(String fileName)
{
JOptionPane.showMessageDialog((Component)content,fileName+" 收到","",JOptionPane.INFORMATION_MESSAGE);
//this.dispose();
}
void otherSideCanceled()
{
JOptionPane.showMessageDialog((Component)content,"对方取消","",JOptionPane.INFORMATION_MESSAGE);
}
void handleException(String whatHappened)
{
JOptionPane.showMessageDialog((Component)content,whatHappened,"",JOptionPane.INFORMATION_MESSAGE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -