java实现FTP功能(源代码)
2008-02-23 09:39:51来源:互联网 阅读 ()
import sun.net.*;
import Java.awt.*;
import java.awt.event.*;
import java.Applet.*;
import java.io.*;
public class FtpApplet 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();
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:搭建WAP应用开发环境
下一篇:Motorola的按键值
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash