会员: 密码:  免费注册 | 忘记密码 | 会员登录 网页功能: 加入收藏 设为首页 网站搜索  
 安全技术技术文档
  · 安全配制
  · 工具介绍
  · 黑客教学
  · 防火墙
  · 漏洞分析
  · 破解专题
  · 黑客编程
  · 入侵检测
 安全技术论坛
  · 安全配制
  · 工具介绍
  · 防火墙
  · 黑客入侵
  · 漏洞检测
  · 破解方法
  · 杀毒专区
 安全技术工具下载
  · 扫描工具
  · 攻击程序
  · 后门木马
  · 拒绝服务
  · 口令破解
  · 代理程序
  · 防火墙
  · 加密解密
  · 入侵检测
  · 攻防演示
技术文档 > JAVA
用VJ++实现FTP的功能
发表日期:2003-05-28 00:00:00作者:n 出处:  

在这里提醒一点,在网络编程中应该多使用线程,否则会出现死锁现象。

就程序界面不刷新了。

下面的程序是在VJ++中写的,并且已经通过,能运行。大家可以自己改为纯java的。

import com.ms.wfc.app.*;

import com.ms.wfc.core.*;

import com.ms.wfc.ui.*;

import com.ms.wfc.html.*;

import sun.net.ftp.*;

import sun.net.*;

import java.io.* ;

import java.net.*;

/**

* 读写在另一个socket中进行,否则会阻塞!

*/

public class Ftp_Study extends Form{

FtpClient aftp;

DataOutputStream outputs;

DataInputStream inputs;

TelnetInputStream ins;

TelnetOutputStream outs;

int ch;

String a="没有连接主机";

String b="正在连接,请等待.....";

String currentPath="/";//当前的目录

String selectFilePath="";//被中的文件

