Java Connection FTP Server instance code _java

Source: Internet
Author: User

No more nonsense to say, directly to everyone to paste the Java code.

 Import java.io.IOException;
Import Sun.net.TelnetInputStream;
Import sun.net.ftp.FtpClient;
public class Myftp {
static ftpclient myftp;
static String hostname;
static String username;
static String password;
/**
* @author cutelion 20051108 14:27
* @param args
/public static void Main (string[) args) {
T ry {
hostname = "203.171.236.123";
Myftp = new FtpClient (hostname);
Myftp.login ("User", "pass");
Myftp.binary ();
Showfilecontents ();
} catch (IOException E1) {
System.out.print (e1);
}
}
public static void Showfilecontents () {
int ch;
StringBuffer buf = new StringBuffer ();
try {
Telnetinputstream instream = Myftp.list ();
while (ch = instream.read ()) >= 0) {
buf.append ((char) ch);
System.out.print (New String (Buf.tostring (). GetBytes ("Iso-8859-1"),
"GBK"));
Instream.close ();
Myftp.closeserver ();
} catch (Exception e) {
System.out.println ("Error" + e);
}}} [@more @]

The following are some of the control commands provided by the FtpClient class.

public void CD (String remotedirectory)

This command switches the directory on the remote system to the directory specified by the parameter remotedirectory.
public void Cdup (): This command is used to switch directories on the remote system to the previous directory.
Public String pwd (): This command displays the status of the directory on the remote system.
public void Binary (): This command sets the transport format to binary format.
public void ASCII (): This command sets the transport protocol to an ASCII format.
public void rename (String string,string string1)

This command renames a directory or file on a remote system.
In addition to the above methods, class FtpClient also provides several methods that can be used to pass and retrieve catalog listings and files. These methods return input and output streams that are available for reading or writing. Here are some of the main methods.

Public Telnetinputstream list ()

Returns the input stream corresponding to the current directory on the remote machine.

Public Telnetinputstream get (String filename)

Gets the file filename on the remote machine, using Telnetinputstream to transfer the file to the local.

Public Telnetoutputstream put (String filename)

Opens an output stream by writing to transmit file filename to the remote computer through this output stream.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.