欢迎光临
我们一直在努力

java实现FTP功能(源代码)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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);
        }
}

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » java实现FTP功能(源代码)-JSP教程,Java技巧及代码
分享到: 更多 (0)