  String hostname="";

String user="";

String pwd="";

int port =21;

public Ftp_Study()

{

 super();

 initForm();

 init();

 

 

}

/**

 * Ftp_Study overrides dispose so it can clean up the

 * component list.

 */

public void dispose(){

 super.dispose();

 components.dispose();

}

private void edit2_textChanged(Object source, Event e){

 

}

private void label3_click(Object source, Event e){

 

}

private void button1_click(Object source, Event e){//connect

 file://this.setftpProxyHost ("61.152.210.94",3128);

 

 this.label1.setText("正在连接,请等待.....");

 this.hostname =this.edit1.getText();

 this.user=this.edit2.getText();

 this.pwd=this.edit3.getText();

    if (this.connect(this.hostname,this.user,this.pwd)){

        this.button1.setEnabled(false);

        this.button2.setEnabled(true);

    } 

}

private void button2_click(Object source, Event e){//stop

 System.out.println ("button stop start");

 this.stop();

    this.button1.setEnabled(true);

    this.button2.setEnabled(false);

    this.label1.setText("与主机"+hostname+"连接已断开!");

 this.edit4.setText ("");

 System.out.println ("button stop end");

}

private void button3_click(Object source, Event e){//go

 System.out.println ("button enterDirectoryThread start");

 String path=this.edit5 .getText (); 

 Thread t=new enterDirectoryThread(path);

 t.setPriority (Thread.MIN_PRIORITY );

 t.start();

 System.out.println ("button enterDirectoryThread end");

}

private void button4_click(Object source, Event e){//getfile 

 System.out.println ("button getFileThread start");

 Thread t=new getFileThread();

 t.setPriority (Thread.MIN_PRIORITY );

 t.start(); 

 System.out.println ("button getFileThread end");

}

private void button5_click(Object source, Event e){//sendfile

 System.out.println ("button sendFileThread start");

 openFileDialog1.showDialog();

 this.selectFilePath=openFileDialog1.getFileName ();

 System.out.println (this.selectFilePath+" has been selected!");

 Thread t=new sendFileThread();

 t.setPriority (Thread.MIN_PRIORITY );

 file://t.start();

 System.out.println ("button sendFileThread end");

 

}

/**

 * NOTE: The following code is required by the Visual J++ form

 * designer. It can be modified using the form editor. Do not

 * modify it using the code editor.

 */

Container components = new Container();

Label label1 = new Label();

Label label2 = new Label();

Label label3 = new Label();

Label label4 = new Label();

Edit edit1 = new Edit();

Edit edit2 = new Edit();

Edit edit3 = new Edit();

Edit edit4 = new Edit();

Button button1 = new Button();

Button button2 = new Button();

Edit edit5 = new Edit();

Label label5 = new Label();

Button button3 = new Button();

Button button4 = new Button();

Button button5 = new Button();

OpenFileDialog openFileDialog1 = new OpenFileDialog();

private void initForm()

{

 this.setText("Ftp_Study");

 this.setAutoScaleBaseSize(new Point(6, 12));

 this.setClientSize(new Point(455, 348));

 label1.setLocation(new Point(16, 8));

 label1.setSize(new Point(224, 24));

 label1.setTabIndex(0);

 label1.setTabStop(false);

 label1.setText("没有连接主机");

 label2.setLocation(new Point(8, 40));

 label2.setSize(new Point(48, 23));

 label2.setTabIndex(1);

 label2.setTabStop(false);

 label2.setText("Host:");

 label3.setLocation(new Point(8, 72));

 label3.setSize(new Point(48, 23));

 label3.setTabIndex(2);

 label3.setTabStop(false);

 label3.setText("UseID:");

 label3.addOnClick(new EventHandler(this.label3_click));

 label4.setLocation(new Point(184, 72));

 label4.setSize(new Point(56, 23));

 label4.setTabIndex(3);

 label4.setTabStop(false);

 label4.setText("PWD:");

 edit1.setLocation(new Point(64, 40));

 edit1.setSize(new Point(296, 19));

 edit1.setTabIndex(4);

 edit1.setText("");

 edit2.setLocation(new Point(56, 72));

 edit2.setSize(new Point(104, 19));

 edit2.setTabIndex(5);

 edit2.setText("");

 edit2.addOnTextChanged(new EventHandler(this.edit2_textChanged));

 edit3.setLocation(new Point(240, 72));

 edit3.setSize(new Point(104, 19));

 edit3.setTabIndex(6);

 edit3.setText("");

 edit3.setPasswordChar('*');

 edit4.setLocation(new Point(8, 160));

 edit4.setSize(new Point(440, 184));

 edit4.setTabIndex(7);

 edit4.setText("");

 edit4.setMultiline(true);

 edit4.setReadOnly(true);

 edit4.setScrollBars(ScrollBars.BOTH);

 edit4.setWordWrap(false);

 button1.setLocation(new Point(384, 16));

 button1.setSize(new Point(64, 24));

 button1.setTabIndex(8);

 button1.setText("连接");

 button1.addOnClick(new EventHandler(this.button1_click));

 button2.setEnabled(false);

 button2.setLocation(new Point(384, 48));

 button2.setSize(new Point(64, 24));

 button2.setTabIndex(9);

 button2.setText("断开");

 button2.addOnClick(new EventHandler(this.button2_click));

 edit5.setLocation(new Point(56, 104));

 edit5.setSize(new Point(312, 19));

 edit5.setTabIndex(10);

 edit5.setText("");

 label5.setLocation(new Point(16, 104));

 label5.setSize(new Point(40, 24));

 label5.setTabIndex(11);

 label5.setTabStop(false);

 label5.setText("Path:");

 button3.setLocation(new Point(384, 80));

 button3.setSize(new Point(64, 23));

 button3.setTabIndex(12);

 button3.setText("Go!");

 button3.addOnClick(new EventHandler(this.button3_click));

 button4.setLocation(new Point(224, 128));

 button4.setSize(new Point(64, 24));

 button4.setTabIndex(13);

 button4.setText("getFile");

 button4.addOnClick(new EventHandler(this.button4_click));

 button5.setLocation(new Point(304, 128));

 button5.setSize(new Point(64, 24));

 button5.setTabIndex(14);

 button5.setText("sendFile");

 button5.addOnClick(new EventHandler(this.button5_click));

 /* @designTimeOnly openFileDialog1.setLocation(new Point(288, 8)); */

 this.setNewControls(new Control[] {

    button5,

    button4,

    button3,

    label5,

    edit5,

    button2,

    button1,

    edit4,

    edit3,

    edit2,

    edit1,

    label4,

    label3,

    label2,

    label1});

}

public void init(){

 this.edit1.setText("202.115.16.2");

 this.edit2.setText ("anonymous");

 this.edit3.setText ("0000"); 

}

public boolean connect(String hostname,String uid,String pwd){

 this.hostname =hostname;

 this.label1 .setText (this.b );

 this.a ="连接主机: "+hostname+" 成功";

 boolean bsuccess=true;//success

 

 try{

  this.aftp =new FtpClient(this.hostname);

  this.aftp.login (uid,pwd);

  this.aftp .binary ();

  this.showFileContents ();

 }

 catch(FtpLoginException e){

  this.a="无权限与主机: "+hostname+" 连接";

  bsuccess=false;

 }

 catch(IOException e){

  this.a="连接主机: "+hostname+" 失败"; 

  bsuccess=false;

 }

 catch(SecurityException e){

  this.a="无权限与主机: "+hostname+" 失败"; 

  bsuccess=false;

 }

 finally{

  this.label1 .setText (a);

  return bsuccess;

 }  

}

public void stop(){

 try{

  this.aftp.closeServer (); 

 }

 catch(IOException e){

 }

}

/**

 * filepath --local file path

 */

public boolean sendFile(String filepath){

 boolean result =true;//success?

 if(this.aftp !=null){

  this.label1 .setText("正在粘贴文件,请耐心等待....");

  String contentperline;

  a="粘贴成功!";

  /*

  try {

  FtpClient ftpClient=new FtpClient();

  ftpClient.openServer(this.hostname);

  ftpClient.login(this.user ,this.pwd);

  if (this.selectFilePath.length()!=0)

    ftpClient.cd(this.currentPath);

  ftpClient.binary();

  String fn=new File(this.selectFilePath).getName();//filename

  TelnetOutputStream is=ftpClient.put(fn);

  File file_out=new File(fn);

  FileOutputStream os=new

  FileOutputStream(file_out);

  byte[] bytes=new byte[1024];

  int c;

  while ((c=is.write(bytes))!=-1) {

    os.write(bytes,0,c);

  }

  is.close();

  os.close();

  ftpClient.closeServer();

  }catch(IOException e){

  a="粘贴失败!";

  result=false; 

  } */

  this.label1 .setText (a);

  this.showFileContents ();

 }

 else{

  result=false; 

 }

 return result;

}

/**

 * show all the conten of the file list

 */

public void showFileContents(){//其中的空当都是空格!

 StringBuffer buf=new StringBuffer();

 this.edit4.setText ("");

 try{

  this.ins=this.aftp.list ();//return the telnetinputstream

  while((this.ch=this.ins.read())>0){

  buf.append ((char)ch);//空当都 是空格  

  }

  this.edit4.setText(buf.toString());

  this.ins.close ();

  System.out.println ("showFileContents ins closed");

 }

 catch(IOException e){ 

 } 

}

/**

 * 进入目录中

 */

public void enterDiectory(String path){

 try{

  if (path.length()>0){

  this.aftp.cd(path); file://enter in the directory  

  this.showFileContents();

  if(path.equals("..") ){

   int pos=0;

   pos=this.currentPath.lastIndexOf("/");

   this.currentPath=this.currentPath.substring(0,pos);  

   pos=this.currentPath.lastIndexOf("/");

   this.currentPath=this.currentPath.substring (0,pos+1);

  }

  else

   this.currentPath=this.currentPath+path+"/";

  this.a="当前目录: "+this.currentPath;

  System.out.println ("this.currentPath: "+this.currentPath);

  }

 }

 catch(IOException e){

  this.a="无法进入目录: /"+path; 

 }

 finally{

  this.label1 .setText (this.a );  

 }

}

public void getFile(String filename){

 try {

  FtpClient ftpClient=new FtpClient();

  ftpClient.openServer(this.hostname);

  ftpClient.login(this.user, this.pwd);

  if (this.currentPath.length()!=0)

  ftpClient.cd(this.currentPath);

  ftpClient.binary();

  TelnetInputStream is=ftpClient.get(filename);

  File file_out=new File(filename);

  FileOutputStream os=new FileOutputStream(file_out);

  byte[] bytes=new byte[1024];

  int c;

  while ((c=is.read(bytes))!=-1) {

   os.write(bytes,0,c);

  }

  is.close();

  os.close();

  ftpClient.closeServer(); 

 } catch (IOException ex) {;}

}

public void getFile(){

 String filename=this.edit5.getText();

 this.getFile(filename); 

}

/*

public void setftpProxyHost(String ProxyHost,int ProxyPort){

 this.aftp.ftpProxyHost=ProxyHost;

 this.aftp.ftpProxyPort=ProxyPort;

 this.aftp.useFtpProxy=true;

}*/

/**

 * The main entry point for the application.

 *

 * @param args Array of parameters passed to the application

 * via the command line.

 */

public static void main(String args[])

{

 Application.run(new Ftp_Study());

}

class getFileThread extends Thread {

 public void run(){

  getFile();

  System.out.println ("getFile is completed");

 }    

}

class sendFileThread extends Thread {

 public void run(){ 

  sendFile(selectFilePath);

  System.out.println ("sendFile is completed. ");

 }    

}

class enterDirectoryThread extends Thread {

 String szPath;

 

 public enterDirectoryThread(String path){

  this.szPath =path;

 }

 

 public void run(){

  enterDiectory(this.szPath);

  System.out.println ("enterDiectory is completed");

 }

}

class connectThread extends Thread {

 String hostname="";

 String uid="";

 String pwd="";

 

 public connectThread(String hostname1,String uid1,String pwd1){

  this.hostname=hostname1;

  this.uid=uid1;

  this.pwd =pwd1;

 }

 

 public void run(){

  connect(this.hostname,this.uid,this.pwd);

  System.out.println ("connect ... ");

 }    

}

}

本人水平有限,望多指教。

返回顶部】 【打印本页】 【关闭窗口

关于我们 / 给我留言 / 版权举报 / 意见建议 / 网站编程QQ群   
Copyright ©2003- 2024 Lihuasoft.net webmaster(at)lihuasoft.net 加载时间 0.00168