Java实现ftp功能(源程序)

2016-02-19 15:35 30 1 收藏

下面图老师小编要跟大家分享Java实现ftp功能(源程序),简单的过程中其实暗藏玄机,还是要细心学习,喜欢还请记得收藏哦!

【 tulaoshi.com - 编程语言 】

  

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

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

来源:http://www.tulaoshi.com/n/20160219/1609979.html

延伸阅读
标签: Web开发
代码如下: % ' make sure you have the 6 dir_*.gif files! ' yes, you can copy ours if ya want too.. :) ' http://line9.com/img/dir_dir.gif .. etc. Function ShowImageForType(strName) strTemp = strName If strTemp "dir" Then strTemp = LCase(Right(strTemp, Len(strTemp) - InStrRev(strTemp, ".", -1, 1))) End If Select Case ...
---- 摘要:在Java出现之前,编写多线程程序是一件烦琐且伴随许多不安全因素的事情。利用Java,编写安全高效的多线程程序变得简单,而且利用多线程和Java的网络包我们可以方便的实现多线程服务器程序。 ---- Java是伴随Internet的大潮产生的,对网络及多线程具有内在的支持,具有网络时代编程语言的一切特点。从Java的当前应用看...
标签: ASP
  使用了imagelib组件!      <%      site_id=Request.QueryString("site_id")      if isempty(site_id) then      Response.End      end if        &nb...
工作需要想实现,让两方的用户登录一台FTP服务器,对自己的目录可写,对对方的目录有只读的权限,且不能读写第三方的目录。采用的操作系统是FB,FTP服务器端的软件pureftp,默认自己的目录是FTP登录后的根目录。实现过程如下:使用ftpadmin建立四个用户,A用户使用exchange_ME1写数据,提供exchange_ME2让B用户读数据;B用户使用exchange_YOU...
分3步. 1: 编译。选取UltraEdit的菜单: 高级-〉工具配置编译C# Console命令行填:csc %n%e工作目录:%p名称随意。保存活动文件,输出到列表窗口,捕捉输出 这3个都选上。 最后点击 插入。 %P Path only ("C:\project\test\")%N fileName only ("test")%E Extension only (".c") 编译C# Console 有图标:csc /wi...

经验教程

659

收藏

25
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部