因为我最近比较忙为了省时间,所以我用java实现的,用delphi实现比较费时间。(希望能够抛砖引玉)
//////////////////////////////////////
// 服务器端 //
//////////////////////////////////////
package networktransfer;
import javax.swing.*;
import java.rmi.*;
import java.rmi.registry.*;
import java.rmi.server.*;
import java.io.*;
import java.nio.*;
import java.util.*;
import java.io.FileInputStream;
import java.nio.channels.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.Border;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class ServerMain extends JFrame implements TransferInter,Serializable {
String dir=null;
Properties profile=null;
Properties privilege=null;
FileInputStream proFin=null;
FileInputStream priFin=null;
JPanel jPanel1 = new JPanel();
BorderLayout borderLayout1 = new BorderLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JTextArea Jtxa = new JTextArea();
JPanel jPanel2 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
FlowLayout flowLayout1 = new FlowLayout();
JButton jButton3 = new JButton();
Border border1 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
Color.white, new Color(148, 145, 140));
JMenuBar jMenuBar1 = new JMenuBar();
JMenu jMenu1 = new JMenu();
JMenuItem jMenuItem1 = new JMenuItem();
JMenuItem jMenuItem2 = new JMenuItem();
JMenuItem jMenuItem3 = new JMenuItem();
public ServerMain() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void jbInit() throws Exception{
this.setSize(400,300);
jPanel1.setLayout(borderLayout1);
jButton1.setFont(new java.awt.Font("Dialog", 0, 12));
jButton1.setText("退出"
;
jButton2.setFont(new java.awt.Font("Dialog", 0, 12));
jButton2.setText("清空记录"
;
jPanel2.setLayout(flowLayout1);
flowLayout1.setAlignment(FlowLayout.RIGHT);
Jtxa.setBackground(Color.white);
Jtxa.setText(""
;
Jtxa.setLineWrap(true);
jButton3.setFont(new java.awt.Font("Dialog", 0, 12));
jButton3.setText("保存记录"
;
jPanel1.setBorder(null);
jScrollPane1.setBorder(border1);
this.setJMenuBar(jMenuBar1);
jMenu1.setText("File"
;
jMenuItem1.setActionCommand("Config Directory"
;
jMenuItem1.setText("Config Directory"
;
jMenuItem1.addActionListener(new ServerMain_jMenuItem1_actionAdapter(this));
jMenuItem2.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jMenuItem2.setText("Config Logon"
;
jMenuItem2.addActionListener(new ServerMain_jMenuItem2_actionAdapter(this));
jMenuItem3.setFont(new java.awt.Font("Dialog", Font.PLAIN, 12));
jMenuItem3.setText("Exit"
;
jMenuItem3.addActionListener(new ServerMain_jMenuItem3_actionAdapter(this));
this.getContentPane().add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, BorderLayout.CENTER);
jPanel1.add(jPanel2, BorderLayout.SOUTH);
jPanel2.add(jButton3, null);
jPanel2.add(jButton2, null);
jPanel2.add(jButton1, null);
jScrollPane1.getViewport().add(Jtxa, null);
jMenuBar1.add(jMenu1);
jMenu1.add(jMenuItem1);
jMenu1.addSeparator();
jMenu1.add(jMenuItem2);
jMenu1.add(jMenuItem3);
profile=new Properties();
File itfile=new File("Server.info"
;
if (!itfile.exists() ||itfile.isDirectory()||!itfile.canRead()) {
dir=".";
}else{
proFin=new FileInputStream(itfile);
profile.load(proFin);
dir=profile.getProperty("dir"
;
}
}
public String[] getFileName() {
File directory=new File(dir);
if (directory.exists()&&directory.canRead() && directory.isDirectory()){
return directory.list();
}
return null;
}
public byte[] transferFile(String fileName,String name,String pass) {
//*************权限判定****************
privilege=new Properties();
try {
File priFile=new File("privliege.info"
;
if (!priFile.exists() || !priFile.canRead()||priFile.isDirectory()){
Jtxa.append("错误:权限文件可能不存在或者文件不可读,请重新设置权限文件!/n"
;
return null;
}
priFin = new FileInputStream(priFile);
privilege.load(priFin);
if (privilege.getProperty(name)!=pass)
{
Jtxa.append("错误:密码不正确,或者无此用户名,请求驳回!/n"
;
return null;
}
//无此用户或密码无效返回空值
}
catch (IOException ex2) {ex2.printStackTrace();
}
//*************权限判定结束****************
File file=new File(fileName);
if (file.exists()&&file.canRead() && !file.isDirectory()){
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
FileChannel rochannel=fin.getChannel();
ByteBuffer robuf=rochannel.map(FileChannel.MapMode.READ_ONLY,0,rochannel.size());
byte buff[]=new byte[robuf.capacity()];
robuf.get(buff,0,buff.length);
Jtxa.append("信息:用户 "+name+"开始读取文件"+fileName+". 时间: "+new Date().toString()+" . /n"
;
return buff;
}
catch (IOException ex) {
Jtxa.append("错误:用户 "+name+"读取文件"+fileName+"失败. 时间: "+new Date().toString()+" . /n"
;
ex.printStackTrace();
}
}
return null;
}
public static void main(String[] args){
ServerMain frame=new ServerMain();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
try {
TransferInter stub=(TransferInter)java.rmi.server.UnicastRemoteObject.exportObject(frame,0);
java.rmi.registry.LocateRegistry.createRegistry(1099);
java.rmi.registry.LocateRegistry.getRegistry().rebind("Transfer",stub);
System.out.println("Success binding the remote object"
;
System.out.println("Server Ready!"
;
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void jMenuItem1_actionPerformed(ActionEvent actionEvent) {
new configDir().setVisible(true);
}
public void jMenuItem2_actionPerformed(ActionEvent actionEvent) {
new Privilege().setVisible(true);
}
public void jMenuItem3_actionPerformed(ActionEvent actionEvent) {
System.exit(0);
}
}
class ServerMain_jMenuItem3_actionAdapter
implements ActionListener {
private ServerMain adaptee;
ServerMain_jMenuItem3_actionAdapter(ServerMain adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuItem3_actionPerformed(actionEvent);
}
}
class ServerMain_jMenuItem2_actionAdapter
implements ActionListener {
private ServerMain adaptee;
ServerMain_jMenuItem2_actionAdapter(ServerMain adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuItem2_actionPerformed(actionEvent);
}
}
class ServerMain_jMenuItem1_actionAdapter
implements ActionListener {
private ServerMain adaptee;
ServerMain_jMenuItem1_actionAdapter(ServerMain adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent actionEvent) {
adaptee.jMenuItem1_actionPerformed(actionEvent);
}
}