Java FTP client source code __java

Source: Internet
Author: User
Tags ftp client

Wrote a simple FTP client, this client mainly uses the FtpClient class, this class is in the JDK Lib/rt.jar package. Now realize the file upload, folder upload. The rest of the functionality needs to be supplemented.

The code is as follows:

Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;

Import Sun.net.TelnetOutputStream;
Import sun.net.ftp.FtpClient;

public class Ftpupload {

FtpClient ftpclient = new FtpClient ();

String sserver = "";

String suser = "";

String Spassword = "";

/**
* @return Returns the Spassword.
*/
Public String Getspassword () {
return spassword;
}

/**
* @param password
* The Spassword to set.
*/
public void Setspassword (String password) {
Spassword = password;
}

/**
* @return Returns the sserver.
*/
Public String Getsserver () {
return sserver;
}

/**
* @param server
* The sserver to set.
*/
public void Setsserver (String server) {
sserver = server;
}

/**
* @return Returns the suser.
*/
Public String Getsuser () {
return suser;
}

/**
* @param user
* The Suser to set.
*/
public void Setsuser (String user) {
suser = user;
}

private void Setlogininfo (String strServerIP, String struser, String strpasswd) {
Sserver = strServerIP;
Suser = struser;
Spassword = strpasswd;
}

private void Connect () throws IOException {
Ftpclient.openserver (sserver);
Ftpclient.login (Suser, Spassword);
Ftpclient.binary ();
}

private void Uploadfolder (String strfoldname) throws IOException {
File F = new file (strfoldname);
if (!f.exists ()) {
Return
}
File subfile[] = F.listfiles ();
for (int i = 0; i < subfile.length; i++) {
UploadFile (Subfile[i], subfile[i].getname ());
}
}

private void UploadFile (String strsrcfilename, String strobjfilename) throws IOException {
File file_in = new file (strsrcfilename);
UploadFile (file_in, strobjfilename);
}

private void UploadFile (File fscrfile, String strobjfilename) throws IOException {
Telnetoutputstream OS = ftpclient.put (strobjfilename);
FileInputStream is = new FileInputStream (fscrfile);
byte[] bytes = new byte[1024];
int C;
while ((c = is.read (bytes))!=-1) {
Os.write (bytes, 0, c);
}
Is.close ();
Os.close ();
Log ("" "+ Strobjfilename +" "file has been uploaded. /r/n ");
}

private void Closeconnect () throws IOException {
Ftpclient.closeserver ();
}

private void log (Object obj) {
System.out.println (obj);
}

public static void Main (string[] args) throws Exception {

Ftpupload ftp = new Ftpupload ();
ftp.setlogininfo ("203.186.92.88", "Anonymous", "Anonymous");
Ftp.connect ();
ftp.uploadfolder ("c:/ftp/");
ftp.uploadfile ("C:/ftp/log.txt", "ftplog.txt");
Ftp.closeconnect ();
}

}

Related Article

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.