How to implement FTP function in the Java application

zhaozj2021-02-16  52

How to implement FTP function in the Java application

About Sun.Net.ftp.ftpclient class is taken from http://netspring.myrice.com/program/java/technic/022.htm

In Java's programming, you may experience FTP programming, this article demonstrates how to implement it. The following is the JAVA source program of these three parts.

1) Display files on the FTP server

Void ftplist_actionperperformed (ActionEvent E) {string server = serveredit.getText (); // Input FTP server IP address string user = user gerEtText (); file: // Log in to the FTP server username String password = passwordedit.getText (); // Log in to the username of the FTP server String path = pathEdit.getText (); // The path on the FTP server Try {ftpclient ftpclient = new ftpclient (); // Create an FTPClient object ftpclient.openserver (Server) ; // Connect the FTP server ftpclient.login (user, password); // log in to the FTP server if (Path.Length ()! = 0) ftpclient.cd (path); telnetInputStream is = ftpclient.list (); int C; While ((c = is.read ())! = - 1) {system.out.print ((char) c);} is.close (); ftpclient.closeServer (); // Exit FTP server} catch IOException ex) {;}}

2) Upload a file from the FTP server

void getButton_actionPerformed (ActionEvent e) {String server = serverEdit.getText (); String user = userEdit.getText (); String password = passwordEdit.getText (); String path = pathEdit.getText (); String filename = filenameEdit.getText ( ); Try {ftpclient ftpclient = new ftpclient (); ftpclient.openserver (server); ftpclient.login (user, password); if (path.length ()! = 0) ftpclient.cd (path); 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.closserver ();} catch (ooException ex) {; }} 3) uploading a file to the FTP server void putButton_actionPerformed (ActionEvent e) {String server = serverEdit.getText (); String user = userEdit.getText (); String password = passwordEdit.getText (); String path = pathEdit. Gett EXT (); string filename = filenameEdit.gettext (); try {ftpclient ftpclient = new ftpclient (); ftpclient.openserver (server); ftpclient.login (user, password); if (path.length ()! = 0) ftpClient.cd (path); ftpClient.binary (); TelnetOutputStream os = ftpClient.put (filename); File file_in = new File (filename); FileInputStream is = new FileInputStream (file_in); 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 (ioException ex) {;}}}

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

New Post(0)