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

📄 chatframe.java

📁 一个用java编的可以跨系统传输文件的工具
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
public class ChatFrame  extends WindowAdapter implements ActionListener {

	Frame f;
	Button b1;
	TextArea ta;
	TextField tf;
	TextField tf1;
    MainServer t;
        
	public ChatFrame()throws IOException
	{
		f=new Frame("socket");
		f.setSize(400,300);
		ta=new TextArea();
		ta.setEnabled(false);
		f.add(ta);
	  
		Panel p=new Panel();
		f.add(p,"South");
		tf=new TextField(20);
		b1=new Button ("Send");
		p.add(tf);
		p.add(b1);
		b1.addActionListener(this);
	  
	  Panel p1=new Panel();
	  f.add(p1,"North");
	  tf1=new TextField(20);
	  p1.add(tf1);
	  f.setVisible(true);
	  f.addWindowListener(this);
   t=new MainServer(ta,tf1);
          try{
             t.serverStart();
             }catch(IOException e){}
	}
 class SendFile extends Thread{ 
     byte byteBuffer[]= new byte[1024]; 
     RandomAccessFile outFile; 
     ServerSocket serSocket; 
     OutputStream outSocket; 
     Socket tempSocket; 
      public SendFile(){ 
        try{ 
        	String s=tf.getText();
        	String f=s.substring(1);
             outFile = new RandomAccessFile(f,"r"); 
             serSocket = new ServerSocket(9090); 
             
        }catch(Exception e){} 
      } 
      
           public void run(){ 
                    try{ 
                       tempSocket=serSocket.accept(); 
                       outSocket=tempSocket.getOutputStream(); 
                        
                       int amount; 
                        
                            while((amount = outFile.read(byteBuffer)) != -1){ 
                                   outSocket.write(byteBuffer, 0, amount); 
                            } 
                        ta.append("Send File complete\n"); 
                        outFile.close(); 
                        tempSocket.close(); 
                        serSocket.close(); 
                       }catch(IOException e){} 
           
       
           } 
  } 
	public void actionPerformed(ActionEvent e)
	{
		
		String str=tf.getText();
		    client();
                
		
		if(str.charAt(0)=='-'){
		ta.append("sending file\n");
		   SendFile sf=new SendFile(); 
       sf.start(); 
       ta.append("wait for...\n"); 
	}
  else
		ta.append("you send:"+tf.getText()+"\n");
		tf.setText("");
		
	}
	public void windowClosing(WindowEvent e)
	{
		System.exit(0);
	}
	public void client(){
		BufferedReader br=null;
		PrintWriter pw=null;
		Socket s=null;
		try{
			s=new Socket (tf1.getText(),8080);
			
		InputStreamReader isr=new InputStreamReader(s.getInputStream());
		br=new BufferedReader(isr);
		pw=new PrintWriter(s.getOutputStream(),true);
		pw.println(tf.getText());
	}
	catch(Exception e){
		System.out.println(e);
	}
	finally
	{
		try
		{
			if(br!=null)
			br.close();
			if(pw!=null)
			pw.close();
			if(s!=null)
			s.close();
		}
		catch(Exception e){}
	}
}
		
	public static void main(String args[])throws IOException
	{
		try{
		new ChatFrame();
	        }catch(IOException e){}
    
	}


}

⌨️ 快捷键说明

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