Ftpjar Package Download: http://download.csdn.net/detail/lgq_0714/4216449
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.OutputStream;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPClientConfig;
Import Org.apache.commons.net.ftp.FTPFile;
Import org.apache.commons.net.ftp.FTPReply;
Import Sun.net.TelnetOutputStream;
Import sun.net.ftp.FtpClient; /** * @author Administrator * @version 1.0 * @date 2012-3-12 * @class_displayName ftputils/public class Ftputils
{/** * Description: Uploading files to FTP server via Sun FtpClient upload (in order to solve the Chinese file garbled problem) * @param URL FTP server IP address * @param port FTP server ports * @param username FTP login account * @param password FTP login password * @param path FTP server save directory * @param originalfilename original filename * @param ftpfilename uploaded to the FTP server the filename * @return successfully returns TRUE, otherwise returns false */public static Boolean UploadFile (String server, in T port, string user, string password, string path, String originalfilename, StringFtpfilename) {Boolean success = false;
FtpClient ftpclient;
try {ftpclient = new ftpclient ();
Ftpclient.openserver (Server,port);
Ftpclient.login (user, password);
System.out.println ("Login ...");
Path is a subdirectory of the home directory under the FTP service if (path.length ()!= 0) ftpclient.cd (path);
Use 2 to upload, prevent uploading Chinese files garbled ftpclient.binary ();
Telnetoutputstream OS = null;
FileInputStream is = null;
OS = Ftpclient.put (ftpfilename);
File file_in = new file (originalfilename);
if (file_in.length () = = 0) {throw new Exception ("Upload file is empty!");
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 ();
Success = true;
catch (Exception e) {e.printstacktrace ();
return success; /** * Description: Downloading files from an FTP server via Apache ftpclient implementing file Downloads * * @param IP FTP server IP address * @param port FTP server ports, default Think: * @param username FTPLogin account * @param password FTP login password * @param remotepath FTP Server relative path * @param filename to download * @param localpath Download Save to the local path * @return */public static Boolean downloadFile (string ip, int port, string username, string password, stri
Ng RemotePath, String fileName, String LocalPath) {Boolean success = false;
ftpclient ftp = new FtpClient ();
try {int reply;
Ftp.connect (IP, port);
The following three lines of code must be, and can not change the encoding format, otherwise can not correctly download the Chinese file ftp.setcontrolencoding ("GBK");
Ftpclientconfig conf = new Ftpclientconfig (FTPCLIENTCONFIG.SYST_NT);
Conf.setserverlanguagecode ("en");
If you use the default port, you can use the Ftp.connect (URL) way to connect directly to the FTP server Ftp.login (username, password);/Login reply = Ftp.getreplycode (); if (!
Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
return success;
} ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles ();
for (int i = 0; i < fs.length i++) {ftpfile ff = fs[i]; if (!ff.getname (). Equals (".") &&!ff.getname (). Equals (".. ")
{File LocalFile = new file (LocalPath + file.separator + ff.getname ());
if (!localfile.exists ())//If the file already exists, do not repeat the download {OutputStream is = new FileOutputStream (localfile); Note that the first parameter of the retrievefile is converted from GBK to iso-8859-1 encoding.
Otherwise, the contents of the downloaded file are empty. This may be due to the default encoding of the AIX system to iso-8859-1 Ftp.retrievefile (new String (Ff.getname (). GetBytes ("GBK"), "iso-8859-1"), is)
;
Is.close ();
}} SimpleDateFormat DateFormat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); System.out.println ("------------------End:" +dateformat.format (New Date ()) + "-----------------------------------"
);
Ftp.logout ();
Success = true;
catch (Exception e) {e.printstacktrace ();
finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
catch (Exception IoE) {ioe.printstacktrace ();
}} return success; public static void Main (string[] args) {try {downloadFile ("192.168.103.215 ",", "Servu", "111111", "D:/ftpserv", "Test. xls", "c:/primeton2012/jakarta-tomcat-5.0.28/upload/feedback_i
NFO ");
SERVER:FTP Server IP address String server = "192.168.103.215";
User: Log on to the FTP server with the username String user = "Servu";
Password: password String password = "111111" for the username on the FTP server;
PATH:FTP on the server path String path = "D:/ftpserv";
To upload a local file path String originalfilename = "f://web//web//src//test. xls";
Upload server filename String ftpfilename = "Test 2012-3-12-Arbor Day-.xls";
UploadFile (server, user, password, path, originalfilename, ftpfilename);
catch (Exception e) {e.printstacktrace ();
}
}
}