学习过程中,想实现FTP客户端的一些功能,从网上找到一个程序试着调试了一下.
import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class FtpApplet1 extends Applet{
Label LblPrompt;
Button BtnConn;
Button BtnClose;
TextField TxtUID;
TextField TxtPWD;
TextField TxtHost;
TextArea lsArea;
FtpClient aftp;
String hostname="";
public String a="没有连接主机";
TelnetInputStream ins;
TelnetOutputStream outs;
DataOutputStream outputs;
int ch;
public void init(){
setBackground(Color.white);
setLayout(new GridBagLayout());
//GridBagConstraints 类指定使用 GridBagLayout 类布置的组件的约束。
GridBagConstraints GBC=new GridBagConstraints();
LblPrompt=new Label("没有连接到主机");
LblPrompt.setAlignment(Label.LEFT);
BtnConn=new Button("连接");
BtnClose=new Button("断开");
BtnClose.enable(false);
TxtUID=new TextField("",15);
TxtPWD=new TextField("",15);
TxtPWD.setEchoCharacter('*');
TxtHost=new TextField("",20);
Label LblUID=new Label("User ID");
Label LblPWD=new Label("PWD");
Label LblHost=new Label("Host");
lsArea=new TextArea(30,80);
lsArea.setEditable(false);
//REMAINDER指定此组件是其行或列中的最后一个组件。
GBC.gridwidth=GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
//以前实现窗口的布局
((GridBagLayout)getLayout()).setConstraints(LblPrompt,GBC);
add(LblPrompt);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblHost,GBC);
add(LblHost);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(TxtHost,GBC);
add(TxtHost);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblUID,GBC);
add(LblUID);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(TxtUID,GBC);
add(TxtUID);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(LblPWD,GBC);
add(LblPWD);
GBC.gridwidth=1;
((GridBagLayout)getLayout()).setConstraints(TxtPWD,GBC);
add(TxtPWD);
GBC.gridwidth=1;
GBC.weightx=2;
((GridBagLayout)getLayout()).setConstraints(BtnConn,GBC);
add(BtnConn);
GBC.gridwidth=GridBagConstraints.REMAINDER;
((GridBagLayout)getLayout()).setConstraints(BtnClose,GBC);
add(BtnClose);
GBC.gridwidth=GridBagConstraints.REMAINDER;
GBC.fill = GridBagConstraints.HORIZONTAL;
((GridBagLayout)getLayout()).setConstraints(lsArea,GBC);
add(lsArea);
}
public boolean connect(String hostname,String uid,String pwd){
//this关键字只能在方法内部使用,表示对"调用方法的那个对象"
//的引用。当需要明确明确指出对当前对象的引用时,才需要使用
//this字键字
this.hostname=hostname;
LblPrompt.setText("正在连接,请等待...");
try{
aftp=new FtpClient(hostname);
aftp.login(uid,pwd);
aftp.binary();
//显示目录
showFileContents();
}
catch(FtpLoginException e){
a="无权限与主机"+hostname+"连接!";
LblPrompt.setText(a);
return false;
}
catch(IOException e){
a="连接主机"+hostname+"失败!";
LblPrompt.setText(a);
return false;
}
catch(SecurityException e){
a="无权限与主机"+hostname+"连接!";
LblPrompt.setText(a);
return false;
}
LblPrompt.setText("连接主机:"+hostname+"成功!");
return true;
}
public void stop(){
try{
aftp.closeServer();
}
catch(IOException e){
}
}
public void showFileContents(){
StringBuffer buf=new StringBuffer();
lsArea.setText("");
try{
ins=aftp.list();
while((ch=ins.read())>=0){
buf.append((char)ch);
}
lsArea.appendText(buf.toString());
ins.close();
}
catch(IOException e){
}
}
public void paint(Graphics g){}
public boolean action(Event evt,Object obj){
if(evt.target==BtnConn){
LblPrompt.setText("正在连接,请等待...");
if(connect(TxtHost.getText(),TxtUID.getText(),TxtPWD.getText())){
BtnConn.setEnabled(false);
BtnClose.setEnabled(true);
}
return true;
}
if(evt.target==BtnClose){
stop();
BtnConn.enable(true);
BtnClose.enable(false);
LblPrompt.setText("与主机"+hostname+"连接已断开!");
return true;
}
return super.action(evt,obj);
}
public boolean sendFile(String filepathname){
boolean result=true;
if(aftp!=null){
LblPrompt.setText("正在粘贴文件,请耐心等待...");
String contentperline;
try{
a="粘贴成功";
String fg=new String("//");
int index=filepathname.lastIndexOf(fg);
String filename=filepathname.substring(index+1);
File localFile;
localFile=new File(filepathname);
RandomAccessFile sendFile=new RandomAccessFile(filepathname,"r");
sendFile.seek(0);
outs=aftp.put(filename);
outputs=new DataOutputStream(outs);
while(sendFile.getFilePointer()<sendFile.length()){
ch=sendFile.read();
outputs.write(ch);
}
outs.close();
sendFile.close();
}
catch(IOException e){
a="粘贴失败!";
result=false;
}
LblPrompt.setText(a);
showFileContents();
}
else{
result=false;
}
return result;
}
public static void main(String[] args){
Frame f=new Frame("FTP Clent");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
FtpApplet1 ftp=new FtpApplet1();
//需要先初始化
ftp.init();
//再做开始的动作
ftp.start();
//
f.add(ftp);
//调整此窗口的大小,以适合其子组件的首选大小和布局。
//如果该窗口和/或其所有者仍不可显示,则两者在计算首选
//大小之前变得可显示。在计算首选大小之后,将会验证该
//Window。
f.pack();
//根据参数的值显示或隐藏此组件。
f.setVisible(true);
}
}
//1.this
//在构造器中调用构造器。可能为一个类写了多个构造器有时可能想在一个
//构造器中调用另一个构造器,以避免重复代码。尽管可以用this调用一个
//构造器,但却不能调用两个。
//此外,必须将构造器调用置于最初始处,否则编译器会报错。
上例中只实现了文件列表,接下来该实现的是文件的下载和同步了.