网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  java实现ftp功能(源程序)     
  文章作者:未知  文章来源:水木森林  
  查看:120次  录入:管理员--2007-11-17  
 
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() < 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);
               }

             });
             ftpapplet ftp = new  ftpapplet();
             ftp.init();
             ftp.start();
             f.add(ftp);
             f.pack();
             f.setvisible(true);
        }
}
 
 
上一篇: java入门之:初学java注意什么?    下一篇: java学习备忘录(二)基础语法和数组
  相关文档
如何在linux下配置java开发环境详述 11-17
wirelessmessagingapi(2) 11-17
struts vs turbine 11-17
基础应用:Java Web中三层架构配置入门 08-07
j2ee基础:servlet中如何捕获session事件 12-05
java的核心升级 jdk 6 + jre 6 update 4 03-26
sun :未来填补数字鸿沟不是pc而是java 11-16
javabean实现多文件上传功能的两种方法 11-16
软件资产重用的具体内容 11-17
怎样用servletrunner来运行servlet 11-17
java classpath全解 11-17
java模块游戏规则和界面设计(一) 11-17
一个让98%的java程序员犯难的偏门问题 11-17
企业应用的web服务安全技术:框架(图) 11-17
[portal参考手册]portlet核心api 11-17
jsp与javabean组合循序渐进教程(2) 11-17
java编程准则 11-17
j2ee和xml为企业应用软件创造机会 11-17
建造linux开发环境--tomcat,jsp 11-17
为struts2的表单验证添加ajax效果实例 11-16
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息