分享
 
 
 

用VJ++实现FTP的功能

王朝java/jsp·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

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

就程序界面不刷新了。

下面的程序是在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 ... ");

}

}

}

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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有