FTP upload Download

Source: Internet
Author: User

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

Import org.apache.commons.net.ftp.FTPClient;
Import Org.apache.commons.net.ftp.FTPFile;
Import org.apache.commons.net.ftp.FTPReply;
Import Org.apache.log4j.Logger;

Import Com.wrt.zjg.webservices.model.FtpModel;

public class Ftputil {

private static Logger Logger = Logger.getlogger (Ftputil.class);

private static ftpclient ftp;

/**
* Get FTP connection
* @param FM
* @return
* @throws Exception
*/
public static Boolean CONNECTFTP (Ftpmodel FM) throws exception{
FTP = new FtpClient ();
Boolean flag = false;
int reply;
if (fm.getport () = = null) {
Ftp.connect (Fm.getipaddr (), 21);
} else {
Ftp.connect (Fm.getipaddr (), Fm.getport ());
}
Ftp.login (Fm.getusername (), fm.getpwd ());
Ftp.setfiletype (Ftpclient.binary_file_type);
Reply = Ftp.getreplycode ();
if (! Ftpreply.ispositivecompletion (Reply)) {
Ftp.disconnect ();
return flag;
}
Ftp.changeworkingdirectory (Fm.getpath ());
Flag = true;
return flag;
}

/**
* Close FTP connection
*/
public static void Closeftp () {
if (FTP! = null && ftp.isconnected ()) {
try {
Ftp.logout ();
Ftp.disconnect ();
} catch (IOException e) {
E.printstacktrace ();
}
}
}

/**
* FTP upload file
* @param file
* @throws Exception
*/
public static void upload (file file) throws Exception {
if (File.isdirectory ()) {
Ftp.makedirectory (File.getname ());
Ftp.changeworkingdirectory (File.getname ());
string[] files = file.list ();
for (String fstr:files) {
File File1 = new file (File.getpath () + "/" + fstr);
if (File1.isdirectory ()) {
Upload (file1);
Ftp.changetoparentdirectory ();
} else {
File File2 = new file (File.getpath () + "/" + fstr);
FileInputStream input = new FileInputStream (file2);
Ftp.storefile (File2.getname (), input);
Input.close ();
}
}
} else {
File file2= new file (File.getpath ());
FileInputStream input = new FileInputStream (file2);
Ftp.storefile (File2.getname (), input);
Input.close ();
}
}

/**
*
* @param FM will upload the file
* @param localbasedir Local Directory
* @param remotebasedir Remote Directory
* @throws Exception
*/
public static void Startdown (Ftpmodel FM, String Localbasedir, String remotebasedir) throws Exception {
if (FTPUTIL.CONNECTFTP (FM)) {
try {
ftpfile[] files = null;
Boolean changedir = Ftp.changeworkingdirectory (Remotebasedir);
if (Changedir) {
Ftp.setcontrolencoding ("GBK");
Files = Ftp.listfiles ();
for (int i = 0; i < files.length; i++) {
try {
DownloadFile (Files[i], Localbasedir, Remotebasedir);
} catch (Exception e) {
Logger.error (e);
Logger.error ("<" +files[i].getname () + "> Download Failed");
}
}
}

} catch (Exception e) {
Logger.error (e);
Logger.error ("An exception occurred during the download process! ");
}
} else {
Logger.error ("Link failed! ");
}
}

/**
* Download FTP file
* When you need to download the ftp file, call this method
* Download based on <b> obtained file name, local address, remote address </b>
* @param ftpfile
* @param Relativelocalpath
* @param Relativeremotepath
*/
public static void DownloadFile (Ftpfile ftpfile, String Relativelocalpath, String relativeremotepath) {
if (Ftpfile.isfile ()) {
if (Ftpfile.getname (). IndexOf ("?") = =-1) {
OutputStream outputstream = null;
try {
File Locafile = new file (Relativelocalpath + ftpfile.getname ());
if (locafile.exists ()) {
Return
} else {
OutputStream = new FileOutputStream (Relativelocalpath + ftpfile.getname ());
Ftp.retrievefile (Ftpfile.getname (), outputstream);
Outputstream.flush ();
Outputstream.close ();
}
} catch (Exception e) {
Logger.error (e);
} finally {
try {
if (outputstream! = null) {
Outputstream.close ();
}
} catch (IOException e) {
Logger.error ("Output file stream exception");
}
}
}
} else {
String Newlocalrelatepath = Relativelocalpath + ftpfile.getname ();
String newremote = new String (Relativelocalpath + ftpfile.getname (). toString ());
File File = new file (Newlocalrelatepath);
if (!file.exists ()) {
File.mkdirs ();
}
try {
Newlocalrelatepath = Newlocalrelatepath + "/";
Newremote = Newremote + "/";
String Currentworkdir = Ftpfile.getname (). toString ();
Boolean changedir = Ftp.changeworkingdirectory (Currentworkdir);
if (Changedir) {
ftpfile[] files = null;
Files = Ftp.listfiles ();
for (int i = 0; i < files.length; i++) {
DownloadFile (Files[i], Newlocalrelatepath, newremote);
}
}
if (Changedir) {
Ftp.changetoparentdirectory ();
}
} catch (Exception e) {
Logger.error (e);
}
}
}

public static void Main (string[] args) throws Exception {
TODO auto-generated Method Stub
Ftpmodel fm = new Ftpmodel ();
FM.SETIPADDR ("192.168.1.252");
Fm.setusername ("ftp");
Fm.setpwd ("ftp12345");
Fm.setpath ("/temp/zjg/messagexml/");
FTPUTIL.CONNECTFTP (FM);
File File = new file ("E:/test/959663081-20170606154839294.xml");
Ftputil.upload (file);

}

}

FTP upload Download

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.