![](https://p-blog.csdn.net/images/p_blog_csdn_net/wangzeyou0415/b7c7f9d6e4ac4822b429dc603df6a081.png)
![](https://p-blog.csdn.net/images/p_blog_csdn_net/wangzeyou0415/b7a0efa9ccae49288f937049e086e2e6.png)
今晚写了网路经由类装载器
服务器:
ServerClassGiver.java文件:
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);
}
}
客户端:
ClassLoaderTest.java
import java.util.*;
import java.io.*;
import java.net.*;
import java.util.regex.*;
// Client
public class ClassLoaderTest extends ClassLoader
{
private String IPAddr = null;
private int port;
public ClassLoaderTest( String ip , int port )
{
ip = ip.trim();
Pattern ptn = Pattern.compile("//d+.//d+.//d+.//d+");
Matcher mtc = ptn.matcher(ip);
if( !mtc.matches() || port > 65535 || port <= 0)
{
throw new IllegalArgumentException("Argument Error!");
}
IPAddr = ip;
this.port = port;
}
protected Class findClass( String Name ) throws ClassNotFoundException
{
try
{
Socket sock = new Socket( IPAddr , port );
InputStream sin = sock.getInputStream();
BufferedInputStream bin = new BufferedInputStream( sin );
ByteArrayOutputStream baout = new ByteArrayOutputStream();
int b;
while( (b=bin.read()) != -1 )
{
baout.write(b);
}
byte[] bts = baout.toByteArray();
bin.close();
baout.close();
return defineClass( Name , bts , 0 , bts.length );
}
catch(Exception e )
{
return null;
}
}
public static void main(String [] args)throws Exception
{
ClassLoaderTest ldr = new ClassLoaderTest("162.105.81.165",80);
Class cls = ldr.loadClass("ClassFilter");
System.out.println(cls.getName());
System.out.println(cls.getClassLoader().getClass().getName());
}
}