网路类装载器(服务端)(0825)

网路类装载器(服务端)

 

import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.net.*;
import javax.swing.filechooser.FileFilter;


class ClassFilter extends FileFilter
{
 public boolean accept( File pathName)
 {
  return pathName.getPath().endsWith(".class");
 } 
 public String getDescription()
 {
  return "java's Class file *.class";
 }
}

public class ServerClassGiver extends JFrame implements ActionListener
{
 private ServerSocket sock;
 private JTextField  PathText = new JTextField("Please Input the class file");
 private JButton BrowseBtn  = new JButton("Browse");
 private JButton AcceptBtn = new JButton("Accept");
 
 private File fileToSend = null;
 
 
 public ServerClassGiver( int port ) throws Exception
 {
  sock = new ServerSocket( port );  
  setDefaultCloseOperation( EXIT_ON_CLOSE );
  setBounds( 0,0,200,300 );
   
  AcceptBtn.setEnabled(false);
  PathText.setEditable(false);
  
  BrowseBtn.addActionListener(this);
  AcceptBtn.addActionListener(this);
    
  this.setLayout( new FlowLayout() );
  this.getContentPane().add( PathText );
  this.getContentPane().add( BrowseBtn );
  this.getContentPane().add( AcceptBtn );

  setVisible(true);  
 }
 

  
 public void actionPerformed( ActionEvent e )
 {
  if( e.getActionCommand().equals("Browse"))
  {
   JFileChooser open = new JFileChooser();
   open.setFileFilter( new ClassFilter() );
   
   if( open.showOpenDialog( this ) == JFileChooser.APPROVE_OPTION )
   {
    fileToSend = open.getSelectedFile();
    
    if( !fileToSend.exists() )
    {
     JOptionPane.showMessageDialog( this , "File does not exist!" ,
        "Warnning!" , JOptionPane.WARNING_MESSAGE, null);
      return;
    }
    
    PathText.setText( fileToSend.getPath());
    if( !AcceptBtn.isEnabled() )
    {
     AcceptBtn.setEnabled(true);
    }
    
   }
  }
  else if( e.getActionCommand().equals("Accept"))
  {
   try
   {
    Socket client = sock.accept();
    FileInputStream fin = new FileInputStream( fileToSend );
    BufferedInputStream bin = new BufferedInputStream( fin );
    BufferedOutputStream bout = new BufferedOutputStream(client.getOutputStream());
    
    int b;
    while( (b=bin.read())!=-1 )
    {
     bout.write(b);
    }
    bin.close();
    bout.close();
    
    JOptionPane.showMessageDialog( this , "Finished!" );
   }
   catch(Exception ex)
   {
    JOptionPane.showMessageDialog( this , ex.getMessage() );
   }
  }
 }

 public static void main(String [] args )throws Exception
 {
  new ServerClassGiver(80);
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值