📄 midclient.java
字号:
p1.add("North",p11);
p1.add("Center",p12);
// p1.add("South",p13);
AddHost.add("North",p1);
AddHost.add("Center",p2);
//AddHost.setBounds(200,150,400,130);
AddHost.setSize(150,150);
AddHost.show();
}
//服务器文件存储。。。。。。
public void down()
{
try
{
int result;
existFile=false;
result=Jsave.showSaveDialog(treePane);
if(result==Jsave.APPROVE_OPTION)
{
selectFile = Jsave.getSelectedFile(); //取得选择的文件
fileName=String.valueOf(selectFile);
if(selectFile.exists())
{
String message = "文件已经存在,要覆盖吗?";
Object[] saveButton = {"OK","NO"};
JOptionPane save= new JOptionPane(message,JOptionPane.WARNING_MESSAGE,
JOptionPane.DEFAULT_OPTION,null,saveButton);
JDialog saveD = save.createDialog(save,"保存文件"); //产生 saveD 对话框
saveD.setVisible(true);
Object push = save.getValue();
if(push == saveButton[0])
{
existFile=true;
}
}
else
{
existFile=true;
}
if(existFile)
{
File outputFile = new File(selectFile.getAbsolutePath());
fo=new FileOutputStream(outputFile);
String str=String.valueOf(tree.getSelectionPath());
System.out.println(str);
out.println("&download&");
out.flush();
out.println(str);
out.flush();
}
}
}
catch(IOException e)
{}
}
BufferedInputStream bufin;
public void connect()
{
try
{
disConnect=false;
ClientSocket=new Socket(HostName,IPPort);
InetAddress HostAddress=ClientSocket.getInetAddress();
ConnectIpText.setText(HostAddress.toString());
out=new PrintWriter(ClientSocket.getOutputStream(),true);
in=new BufferedReader(new InputStreamReader(ClientSocket.getInputStream()));
bufin=new BufferedInputStream(ClientSocket.getInputStream());
if(node1.getChildCount()>0)
{
out.println("&pass&");
out.flush();
}
else
{
String s1=new String("&nopass&");
while(s1.equals("&nopass&"))
{
CP_dialog();
if(passOk)
{
out.println(passw.getText());
out.flush();
passw.setText("");
s1=in.readLine();
if(s1.equals("&nopass&"))
{
JOptionPane.showMessageDialog(getContentPane(),
"密码错误,请重输");
}
}
else
{
disConnect=true;
out.close();
in.close();
ClientSocket.close();
JOptionPane.showMessageDialog(getContentPane(),
"目前处于非连接状态,请点击主机I重新建立连接");
break;
}
}
if(!disConnect)
{
startPort=true;
String s=in.readLine();
while(!s.equals("&&over&&"))
{
node1.add(new DefaultMutableTreeNode(s));
s=in.readLine();
}
System.out.println("over");
}
}
}
catch(Exception e){}
}
//密码输入窗口
public void CP_dialog()
{
passOk=true;
JLabel host =new JLabel("主机");
JTextField hostText =new JTextField(10);
hostText.setEditable(false);
JLabel ipPort =new JLabel("端口");
JTextField portText =new JTextField(10);
portText.setEditable(false);
JLabel pass =new JLabel("密码");
hostText.setText(HostName);
portText.setText(String.valueOf(IPPort));
listen=new Dialog(this,"登陆服务器",true);
listen.addWindowListener
(
new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
passOk=false;
HostName=null;
passw.setText("");
out.println("&exit&");
out.flush();
listen.dispose();
}
}
);
JPanel p1=new JPanel(new BorderLayout());
JPanel p11=new JPanel();
JPanel p12=new JPanel();
JPanel p13=new JPanel();
JPanel p2=new JPanel(new FlowLayout());
host.setFont(new Font("宋体",Font.PLAIN,12));
hostText.setFont(new Font("宋体",Font.PLAIN,12));
ipPort.setFont(new Font("宋体",Font.PLAIN,12));
portText.setFont(new Font("宋体",Font.PLAIN,12));
pass.setFont(new Font("宋体",Font.PLAIN,12));
passw.setFont(new Font("宋体",Font.PLAIN,12));
p11.add(host);
p11.add(hostText);
p12.add(ipPort);
p12.add(portText);
p13.add(pass);
p13.add(passw);
passw.addKeyListener(new KeyLis(this,1));
p1.add("North",p11);
p1.add("Center",p12);
p1.add("South",p13);
p2.add(ok1);
p2.add(cancel1);
listen.add("North",p1);
listen.add("Center",p2);
listen.setSize(150,160);
listen.show();
}
//接收文件流
public void startCopy()
{
try
{
String strnum=in.readLine();
ByteNum=Integer.parseInt(strnum);
byte[] data=new byte[4096];
copy=true;
try
{
Thread.sleep(1);
}
catch(Exception e3){}
BufferedInputStream bufin=new BufferedInputStream(ClientSocket.getInputStream());
//定义一个输入流来接收文件的字节流
int b;
int j=0;
Jrate=0;
finish=0;
while(finish++<ByteNum)
{
// if((b=bufin.read())==-1){System.out.println(b+" "+finish); break;}//为什么不会出现-1
b=bufin.read();
if(b<=0)
data[j]=(byte)(b);
else
data[j]=(byte)(b-256);
if(j++>=4095)
{
fo.write(data,0,4096);
fo.flush();
j=0;
Jrate++;
}
}
fo.write(data,0,j);
fo.flush();
// bufin.read(data,0,1048576-j+1);
fo.close();
copy=false;
}
catch(IOException e){}
}
//保存IP地址及端口
public void saveInit()
{
int yi=0;
try
{
File AdressSave=new File("HostAddress.txt");
FileWriter readOut=new FileWriter(AdressSave);
BufferedWriter bufOut=new BufferedWriter(readOut);
while(yi<Address.getItemCount())
{
bufOut.write(Address.getItem(yi));
bufOut.newLine();
bufOut.flush();
yi++;
}
bufOut.close();
readOut.close();
System.out.println("save finished");
}
catch(Exception e2){}
}
public void quit()
{
try
{
if(startPort)
{
out.println("Bye_Bye");
out.flush();
in.close();
out.close();
ClientSocket.close();
}
}
catch(IOException end){}
}
//增加主机个数
public void addchoice()
{
String HP_get=null;
boolean issame=false;
HP_get=Computer.getText()+":"+Port1.getText();
for(int yi=0;yi<Address.getItemCount();yi++)
{
if(HP_get.equals(Address.getItem(yi)))
issame=true;
}
if(!issame)
{
Address.insert(HP_get,Address.getItemCount());
treeModel = (DefaultTreeModel)tree.getModel();
DefaultMutableTreeNode parentNode=(DefaultMutableTreeNode)treeModel.getRoot();
System.out.println(String.valueOf(parentNode));
parentNode.add(new DefaultMutableTreeNode(Address.getItem(Address.getItemCount()-1)));
treeModel.reload();
}
AddHost.dispose();
}
public void update()
{
TreePath currentPath=null;
currentPath=tree.getSelectionPath();
node1 = (DefaultMutableTreeNode)(currentPath.getLastPathComponent());
if(!node1.isLeaf())
node1.removeAllChildren();
treeModel = (DefaultTreeModel)tree.getModel();
treeModel.reload((TreeNode)currentPath.getLastPathComponent());
{
try
{
DefaultMutableTreeNode node2=null;
out.println("addClientTree");
out.flush();
out.println(String.valueOf(currentPath));
out.flush();
String s=new String("");
String F_ornot=new String("");
s=in.readLine();
//node2=new DefaultMutableTreeNode(s);
//System.out.println(s);
if(s.equals("&isFile&"))
{
String filepath=in.readLine();
System.out.println(filepath);
int parameter=JOptionPane.showConfirmDialog(null,
"打开或运行 "+filepath,"打开["+HostName+"]",JOptionPane.YES_NO_OPTION);
if(parameter==0)
{
out.println("&execute&");
out.flush();
}
else
{
out.println("&inexecute&");
out.flush();
}
s=in.readLine();
}
while(!s.equals("&&over&&"))
{
F_ornot=in.readLine();
// System.out.println(F_ornot);
if(node2==null)
{
if(F_ornot.equals("directory"))
node2=new DefaultMutableTreeNode(s);
else
node2=new DefaultMutableTreeNode(s,false);
node1.add(node2);
}
else if(F_ornot.equals("directory"))
{
node1.add(new DefaultMutableTreeNode(s,true));
// System.out.println(F_ornot+" add directory");
}
else //if(F_ornot.equals("file"));
{
node1.add(new NodeInformation(s,false));
// System.out.println(F_ornot+" add file");
}
s=in.readLine();
}
if(node2!=null)
tree.scrollPathToVisible(new TreePath(node2.getPath()));
}
catch(Exception e1){}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -