📄 webmovefile.java
字号:
import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
public class WebMoveFile extends JFrame{
public static void main(String args[]){
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch(Exception e) {
e.printStackTrace();
}
new WebMoveFile();
}
public WebMoveFile(){
super("邮递员");
JButton1 = new JButton("选择文件");
JButton2 = new JButton("发送文件");
JPanel1 = new JPanel();
JPanel2 = new JPanel();
JLabel1 = new JLabel("");
JLabel2 = new JLabel("");
JFrame1 = new JFrame();
JFrame2 = new JFrame();
fileCarryer = new DefaultListModel();
clientCarryer = new DefaultListModel();
JList1 = new JList(clientCarryer);
JList2 = new JList(fileCarryer);
JScrollPane1 = new JScrollPane(JList1);
JScrollPane2 = new JScrollPane(JList2);
JTextArea1 = new JTextArea();
JScrollPane3 = new JScrollPane(JTextArea1);
JSplitPane1 = new JSplitPane();
JSplitPane2 = new JSplitPane();
JSplitPane2.setOrientation(JSplitPane.VERTICAL_SPLIT);
sendNameCarryer = new MyVector();
acceptNameCarryer = new MyVector();
realFileCarryer = new Vector();
fileLength = new Vector();
JPopupMenu1 = new JPopupMenu();
JPopupMenu2 = new JPopupMenu();
JMenuItem1 = new JMenuItem("删除");
JMenuItem2 = new JMenuItem("添加");
JMenuItem3 = new JMenuItem("删除");
JMenuItem4 = new JMenuItem("聊天");
JPopupMenu1.add(JMenuItem1);
JPopupMenu2.add(JMenuItem2);
JPopupMenu2.add(JMenuItem3);
JPopupMenu2.add(JMenuItem4);
JPanel1.add(JButton1);
JPanel1.add(JButton2);
JSplitPane1.setLeftComponent(JScrollPane1);
JSplitPane1.setRightComponent(JScrollPane2);
JSplitPane2.setTopComponent(JSplitPane1);
JSplitPane2.setRightComponent(JScrollPane3);
add(JPanel1,BorderLayout.NORTH);
add(JSplitPane2,BorderLayout.CENTER);
JButton1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
fileSelect();
}
});
JButton2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
if(JList1.getSelectedIndex() == -1){
JOptionPane.showMessageDialog(null,"请选择一名用户!");
}
else if(fileCarryer.getSize() == 0){
JOptionPane.showMessageDialog(null,"请选择要传送的文件!");
}
else{
new fileSender().start();
}
}
});
JMenuItem1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int[] indices = JList2.getSelectedIndices();
for(int i = 0;i < indices.length;i++){
fileCarryer.removeElementAt(indices[i]-i);
sendNameCarryer.removeElementAt(indices[i]-i);
realFileCarryer.removeElementAt(indices[i]-i);
fileLength.removeElementAt(indices[i]-i);
}
}
});
JMenuItem2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
addClient ac = new addClient();
Dimension acSize=ac.getSize();
Point p=getLocation();
ac.setLocation((frameSize.width-acSize.width)/2+p.x,(frameSize.height-acSize.height)/2+p.y);
ac.setVisible(true);
}
});
JMenuItem3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
int[] indices = JList1.getSelectedIndices();
for(int i = 0;i < indices.length;i++){
ipCarryer.removeElementAt(indices[i]-i);
clientCarryer.removeElementAt(indices[i]-i);
}
}
});
JMenuItem4.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event1){
try{
note("连接中,请等待...");
Socket s = new Socket();
s.connect(new InetSocketAddress((String)ipCarryer.elementAt(JList1.getSelectedIndex()),8190),DEFAULT_DELAY);
chat c = new chat(s);
Dimension cSize=c.getSize();
Point p=getLocation();
c.setLocation((frameSize.width-cSize.width)/2+p.x,(frameSize.height-cSize.height)/2+p.y);
c.setVisible(true);
}
catch(Exception event2){
JOptionPane.showMessageDialog(null,"对方貌似不在线,请稍后再试!");
note("连接失败!");
}
}
});
JList1.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event1) {
}
public void mouseReleased(MouseEvent event2) {
if(event2.isPopupTrigger()){
JPopupMenu2.show(event2.getComponent(),event2.getX(),event2.getY());
}
}
});
JList2.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent event1) {
}
public void mouseReleased(MouseEvent event2) {
if(event2.isPopupTrigger()){
JPopupMenu1.show(event2.getComponent(),event2.getX(),event2.getY());
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
clientSaver();
System.exit(0);
}
});
new MyServer().start();
new chatServer().start();
clientLoader();
setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
JSplitPane1.setDividerSize(DEFAULT_DIVIDERSIZE);
JSplitPane1.setDividerLocation(DEFAULT_DIVIDERLOCATION1);
JSplitPane2.setDividerSize(DEFAULT_DIVIDERSIZE);
JSplitPane2.setDividerLocation(DEFAULT_DIVIDERLOCATION2);
JList1.setBackground(new Color(DEFAULT_BLUE,DEFAULT_RED,DEFAULT_YELLOW));
JList1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JList2.setBackground(new Color(DEFAULT_BLUE,DEFAULT_RED,DEFAULT_YELLOW));
JTextArea1.setBackground(new Color(DEFAULT_BLUE,DEFAULT_RED,DEFAULT_YELLOW));
JTextArea1.setEditable(false);
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
frameSize = getSize();
setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
setVisible(true);
}
public void fileSelect(){
FileDialog fd = new FileDialog(JFrame1,"file load dialog",FileDialog.LOAD);
fd.show();
boolean repeat = false;
String dir = fd.getDirectory() + fd.getFile();
if(dir.equals("nullnull")){
}
else{
for(int i = 0;i < realFileCarryer.size();i++){
if(((File)realFileCarryer.elementAt(i)).getPath().equals(dir)){
repeat = true;
}
}
if(repeat == false){
File file = new File(dir);
realFileCarryer.addElement(file);
fileCarryer.addElement(fd.getFile() + " 长度:" + file.length());
sendNameCarryer.addElement(fd.getFile());
fileLength.addElement(file.length());
}else{
JOptionPane.showMessageDialog(null,"已存在此文件!");
}
}
}
public void clientLoader(){
ipCarryer = new Vector();
try{
FileInputStream fis = new FileInputStream("client.data");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String dir = "";
int i = 1;
while((dir = br.readLine()) != null){
if(i == 1){
ipCarryer.addElement(dir.trim());
}
if(i == 2){
clientCarryer.addElement(dir);
i = 0;
}
i++;
}
br.close();
}
catch(IOException event){
System.out.println(event.getMessage());
}
}
public void clientSaver(){
try{
FileOutputStream fos = new FileOutputStream("client.data");
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
String str = "";
for(int i = 0;i < clientCarryer.size();i++){
str = str + (String)ipCarryer.elementAt(i) + (char)13 + (char)10;
str = str + (String)clientCarryer.elementAt(i) + (char)13 + (char)10;
}
bw.write(str);
bw.close();
}
catch(IOException event){
System.out.println(event.getMessage());
}
}
public void note(String mention){
int h,mi,s;
Calendar cal = Calendar.getInstance();
h=cal.get(Calendar.HOUR_OF_DAY);
mi=cal.get(Calendar.MINUTE);
s=cal.get(Calendar.SECOND);
String str = "["+(h<10?"0"+h:h)+":"+(mi<10?"0"+mi:mi)+":"+(s<10?"0"+s:s)+"]";
str = str + mention;
str = str + (char)13 + (char)10;
str = str + JTextArea1.getText();
JTextArea1.setText(str);
}
public String checkClient(Socket s){
String ip = s.getInetAddress().getHostAddress();
String name = "";
for(int i = 0;i < ipCarryer.size();i++){
if(ip.equals((String)ipCarryer.elementAt(i))){
name = (String)clientCarryer.elementAt(i);
}
}
return name;
}
public int DEFAULT_WIDTH = 400;
public int DEFAULT_HEIGHT = 600;
public int DEFAULT_DIVIDERSIZE = 1;
public int DEFAULT_DIVIDERLOCATION1 = 120;
public int DEFAULT_DIVIDERLOCATION2 = 400;
public int DEFAULT_DATASIZE = 1024 * 10;
public int DEFAULT_BLUE =255;
public int DEFAULT_RED =255;
public int DEFAULT_YELLOW =255;
public int DEFAULT_DELAY = 1000;
JButton JButton1;
JButton JButton2;
JPanel JPanel1;
JPanel JPanel2;
JLabel JLabel1;
JLabel JLabel2;
JFrame JFrame1;
JFrame JFrame2;
JScrollPane JScrollPane1;
JScrollPane JScrollPane2;
JScrollPane JScrollPane3;
JList JList1;
JList JList2;
JTextArea JTextArea1;
JSplitPane JSplitPane1;
JSplitPane JSplitPane2;
JPopupMenu JPopupMenu1;
JPopupMenu JPopupMenu2;
JMenuItem JMenuItem1;
JMenuItem JMenuItem2;
JMenuItem JMenuItem3;
JMenuItem JMenuItem4;
String from;
String to;
DefaultListModel fileCarryer;
DefaultListModel clientCarryer;
Vector ipCarryer;
MyVector sendNameCarryer;
MyVector acceptNameCarryer;
Vector realFileCarryer;
Vector fileLength;
Dimension screenSize;
Dimension frameSize;
class fileSender extends Thread{
public void run(){
try{
int j = 0,m = 0;
byte[] b = new byte[5];
dataSender = new byte[DEFAULT_DATASIZE];
note("连接" + (String)clientCarryer.elementAt(JList1.getSelectedIndex()) + "中...");
s = new Socket();
s.connect(new InetSocketAddress((String)ipCarryer.elementAt(JList1.getSelectedIndex()),8189),DEFAULT_DELAY);
sendOS = s.getOutputStream();
sendIS = s.getInputStream();
oos = new ObjectOutputStream(sendOS);
m = sendIS.read(b);
String str = new String(b,0,m);
if(str.equals("false")){
note("对方拒绝接收文件");
s.close();
sendOS.close();
sendIS.close();
}
else{
oos.writeObject(sendNameCarryer);
sendIS.read(b);
String name = "";
double send = 0,length = 0;
for(int i = 0;i < realFileCarryer.size();i++){
length = Double.parseDouble((String.valueOf(fileLength.elementAt(i))));
fis = new FileInputStream((File)realFileCarryer.elementAt(i));
note("文件" + sendNameCarryer.elementAt(i) + "正在传送");
name = (String)fileCarryer.getElementAt(i);
fileCarryer.setElementAt(name + " " + (int)(send * 100 / length) + "/100",i);
while((j = fis.read(dataSender)) != -1){
sendOS.write(dataSender,0,j);
send += j;
fileCarryer.setElementAt(name + " " + (int)(send * 100 / length) + "/100",i);
}
Thread.sleep(1000);
sendOS.write("over".getBytes());
note("文件" + sendNameCarryer.elementAt(i) + "传送完毕");
send = 0;
fis.close();
}
fileCarryer.removeAllElements();
sendNameCarryer.removeAllElements();
realFileCarryer.removeAllElements();
fileLength.removeAllElements();
}
}
catch(Exception event){
event.printStackTrace();
JOptionPane.showMessageDialog(null,"对方貌似不在,请稍后再试。");
note("连接失败");
}
finally{
try{
fis.close();
s.close();
sendIS.close();
sendOS.close();
oos.close();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -