⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 webmovefile.java

📁 java编写的局域网文件传输工具
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				catch(Exception event){
				}    
			}
		}
		byte[] dataSender;
		
		Socket s;
		
		FileInputStream fis;
		OutputStream sendOS;
	    InputStream sendIS;
        ObjectOutputStream oos;
	}
	class MyServer extends Thread{
		public void run(){
			try{
				ServerSocket MyServer = new ServerSocket(8189);
				while(true){
					Socket s = MyServer.accept();
					int yon;
					yon = JOptionPane.showConfirmDialog(null,s.getInetAddress().getHostAddress() + "向你发送文件,接收?","文件接收",JOptionPane.YES_NO_OPTION);
  		            if(yon==JOptionPane.YES_OPTION){
  			            new fileAccepter(s).start();
  		            }else{
  		            	OutputStream os = s.getOutputStream();
		        	    os.write("false".getBytes());
		        	    os.close();
		        	    s.close();
  		            }
					
				}
		    }
		    catch(Exception event){
		    	System.out.println(event.getMessage());
		    }
		}
		class fileAccepter extends Thread{
			public fileAccepter(Socket s){
    		    this.s = s;
    	    }
    	    public void run(){
    	    	dataAccepter = new byte[DEFAULT_DATASIZE];
    	    	FileDialog fd=new FileDialog(JFrame2,"file save dialog",FileDialog.SAVE);
    	    	fd.setFile("unname");
    	    	fd.show();
		        String dir = fd.getDirectory();
		        int i = 0,j = 0;System.out.println(dir);
		        if(dir==null){
		        	try{
		        		OutputStream os = s.getOutputStream();
		        	    os.write("false".getBytes());
		        	    os.close();
		        	}
		        	catch(Exception event){
		        		System.out.println(event.getMessage());
		        	}
		        }
		        else{
		        	try{
		        	    acceptOS = s.getOutputStream();
		        	    acceptIS = s.getInputStream();
    	    		    ois = new ObjectInputStream(acceptIS);
    	    		    acceptOS.write("true".getBytes());
    	    		    acceptNameCarryer = (MyVector)ois.readObject();
    	    		    acceptOS.write("ok".getBytes());
    	    		    for(i = 0;i < acceptNameCarryer.size();i++){
    	    		    	fos = new FileOutputStream(dir + acceptNameCarryer.elementAt(i));
    	    		    	note("文件" + acceptNameCarryer.elementAt(i) + "正在接收");
    	    		    	while(true){
    	    		            j = acceptIS.read(dataAccepter);
    	    		            String str = new String(dataAccepter,0,j);
    	    		            if(str.equals("over")){
    	    		            	note("文件" + acceptNameCarryer.elementAt(i) + "接收完毕");
    	    		        	    break;
    	    		            }
    	    		            else{
    	    		        	    fos.write(dataAccepter,0,j);
    	    		            }
    	    		        }
    	    		        fos.close();
    	    		    }
    	    		    
    	    	    }
    	    	    catch(Exception event){
    	    		    System.out.println(event.getMessage());
    	    	    }
    	    	    finally{
    	    	    	try{
    	    	    		s.close();
    	    		        acceptOS.close();
    	    		        acceptIS.close();
    	    		        ois.close();
    	    	    	}
    	    	    	catch(Exception event){
    	    	    	}
    	    	    }
		        }
    	    }
    	    byte[] dataAccepter;
    	    
    	    Socket s;
	
	        FileOutputStream fos;
	        OutputStream acceptOS;
            InputStream acceptIS;
            ObjectInputStream ois;
        }
    }
    class chatServer extends Thread{
    	ServerSocket ss;
  
    	public chatServer(){
    		try{
    			ss = new ServerSocket(8190);
    		}
    		catch(Exception event){
    		}
    	
    	}
    	public void run(){
    		try{
    			while(true){
    				Socket s = ss.accept();
    				int yon;
					yon = JOptionPane.showConfirmDialog(null,checkClient(s) + "希望与你聊天,是否接受?","聊天请求",JOptionPane.YES_NO_OPTION);
  		            if(yon==JOptionPane.YES_OPTION){
  			            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);
  		            }else{
  		            	PrintStream ps = new PrintStream(s.getOutputStream());
  		            	ps.write("false".getBytes());
  		            	ps.flush();
  		            	ps.close();
		        	    s.close();
  		            }
    				
    				
		    		
    			}
    		}
    		catch(Exception event){
    		}
    	}
    }
    class addClient extends JFrame{
	    public addClient(){
		    JTextField1 = new JTextField(10);
		    JTextField2 = new JTextField(10);
		    JLabel1 = new JLabel("用户名");
		    JLabel2 = new JLabel("ip");
		    JButton1 = new JButton("确定");
		    JButton2 = new JButton("取消");
		    JPanel1 = new JPanel();
		    JPanel2 = new JPanel();
		    JPanel1.setLayout(new FlowLayout());
		    JPanel2.setLayout(new FlowLayout());
		    JPanel1.add(JLabel1);
		    JPanel1.add(JTextField1);
		    JPanel1.add(JLabel2);
		    JPanel1.add(JTextField2);
		    JPanel2.add(JButton1);
		    JPanel2.add(JButton2);
		    setLayout(new GridLayout(0,1));
		    add(JPanel1);
		    add(JPanel2);
		    JButton1.addActionListener(new ActionListener(){
		        public void actionPerformed(ActionEvent event){
		        	boolean repeat = false;
		    	    if(JTextField1.getText() != null && JTextField2.getText() != null){
		    	    	for(int i = 0;i < ipCarryer.size();i++){
		    	    		if(((String)ipCarryer.elementAt(i)).equals(JTextField2.getText().trim())){
		    	    			repeat = true;
		    	    		}
		    	    	}
		    	    	if(repeat == false){
		    	    		clientCarryer.addElement(JTextField1.getText());
		    		        ipCarryer.addElement(JTextField2.getText());
		    	    	}
		    	    	else{
		    	    		JOptionPane.showMessageDialog(null,"已存在此ip!");
		    	    	}
		    		    
		    	    }
		    	    else{
		    		    JOptionPane.showMessageDialog(null,"请填写完整!");
		    	    }
		        }
		    });
		    JButton2.addActionListener(new ActionListener(){
		        public void actionPerformed(ActionEvent event){
		        	setVisible(false);
		        }
	    	});
		    pack();
	    }
	    JTextField JTextField1;
	    JTextField JTextField2;
	    JButton JButton1;
	    JButton JButton2;
	    JPanel JPanel1;
	    JPanel JPanel2;
	    JLabel JLabel1;
	    JLabel JLabel2;
    }
    class chat extends JFrame{
    	public chat(Socket s){
    		super("与" + checkClient(s) + "聊天中");
    		name = checkClient(s);
    		this.s = s;
    		b = new byte[1024 * 10];
    		run = true;
    		
    		JTextArea1 = new JTextArea();
    		JTextArea2 = new JTextArea();
    		JSplitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
    		JButton1 = new JButton("发送");
    		JPanel1 = new JPanel();
    		JScrollPane1 = new JScrollPane(JTextArea1);
    		JScrollPane2 = new JScrollPane(JTextArea2);
    		JSplitPane1.setTopComponent(JScrollPane1);
    		JSplitPane1.setRightComponent(JScrollPane2);
    		JPanel1.add(JButton1);
    		add(JSplitPane1,BorderLayout.CENTER);
    		add(JPanel1,BorderLayout.SOUTH);
    		JSplitPane1.setDividerSize(1);
    		JSplitPane1.setDividerLocation(250);
    		JButton1.addActionListener(new ActionListener(){
    		    public void actionPerformed(ActionEvent event1){
    		    	String str = JTextArea2.getText();
    		    	try{
    		    		ps.write(str.getBytes());          //用打印流发送聊天信息
				        ps.flush();
				        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 time = (h<10?"0"+h:h)+":"+(mi<10?"0"+mi:mi)+":"+(s<10?"0"+s:s);
				        str = JTextArea1.getText() + "我自己" + "  " + time + (char)13 + (char)10 + str + (char)13 + (char)10;
				        JTextArea1.setText(str);
				        JTextArea2.setText("");
    		    	}
    		    	catch(Exception event2){
    		    	}
    		    }
    		    
    		});
    		addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent event1){
                	try{
                		ps.write("over".getBytes());          //用打印流发送聊天信息
				        ps.flush();
                	}
                	catch(Exception event2){
                	}
                	
                    chatOver();
                    
                }
            });
            try{
            	is = s.getInputStream();
                os = s.getOutputStream();
            }
            catch(Exception event){
            }
            ps = new PrintStream(os);
            dis = new DataInputStream(is) ;  
    		
        	new waiter().start();
            JTextArea1.setEditable(false);
    		setSize(300,400);
    	}
    	public void chatOver(){
    		try{
    			s.close();
    			ps.close();
    			dis.close();
    			run = false;
    			setVisible(false);
    		}
    		catch(Exception event){
    		}
    	}
    	JTextArea JTextArea1;
    	JTextArea JTextArea2;
    	JButton JButton1;
    	JPanel JPanel1;
    	JScrollPane JScrollPane1;
    	JScrollPane JScrollPane2;
    	JSplitPane JSplitPane1;
    	
    	String name;
    	String ip;
        Socket s; 
        
        InputStream is;
        OutputStream os;
        PrintStream ps;
        DataInputStream dis;  
        byte[] b;
        
        boolean run;
        class waiter extends Thread{
        	public void run(){
        		int i = 0;
        		while(true && run){
        			try{
        				i = dis.read(b);
        				String str = new String(b,0,i);
        				if(str.equals("over")){
        					JOptionPane.showMessageDialog(null,"对方已关闭聊天窗口!");
        					chatOver();
        				}
        				else if(str.equals("false")){
        					JOptionPane.showMessageDialog(null,"对方拒绝了你的聊天请求!");
        					chatOver();
        				}
        				else{
        					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 time = (h<10?"0"+h:h)+":"+(mi<10?"0"+mi:mi)+":"+(s<10?"0"+s:s);
        					str = JTextArea1.getText() + name + "  " + time + (char)13 + (char)10 + str + (char)13 + (char)10;
        				    JTextArea1.setText(str);
        				}
        				
        			}
        			catch(Exception event){
        			}
        		}
        	}
        }
    }
    class YON extends JFrame{
    	public YON(String str){
    		JPanel1 = new JPanel();
    		JPanel2 = new JPanel();
    		JButton1 = new JButton("确定");
    		JButton2 = new JButton("取消");
    		JLabel1 = new JLabel(str);
    		setLayout(new GridLayout(0,1));
    		JPanel1.add(JLabel1);
    		JPanel2.add(JButton1);
    		JPanel2.add(JButton2);
    		add(JPanel1);
    		add(JPanel2);
    		JButton1.addActionListener(new ActionListener(){
    		    public void actionPerformed(ActionEvent event){
    		    	YON = true;
    		    	setVisible(false);
    		    }
    		});
    		JButton2.addActionListener(new ActionListener(){
    		    public void actionPerformed(ActionEvent event){
    		    	YON = false;
    		    	setVisible(false);
    		    }
    		});
    		pack();
    	}
    	JPanel JPanel1;
    	JPanel JPanel2;
    	JButton JButton1;
    	JButton JButton2;
    	JLabel JLabel1;
    	boolean YON;
    }
}
class MyVector extends Vector implements Serializable{
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -