Import Org.apache.commons.io.IOUtils;
Import org.apache.commons.net.ftp.FTPClient;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.FileOutputStream; /** * JAVA ftp upload Download Helper class * * Commons-net-1.4.1.jar pftclinet JAR Package * * @author: Jenminzhang * * public class Filemana
gerbyftp {/** * FTP upload Single file test/public static void Fileuploadbyftp () {ftpclient ftpclient = new FtpClient ();
FileInputStream FIS = null;
try {ftpclient.connect ("192.85.1.9");
Ftpclient.login ("Zhangzhenmin", "62672000");
File Srcfile = new file ("E:\\test_back_081409.sql");
FIS = new FileInputStream (srcfile);
Set up the upload directory ftpclient.changeworkingdirectory ("/home/zhangzhenmin");
Ftpclient.setbuffersize (1024);
Ftpclient.setcontrolencoding ("GBK");
Set file type (binary) ftpclient.setfiletype (ftpclient.binary_file_type);
Ftpclient.storefile ("Test_back_081901.sql", FIS);
catch (IOException e) {e.printstacktrace (); throw new RuntiMeexception ("FTP client error.)
", e);
finally {ioutils.closequietly (FIS);
try {ftpclient.disconnect ();
catch (IOException e) {e.printstacktrace (); throw new RuntimeException ("Shutdown FTP connection) exception occurred."
", e);
/** * FTP Downloads a single file test/public static void Filedownloadbyftp () {ftpclient ftpclient = new FtpClient ();
FileOutputStream fos = null;
try {ftpclient.connect ("192.85.1.9");
Ftpclient.login ("Zhangzhenmin", "62672000");
String remotefilename = "/home/zhangzhenmin/test_back_081901.sql";
FOS = new FileOutputStream ("E:/test/test_back_081901.sql");
FOS = new FileOutputStream ("H:/test/test_back_081901.sql");
Ftpclient.setbuffersize (1024);
Set file type (binary) ftpclient.setfiletype (ftpclient.binary_file_type);
Ftpclient.retrievefile (Remotefilename, FOS);
catch (IOException e) {e.printstacktrace (); throw new RuntimeException ("FTP client error.
", e);
Finally {ioutils.closequietly (FOS);
try {ftpclient.disconnect (); }catch (IOException e) {e.printstacktrace (); throw new RuntimeException ("Shutdown FTP connection) exception occurred."
", e);
}} public static void Main (string[] args) {fileuploadbyftp ();
Filedownloadbyftp ();
}
}