FTP file upload download and create multilevel catalog __ Tools Class

Source: Internet
Author: User
Tags create directory ftp file ftp login ftp upload file server port

FTP file upload download and create multilevel directory

	 
	/** * FTP Upload file * * @param URL * FTP server hostname * @param port * FTP server ports * @param u Sername * FTP login account * @param password * FTP login password * @param path * FTP Server save directory * @p  Aram filename * Upload file name on FTP server * @param input * Enter stream * @return successfully returns TRUE, otherwise returns false * @author Author Wanghaixing * @date Date Created: June 13, 2017 pm 4:01:51 * @version 1.0/public static Boolean uploadfile (String URL, in 
		T port, string Username, string password, string path, string filename, InputStream input) {Boolean success = false;
		ftpclient ftp = new FtpClient ();
			try {int reply; 
			Ftp.connect (URL, port);//Connect FTP server//If the default port is used, you can connect the FTP server directly using the Ftp.connect (URL) ftp.login (username, password);/Login
			Ftp.setfiletype (Ftpclient.binary_file_type);
			Reply = Ftp.getreplycode (); if (!
				Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
			return success; } ftp.changeworkingdirectory (PATH);
			Ftp.storefile (filename, input);

			Ftp.appendfile (filename, input);
			Input.close ();
			Ftp.logout ();
		Success = true;
		catch (IOException e) {e.printstacktrace ();
				finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
	catch (IOException IoE) {}} return success; /** * Download files from FTP * @param URL * FTP server hostname * @param port * FTP server ports * @param use
	 Rname * FTP login account * @param password * FTP login password * @param remotepath * Relative path on FTP server  * @param filename * file name to download * @param localpath * Save to Local path after download * @return/public static Boolean downfile (string url, int port, string username, string password, string remotepath, String fileName, String lo
		Calpath) {Boolean success = false;
		Boolean isfileexits = false;
		ftpclient ftp = new FtpClient ();
			try {int reply;
			Ftp.connect (URL, port); If you are using the default port, you can use Ftp.connect (urL) the way to directly connect the FTP server Ftp.login (username, password);//Login Ftp.setfiletype (Ftpclient.binary_file_type);
			Reply = Ftp.getreplycode (); if (!
				Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
			return success;
			} ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles ();
					for (Ftpfile ff:fs) {if (Ff.getname (). Equals (FileName)) {isfileexits=true;
					File LocalFile = new file (LocalPath + "/" + ff.getname ());
					OutputStream is = new FileOutputStream (localfile);
					Ftp.retrievefile (Ff.getname (), is);
				Is.close (); } if (!isfileexits) {throw new RuntimeException ("The file to be downloaded does not exist.
			");
			} ftp.logout ();
		Success = true;
		catch (IOException e) {e.printstacktrace ();
				finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
	catch (IOException IoE) {}} return success; /** * Query FTP server on the specified path all filename * * @param URL * FTP server hostname * @param port
	 * FTP Server Port * @param username * FTP login account * @param password * FTP login password * @param RemotePath * The relative path on the FTP server * @return * @date Date Created: June 14, 2017 pm 5:06:57/public static list<string& Gt Listftpfiles (string url, int port, string username, string password, string remotepath) {arraylist<string> result
		List = new arraylist<string> ();
		ftpclient ftp = new FtpClient ();
			try {int reply;
			Ftp.connect (URL, port); If you use the default port, you can use the Ftp.connect (URL) method to connect directly to the FTP server Ftp.login (username, password);/Login Ftp.setfiletype (ftpclient.binary_
			File_type);
			Reply = Ftp.getreplycode (); if (!
				Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
			return resultlist;
			} ftp.changeworkingdirectory (RemotePath);//Transfer to FTP server directory ftpfile[] fs = Ftp.listfiles ();
				for (Ftpfile ff:fs) {Resultlist.add (Ff.getname ()); if (Ff.getname (). Equals (FileName)) {//File LocalFile = new file (LocalPath +)/"+ ff.getname ());
				OutputStream is = new FileOutputStream (localfile);
				Ftp.retrievefile (Ff.getname (), is);
				Is.close ();
		} ftp.logout ();
		catch (IOException e) {e.printstacktrace ();
				finally {if (ftp.isconnected ()) {try {ftp.disconnect ();
	catch (IOException IoE) {}} return resultlist;
	 /** * Create a multi-tier directory file, if the FTP server already exists the file, do not create, if none, then create * @param URL * @param port * @param username * @param password * @param remote * @return * @throws IOException * @date Date Created: June 22, 2017 morning 11:51:33/public static Boolean Cre Atedirecroty (string url, int port, string username, string password, string remote) throws IOException {FtpClient F
		TP = new FtpClient ();
		int reply;
		Ftp.connect (URL, port); If you use the default port, you can use the Ftp.connect (URL) method to connect directly to the FTP server Ftp.login (username, password);/Login Ftp.setfiletype (ftpclient.binary_
		File_type);
		Reply = Ftp.getreplycode (); if (! Ftpreply.ispositivecompletion (Reply)) {ftp.disconnect ();
		} ftp.changeworkingdirectory ("/var/www/html");//Transfer to FTP server directory Boolean success = TRUE;
		String directory = remote + "/";
		String directory = remote.substring (0, Remote.lastindexof ("/") + 1); If the remote directory does not exist, recursively creates the remote server directory if (!directory.equalsignorecase ("/") &&!changeworkingdirectory (New String (
			directory) (FTP)) {int start = 0;
			int end = 0;
			if (Directory.startswith ("/")) {start = 1;
			else {start = 0;
			End = Directory.indexof ("/", start);
			String path = "";
			String paths = "";
				while (true) {String subdirectory = new String (remote.substring (Start, end). GetBytes ("GBK"), "iso-8859-1");
				Path = path + "/" + subdirectory; 
					if (!existfile (path, FTP)) {if (Makedirectory (subdirectory, FTP)) {changeworkingdirectory (subdirectory, FTP);
						else {System.out.println ("Create directory [+ subdirectory +] failed");
					Changeworkingdirectory (subdirectory, FTP); } else {changeworkingDirectory (subdirectory, FTP);
				} paths = paths + "/" + subdirectory;
				Start = end + 1;
				End = Directory.indexof ("/", start);
				Check that all directories are created if (end <= start) {break;
	}} return success; /** * Change Directory path * @param directory * @param FTP * @return * @date Date Created: June 22, 2017 morning 11:52:13/public stat
		IC boolean changeworkingdirectory (String directory, ftpclient FTP) {Boolean flag = true;
			try {flag = ftp.changeworkingdirectory (directory); if (flag) {System.out.println ("Enter folder" + Directory +) succeeded.
			"); else {System.out.println ("Enter folder" + Directory +) failed.
			");
		} catch (IOException IoE) {ioe.printstacktrace ();
	return flag; /** * Create directory * @param dir * @param ftp * @return * @date creation time: June 22, 2017 morning 11:52:40 * * public static Boole
		An makedirectory (String dir, ftpclient FTP) {Boolean flag = true;
			try {flag = ftp.makedirectory (dir); if (flag) {System.out.println ("Create folder" +Dir + "succeeded."
			"); } else {System.out.println (Create folder + dir +) failed.
			");
		} catch (Exception e) {e.printstacktrace ();
	return flag; /** * To determine whether the FTP server file exists * @param path * @param ftp * @return * @throws IOException * @date created: June 22, 2017
		Midday 11:52:52/public static Boolean Existfile (String path, ftpclient ftp) throws IOException {Boolean flag = false;
		ftpfile[] Ftpfilearr = ftp.listfiles (path);
		if (Ftpfilearr.length > 0) {flag = true;
	return flag; }


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.