package com.cus.net.ptf;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
public class ftpclient extends JFrame implements ActionListener
{
JTextField url_address,user_name,net_route,local_route,list_name,file_name,inlist_name;
JPasswordField user_password;
JButton url_connect,url_disconnect,loaddowm_Button,carry_Button,mkdir_Button,delete_Button,upward_Button,into_Button;
JLabel condition_Label,route_Label;//condition_Label为状态显示栏,route_Label为当前在此站点的当前目录
JPanel message_Panel;
JTable table;//用于显示返回目录下信息列表
Object data[][],name[]={"名称","类型","大小","创建时间"};
//以上为图形界面变量,以下为ftp连接参数
String s=null;
Socket mysocket;
DataInputStream in=null;
DataOutputStream out=null;
boolean linked=false;
ftpclient()
{
super("FTP—Java应用程序");
//组件及布局设置
Container con=getContentPane();
Box box1,box2,box3,box4,box5,box6,box7,box;
box1=Box.createHorizontalBox();
box1.add(Box.createHorizontalStrut(5));
box1.add(new JLabel("地址:"));
url_address=new JTextField("127.0.0.1",50);
box1.add(url_address);
box2=Box.createHorizontalBox();
box2.add(Box.createHorizontalStrut(5));
box2.add(new JLabel("用户名:"));
user_name=new JTextField("user",20);
box2.add(user_name);
box2.add(new JLabel("口令:"));
user_password=new JPasswordField("123456",15);
box2.add(user_password);
url_connect=new JButton("连接");
url_disconnect=new JButton("断开");
box2.add(url_connect);box2.add(Box.createHorizontalStrut(10));box2.add(url_disconnect);
box3=Box.createHorizontalBox();
box3.add(Box.createHorizontalStrut(5));
box3.add(new JLabel("网络目录:"));
route_Label=new JLabel();
box3.add(route_Label);
net_route=new JTextField();
box3.add(net_route);
box4=Box.createHorizontalBox();
box4.add(Box.createHorizontalStrut(5));
local_route=new JTextField();
box4.add(new JLabel("本机目录:"));
box4.add(local_route);
box5=Box.createHorizontalBox();
loaddowm_Button=new JButton("下载");carry_Button=new JButton("上传");
box5.add(loaddowm_Button);box5.add(Box.createHorizontalStrut(100));box5.add(carry_Button);
box6=Box.createHorizontalBox();
box6.add(Box.createHorizontalStrut(5));
mkdir_Button=new JButton("创建目录");delete_Button=new JButton("删除");
list_name=new JTextField(10);file_name=new JTextField(15);
box6.add(new JLabel("新建目录名:"));box6.add(list_name);box6.add(mkdir_Button);
box6.add(Box.createHorizontalStrut(10));
box6.add(new JLabel("删除文件名:"));box6.add(file_name);box6.add(delete_Button);
box7=Box.createHorizontalBox();
box7.add(Box.createHorizontalStrut(5));
box7.add(new JLabel("进入目录:"));
inlist_name=new JTextField();
box7.add(inlist_name);
into_Button=new JButton("确定");
box7.add(into_Button);
upward_Button=new JButton("返回上一级");
box7.add(upward_Button);
box=Box.createVerticalBox();
box.add(Box.createVerticalStrut(10));box.add(box1);box.add(Box.createVerticalStrut(10));box.add(box2);box.add(Box.createVerticalStrut(10));box.add(box3);box.add(Box.createVerticalStrut(10));box.add(box4);box.add(Box.createVerticalStrut(10));box.add(box5);box.add(Box.createVerticalStrut(10));box.add(box6);box.add(Box.createVerticalStrut(10));box.add(box7);
message_Panel=new JPanel();
message_Panel.setLayout(new BorderLayout());
message_Panel.setBackground(new Color(200,255,255));
message_Panel.add(new JLabel("当前目录下的所有信息"),BorderLayout.NORTH);
data=new Object[500][4];//最多支持显示500条信息
table=new JTable(data,name);
table.setEnabled(false);
table.setRowHeight(20);table.setBackground(new Color(200,255,255));
message_Panel.add(new JScrollPane(table),BorderLayout.CENTER);
con.add(message_Panel,BorderLayout.CENTER);
JPanel panel1=new JPanel();
panel1.add(new JLabel("当前状态:"));
condition_Label=new JLabel("未连接");
condition_Label.setForeground(Color.red);
panel1.add(condition_Label);
con.add(box,BorderLayout.NORTH);
con.add(panel1,BorderLayout.SOUTH);
validate();
setBounds(300,100,600,750);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//设置监听器
loaddowm_Button.addActionListener(this); carry_Button.addActionListener(this); mkdir_Button.addActionListener(this); delete_Button.addActionListener(this); upward_Button.addActionListener(this); into_Button.addActionListener(this);
url_connect.addActionListener(this);url_disconnect.addActionListener(this);
}
public boolean showing()
{
try
{
int n=in.readInt();
if(n==0)
{
String mess=in.readUTF();
condition_Label.setText(mess);
return false;
}
int i=0,j;
for(;i<n;i++)
{
for(j=0;j<4;j++)
{
data[i][j]=in.readUTF();
}
if(data[i][1].equals("文件"))data[i][2]=data[i][2]+" Byte";
data[i][3]=new Date(Long.parseLong(data[i][3].toString())).toString();
}
for(;i<500;i++)
{
for(j=0;j<4;j++)
{
data[i][j]="";
}
}
}
catch(IOException e1){condition_Label.setText("网络堵塞,请稍后重试!");}
repaint();
return true;
}
public void actionPerformed(ActionEvent e)//各类事件定义
{
if(e.getSource()==url_connect) //连接到指定服务器
{
try
{
mysocket=new Socket(url_address.getText().trim(),4523);
in=new DataInputStream(mysocket.getInputStream());
out=new DataOutputStream(mysocket.getOutputStream());
out.writeUTF(user_name.getText().trim());
out.writeUTF(new String(user_password.getPassword()).trim());
String zhuantai;
zhuantai=in.readUTF();
condition_Label.setText(zhuantai);
route_Label.setText("");
if(zhuantai.equals("欢迎用户user来到本站!"))
{
linked=true;
try
{
out.writeUTF("dir");}
catch(IOException e1){condition_Label.setText("网络堵塞,请稍后重试!");}
out.writeUTF(".");
showing();
}
}
catch(IOException e1)
{
condition_Label.setText("无法连接到服务器,请检查网络!");
}
repaint();
}
else if(e.getSource()==url_disconnect) //断开连接
{
if(out!=null)
{
try
{
out.writeUTF("byebye");
}
catch(IOException e1){condition_Label.setText("已断开连接!");}
try
{
mysocket.close();
route_Label.setText("");
condition_Label.setText("已断开连接!");
}
catch(IOException e1){}
}
}
else if(e.getSource()==loaddowm_Button) //下载指定文件
{
try
{
out.writeUTF("load");
String tem=route_Label.getText()+net_route.getText();
out.writeUTF(tem);
}
catch(IOException e1){condition_Label.setText("网络堵塞,请稍后重试!");}
try
{
if(in.readUTF().equals("ok"))
{
File loadfile=new File(local_route.getText());
if(!loadfile.createNewFile()){condition_Label.setText("文件已存在");out.writeUTF("操作取消");}
else
{
out.writeUTF("开始接收......");
conditi