Implement FTP function with VJ ++

zhaozj2021-02-16  54

Here you remind a little in the network programming, you should use the thread, otherwise the deadlock will appear. The program interface is not refreshed.

The following program is written in VJ and has passed, can run. Everyone can change themselves to pure 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. *;

/ * ** another reader in the socket, would otherwise clog * / public class Ftp_Study extends Form {FtpClient aftp;! DataOutputStream outputs; DataInputStream inputs; TelnetInputStream ins; TelnetOutputStream outs; int ch; String a = "not connected to the host "; String B =" is connected, please wait ..... "; String currentpath =" / "; // Current directory string selectfilepath =" "; // is in the file string hostname =" "; String User = "" ""; String pwd = "; int port = 21;

Public ftp_study () {super (); inform (); init ();

/ ** * ftp_study overrides dispose so it can clean up the * component list. * / Public void disponent () {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 ("is connected, please wait .....") This.hostname = this.EDit1.Gettext (); this.user = this.edit2.getText (); this.pwd = this.edit3.gettext (); if (this.Connect (this.hostname, this. Hostname, this.user {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 ("Contact with the host" 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; 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 ("No Connection Host") ;

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 ("Connect"); 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 ("Disconnected); 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 = "Connection Host:" Hostname "success"; 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 = "no permissions Host: " Hostname " Connect "; bsuccess = false;} catch (ioException e) {this.a =" Connection Host: " Hostname " Fail "; bsuccess = false;} catch (securityException e) {this.a = "No permissions and host:" hostname "failed"; bsuccess = false;} finally {this.label1 .Settext (a); return bsuccess;}} public void stop ()}}} public void stop ()}} public void stop ()}}} public void statosserver () } Catch (ioexception e) {}}

/ ** * filepath --local file path * / public boolean sendfile (string filepath) {boolean result = true; // success? If (this.Aftp! = Null) {this.label1.Settext ("Paste files, Wait patiently ... "); string contentperLine; a =" Paste success! "; / * 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 (); ftpclose (); ftpclient.closserver ();} catch (ooException e) {a = "Paste failure!"; Result = false;} * / this.label1 .Settext (a); this.showFileContents ();} else {result = false;} returnrate;} / ** * SHOW All the Conten of the File List * / Public Void ShowFileContents () {// The air is spaced! StringBuffer buf = new stringbuffer (); this.edit4.Settext (""); try {this.ins = THIS . atp.list (); // return the telnetinputstream while ((this.ch = this.ins.read ())> 0) {buf.append ((char) ch); // Empty allocate space} this. Edit4.Settext (buf.tostring ()); this.ins.close (); system.out.println ("ShowFileContents INS CLOSED");} catch (ioException e) {}}

/ ** * Enter the directory * / public void Enterdiectory (String path) {if (path.length ()> 0) {this.Aftp.cd (path); file: // enter in the directory this.ShowFileContents (); If (".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 = "Current Directory: " this.currentpath; system.out.println (" this.currentpath: this.currentpath);}} catch (ioException e) {this.a = "Unable to enter the directory: /" 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 (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.closserver ();} catch (} catch (ooException 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 ...");}}} Limited, hope to teach.

转载请注明原文地址:https://www.9cbs.com/read-26385.html

New Post(0)