package myprojects.ftpclient;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
class FTPClient extends JFrame {
JPanel pn=new JPanel();
JPanel pn1=new JPanel(new FlowLayout(0));
JPanel pn2=new JPanel(new FlowLayout(0));
JPanel pn3=new JPanel(new FlowLayout(0));
JPanel pn4=new JPanel(new GridLayout(1,2));
JPanel pc=new JPanel();
JPanel ps=new JPanel();
JLabel labelHost=new JLabel();
JTextField textFieldHost=new JTextField(18);
JButton buttonLink=new JButton(); //连接按钮
JLabel labelUser=new JLabel();
JTextField textFieldUser=new JTextField(8);
JLabel labelPassword=new JLabel();
TextField textFieldPassword=new TextField(8); //密码
JButton buttonDisconnect=new JButton(); //断开按钮
JLabel labelFileShow=new JLabel();
JLabel labelclient=new JLabel();
JLabel labelserver=new JLabel();
JLabel labelstatus=new JLabel();
List listclient=new List(5);
List listserver=new List(5);
PopupMenu pmc1=new PopupMenu();
java.awt.MenuItem upload=new MenuItem("上传");
java.awt.MenuItem view=new MenuItem("浏览");
PopupMenu pms=new PopupMenu();
java.awt.MenuItem download=new MenuItem("下载");
java.awt.MenuItem reflash=new MenuItem("刷新");
TelnetInputStream is=null;//输入流对象
TelnetOutputStream os=null;
//构造方法
public FTPClient(){
setSize(new Dimension(430,485));
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try{
jbInit();
}
catch(Exception e){
e.printStackTrace();
}
}
//初始化成员
private void jbInit() throws Exception{
add(pn,BorderLayout.NORTH);
add(pc,BorderLayout.CENTER);
add(ps,BorderLayout.SOUTH);
pn.setLayout(new GridLayout(4,1));
pn.add(pn1);
pn.add(pn2);
pn.add(pn3);
pn.add(pn4);
pc.setLayout(new GridLayout(1,2));
pc.add(listclient);
listclient.add("fds");
listclient.add("fds");
listclient.add("fds");
listclient.add("fds");
listclient.add("fds");
pc.add(listserver);
listserver.add("s");
listserver.add("s");
listserver.add("s");
listserver.add("s");
listserver.add("s");
pn1.add(labelHost);
pn1.add(textFieldHost);
pn1.add(buttonLink);
pn2.add(labelUser);
pn2.add(textFieldUser);
pn2.add(labelPassword);
pn2.add(textFieldPassword);
pn2.add(buttonDisconnect);
pn3.add(labelFileShow);
pn4.add(labelclient);
pn4.add(labelserver);
ps.add(labelstatus);
labelHost.setText("主机名:");
labelUser.setText("用户名:");
labelPassword.setText("密码:");
textFieldPassword.setEchoChar('*');
labelclient.setText("客户端:");
labelserver.setText("服务器端:");
pmc1.add(upload);
pmc1.add(view);
pms.add(download);
pms.add(reflash);
listclient.add(pmc1);
listclient.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {
pmc1.show(listclient, me.getX(), me.getY());
}
}
});
listserver.add(pms);
listserver.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
if (SwingUtilities.isRightMouseButton(me)) {
pms.show(listserver, me.getX(), me.getY());
}
}
});
buttonLink.setLabel("连接");
buttonLink.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
}
});
buttonLink.setEnabled(true);
buttonDisconnect.setLabel("断开");
buttonDisconnect.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
buttonDisconnect.setEnabled(false);
labelFileShow.setText("目录列表");
upload.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
upload_actionPerformed(e); //调用处理程序
}
});
view.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
view_actionPerformed(e); //调用处理程序
}
});
download.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
download_actionPerformed(e); //调用处理程序
}
});
reflash.addActionListener(new java.awt.event.ActionListener(){
public void actionPerformed(ActionEvent e){
reflash_actionPerformed(e); //调用处理程序
}
});
}
void buttonLink_ActionPerformed(ActionEvent e){ //响应连接按钮事件,连接到服务器
try {
FTPClient ftp=new FTPClient();
ftp.connect(textFieldHost.getText(),textFieldUser.getText(),textFieldPassword.getText());
labelstatus.setText("连接中,请等待.....");
}
catch (IOException e1) {
labelstatus.setText( textFieldHost.getText()+ "连接失败!!!");
e1.printStackTrace();
}
catch (SecurityException e2) {
labelstatus.setText( "No Purview!!!");
e2.printStackTrace();
}
if (textFieldHost.getText().equals(""))
labelstatus.setText( "Ftp服务器地址不能空!!!");
else if (textFieldPassword.getText().equals(""))
labelstatus.setText("用户名不能为空!!!");
else if (textFieldPassword.getText().equals(""))
labelstatus.setText( "密码不能为空!!!");
else
labelstatus.setText("已连接到服务器:" + textFieldHost.getText());
}
void buttonDisconnect_ActionPerformed(ActionEvent e){//响应断开按钮事件
try{
FTPClient ftp=new FTPClient();
ftp.disconnect(); //关闭客户端与服务器端的连接
listclient.setVisible(false);
labelstatus.setText("与服务器断开连接");
}
catch(IOException e1){
System.out.println("Error:"+e1);
}
buttonLink.setEnabled(true);
buttonDisconnect.setEnabled(false);
}
/*void view_actionPerformed(ActionEvent e){
public void showFileContents(){//列出服务器当前目录下的目录和文件名
FTPClient ftp=new FTPClient();
int ch;
StringBuffer buf=new StringBuffer();
try{
inStream=ftp.list(); //获取服务器当前目录下所有文件和目录的输入数据流
while((ch=inStream.read())>=0){ //从输入流中读取数据
buf.append((char)ch); //保存数据到缓冲区
}
listserver.append(buf.toString()); //将目录和文件名显示在文本框中
inStream.close(); //关闭输入流
}
catch(Exception e){
System.out.println("Error:"+e);
}
}
}
*/
void upload_actionPerformed(String infile){
int i=infile.lastIndexOf("\\");
String outfile=infile.substring(i+1,infile.length());
labelstatus.setText("infile["+infile+"]");
labelstatus.setText("outfile["+outfile+"]lastindex["+i+"]");
try{
TelnetOutputStream os=FTPClient.put(outfile);
File file_in=new File(infile);
FileInputStream is=new FileInputStream(file_in);
byte[] bytes=new byte[1024];
int c;
while((c=is.read(bytes))!=-1){
os.write(bytes,0,c);
labelstatus.setText("bytes["+bytes+"]");
}
is.close();
os.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}
void download_actionPerformed(String infile){
int ch;
String file="t.txt";
int i=file.lastIndexOf("/");
String outfile=file.substring(i+1,file.length());
outfile="C:\\temp\\"+outfile;
System.out.println("-------------------file["+file+"]");
System.out.println("-------------------outfile["+outfile+"]lastindex["+i+"]");
try{
RandomAccessFile rFile=new RandomAccessFile(outfile,"rw");
rFile.seek(0);
TelnetInputStream tInput=FTPClient.get(file);
DataInputStream dInput=new DataInputStream(tInput);
while((ch=dInput.read())>=0){
rFile.write(ch);
}
dInput.close();
rFile.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}
void reflash_actionPerformed(){
}
protected void processWindowEvent(WindowEvent e){ //关闭窗口
super.processWindowEvent(e);
if(e.getID()==WindowEvent.WINDOW_CLOSING){
try{
if(is!=null)
is.close();
}
catch(IOException e1){
System.out.println("Error:"+e1);
}
System.exit(0);
}
}
public static void main(String args[]) {
System.out.println("Starting FTPClient...");
FTPClient mainFrame = new FTPClient();
mainFrame.setVisible(true);
}
}
以上的程序编译时还出现了几个错误,根据错误提示信息要加connect()和disconnect()两个方法,但我不知道怎么加.上传和下载功能实现不了.欢迎大家阅读我的程序,给出宝贵意见.我的电子邮件xiabingyi@126.com.