?? useftpapplet.java
字號:
import sun.net.ftp.*;
import sun.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
public class useFtpApplet extends Applet
{
//屬性
FtpClient aftp;
DataOutputStream outputs ;
TelnetInputStream ins;
TelnetOutputStream outs;
TextArea lsArea;
Label LblPrompt;
Button BtnConn;
Button BtnClose;
TextField TxtUID;
TextField TxtPWD;
TextField TxtHost;
int ch;
public String a="沒有連接主機";
String hostname="";
//初始化
public void init ()
{
//設置背景和布局
setBackground(Color.white);
setLayout(new 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);
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.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 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 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 static void main(String args[])
{
Frame f = new Frame("FTP Client");
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ //退出系統
System.exit(0);
}
});
useFtpApplet ftp = new useFtpApplet();
ftp.init();
ftp.start();
f.add(ftp);
f.pack();
f.setVisible(true);
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -