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

📄 tcpip.java

📁 基于TCPIP的局域网传输文件的小软件
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.lang.String.*;

public class TCPIP extends Frame implements Runnable
{ Label label1,label2,label3,label4,label5;
 TextField textField1,textField2,textField3,textField4,textField5;
 Button button1,button2;
 public TCPIP()
 {
  this.setLayout(null);
  this.setBounds(0,0,455,230);
  this.setTitle("TCP/IP Trans");
  this.addWindowListener(new WindowAdapter()
   {
    public void windowClosing(WindowEvent e)
    {
     System.exit(0);
    }
   });
   
   
   label2 = new Label("对方收之Port:");
   label2.setBounds(2,60,80,23);
   this.add(label2);   
   
   label3 =  new Label("要传送的文件:");
   label3.setBounds(2,100,80,23);
   this.add(label3);
   
   label4 = new Label("本机收之Port:");
   label4.setBounds(2,140,80,23);
   this.add(label4);
   
   label5 = new Label("收到的文件存为:");
   label5.setBounds(2,180,95,23);
   this.add(label5);
   
   
   
   label1 = new Label("对方IP:");
   label1.setBounds(2,232,60,15);
   this.add(label1);
   
   textField1 = new TextField("192.168.1.20");
   textField1.setBounds(90,232,100,15);
   this.add(textField1);
   
   textField2 = new TextField("4001");  
   textField2.setBounds(90,60,50,23);
   this.add(textField2);
   
   textField3 = new TextField("");
   textField3.setBounds(90,100,100,23);
   this.add(textField3);
   
   textField4 = new TextField("5001");
   textField4.setBounds(90,140,50,23);
   this.add(textField4);
   
   textField5 = new TextField("test.txt");
   textField5.setBounds(105,180,100,23);
   this.add(textField5);
   
   button1 = new Button("发送");
   button1.setBounds(210,100,50,23);
   button1.addActionListener( new myActionListener() );
   this.add(button1);

   
   this.setVisible(true);
 }
 
 public static void main(String para[])
 {
  new Thread(new TCPIP() ).start();
 }
 
 public void run()
 {
  byte[] theData = new byte[1];
  try
  {
   ServerSocket serverTcp = new ServerSocket(Integer.parseInt(textField4.getText().trim()));
   while(true)
   {
    Socket connSocket = serverTcp.accept();
      BufferedInputStream serverInput = new BufferedInputStream(connSocket.getInputStream(),8129);
    
    File theTar = new File( textField5.getText().trim() );
      File tarParent = theTar.getParentFile();
      if( (tarParent!=null)&& !tarParent.exists() )
        tarParent.mkdirs();
    
    BufferedOutputStream FileObj = new BufferedOutputStream( new FileOutputStream(theTar), 8192 );
    
    while( serverInput.read(theData) != -1)
    {
     FileObj.write(theData);
    }
    
    FileObj.flush();
    FileObj.close();
    serverInput.close();
    connSocket.close();
    JOptionPane.showMessageDialog(null,"接受了一个文件!");
    Thread.sleep(200);  
   }
  }
  
  catch(InterruptedException ecp){System.out.println("Ecpl");}
  catch(FileNotFoundException ecp){System.out.println("Ecp2");}
  catch(IOException ecp){System.out.println("Ecp3");}
 }


class myActionListener implements ActionListener
{
 public void actionPerformed(ActionEvent e)
 {
  byte[] tmpData = new byte[1];
  File sourceFile = new File( textField3.getText().trim() );
  if(!sourceFile.exists())
  {
   JOptionPane.showMessageDialog(null,"要传送的文件不存在!");
   return;
  }
  try
  {
   Socket tcpSocket = new Socket( textField1.getText().trim(), Integer.parseInt(textField2.getText().trim()) ); 
   BufferedInputStream fileObj = new BufferedInputStream( new FileInputStream(textField3.getText().trim()),8192 );
   BufferedOutputStream opStream = new BufferedOutputStream( tcpSocket.getOutputStream(),8192 );
   
   while( fileObj.read(tmpData)!=-1 )
   {
    opStream.write( tmpData );
   }
  
  opStream.flush();
  opStream.close();
  fileObj.close();
  tcpSocket.close();
  JOptionPane.showMessageDialog(null,"一个文件传输完毕!");
  }
  catch(FileNotFoundException ecp){System.out.println("Ecp4");}
  catch(UnknownHostException ecp){System.out.println("Ecp5");}
  catch(IOException ecp){System.out.println("Ecp6");}
  }
 }
}

⌨️ 快捷键说明

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