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 ();
}
}