📄 savefilegui.java
字号:
import java.io.*;
import java.net.*;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class SaveFileGUI extends WindowAdapter implements Runnable,ActionListener{
private File file;
private String fileName,friendName; //file from friend
private InetAddress ip;
private int sendFileKey;
private long friendID;
private JFrame frame;
private JLabel locationLabel;
private final static String CHOOSE="choose";
private final static String SAVE="save";
private final static String REFUSE="refuse";
public SaveFileGUI(String fileName,InetAddress ip,int sendFileKey,long friendID,String friendName){
this.fileName=fileName;
this.ip=ip;
this.sendFileKey=sendFileKey;
this.friendID=friendID;
this.friendName=friendName;
file=null;
}
public void run(){
frame=new JFrame("Friend send-file request");
frame.setContentPane(makeMainPane());
frame.addWindowListener(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae){
String type=ae.getActionCommand();
if(type.equals(CHOOSE)){
JFileChooser fc=new JFileChooser();
fc.setAcceptAllFileFilterUsed(false);
fc.setFileFilter(new DirectoryFilter());
fc.setMultiSelectionEnabled(false);
int returnVal=fc.showSaveDialog(frame);
if(returnVal==JFileChooser.APPROVE_OPTION){
file=fc.getSelectedFile();
locationLabel.setText("To save file in: "+file.getParent());
}
}
else if(type.equals(SAVE)){ //send ready to receive signal
if(file==null){
JOptionPane.showMessageDialog(frame,"You haven't chosen any directory to save file!",
"Where to save?",JOptionPane.WARNING_MESSAGE);
return;
}
FromFriendFile fff=new FromFriendFile(file,ip.toString().substring(1),sendFileKey);
int receiveFileKey=fff.getID();
//ClientThread.addFromFriendFile(fff);
ServerClientData data=new ServerClientData("readyToReceiveFile",sendFileKey,receiveFileKey);
try{
ByteArrayOutputStream baos=new ByteArrayOutputStream(6000);
ObjectOutputStream oos=new ObjectOutputStream(baos);
oos.flush();
oos.writeObject(data);
oos.flush();
byte[] byteArray=baos.toByteArray();
Socket socket=new Socket(ip,4445);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.write(byteArray,0,byteArray.length); //send ready to receive signal
//begin to receive
int t;
//DataInputStream dis=new DataInputStream(socket.getInputStream());
BufferedInputStream bis=new BufferedInputStream(socket.getInputStream());
//BufferedInputStream bis=new BufferedInputStream(dis);
FileOutputStream fos=new FileOutputStream(file);
BufferedOutputStream fileBos=new BufferedOutputStream(fos);
while((t=bis.read())!=-1){
fileBos.write(t);
}
baos.close();
oos.close();
dos.close();
//dis.close();
bis.close();
fos.close();
fileBos.close();
socket.close();
JOptionPane.showMessageDialog(frame,"File: "+file.getName()+" saving finished!",
"File-saving suceccessful!",JOptionPane.INFORMATION_MESSAGE);
}catch(ConnectException e){
JOptionPane.showMessageDialog(frame,"Friend has quit!",
"Connection Failed",JOptionPane.INFORMATION_MESSAGE);
}catch(UnknownHostException e){
JOptionPane.showMessageDialog(frame,"Cannot connect to friend:"+ip,
"Connection Failed",JOptionPane.INFORMATION_MESSAGE);
}catch(IOException e){
JOptionPane.showMessageDialog(frame,"IOException cause connection failed! "+e.getMessage(),
"Receive File Failed",JOptionPane.INFORMATION_MESSAGE);
}
frame.dispose();
}
else{ //refuse
int choice=JOptionPane.showConfirmDialog(frame,
"Do you really want to refuse this file?",
"Refuse Confirm",
JOptionPane.YES_NO_OPTION);
if(choice==1) //do not want to del
return;
frame.dispose();
}
}
public void windowClosing(WindowEvent we){
int choice=JOptionPane.showConfirmDialog(frame,
"Do you really want to refuse this file?",
"Refuse Confirm",
JOptionPane.YES_NO_OPTION);
if(choice==1) //do not want to del
return;
frame.dispose();
}
private Container makeMainPane(){
JPanel original=new JPanel();
original.setLayout(new BoxLayout(original,BoxLayout.Y_AXIS));
JPanel statusPane=new JPanel(new GridLayout(2,1));
JLabel label=new JLabel("File:"+fileName+" from ID:"+friendID+" name:"+friendName);
locationLabel=new JLabel(" ");
statusPane.add(label);
statusPane.add(locationLabel);
JPanel buttonPane=new JPanel(new GridLayout(3,3));
//buttonPane.setLayout(new BoxLayout(original,BoxLayout.))
JButton button=new JButton("Choose");
button.setToolTipText("Choose a directory to save file.");
button.setActionCommand(CHOOSE);
button.addActionListener(this);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
buttonPane.add(button);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
button=new JButton("Save");
button.setToolTipText("Save file.");
button.setActionCommand(SAVE);
button.addActionListener(this);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
buttonPane.add(button);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
button=new JButton("Refuse");
button.setToolTipText("Refuse this send-file request.");
button.setActionCommand(REFUSE);
button.addActionListener(this);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
buttonPane.add(button);
buttonPane.add(Box.createRigidArea(button.getPreferredSize()));
original.add(statusPane);
original.add(Box.createVerticalStrut(5));
original.add(buttonPane);
return original;
}
}
class DirectoryFilter extends FileFilter{
public boolean accept(File f) {
if(f.isDirectory())
return true;
else
return false;
}
public String getDescription() {
return "Directory Only";
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -