阅 读 文 章

Java源码:实现简单的FTP功能

[来源:网上转载 (http://www.chinaunix.net) | 作者:网友(不详) | 时间:2007-07-07 | 浏览:人次 ]

applet代码如下:

JAVA源代码如下:
package jdeveloper.ftp;import sun.net.ftp.*;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()
论坛热门帖子: [lch203] 写得蛮好的linux学习笔记(10-21)
[黑马制造] 学习java的30个目标(10-19)
[笑傲股林] 做测试半年了,有点迷茫,应该再学些什么提高自己的测试水平和测试能力呢?(10-19)
[udp8589] 大家用google的来吱一声? 用百度的~~也来报道下?(10-18)
[沂偌掳兆] 本人总结的一些认为C++比较经典的书籍,希望对大家有用(10-18)
TAG标签: 功能 简单 实现 源码 new GBC String GridBagLayout 连接

最新评论 共有0位网友发表了评论

发表评论

评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名:(注册)
密码:
验证码:
匿名发表

网站地图友情连接交流论坛网站投稿广告服务联系我们留言本站长统计
Some rights reserved: www.chmhome.com, 鄂ICP备07010232号 E-mail:chinakafei@live.com,QQ:552766
中国咖啡技术网(Chmhome):国外编程技术书籍,中文编程手册,经典编程文章,交流技术,技术软件下载,计算机论文,毕业论文.