Java upload, download files via FTP, traverse file directory

Source: Internet
Author: User
Tags create directory
Java Upload through FTP, download files, traverse file directory   Collection stars ~   published in  5 years ago   reading  7703  favorites  9  Point praise  2  Reviews & Nbsp;0
Import Java.io.DataInputStream;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.ArrayList;

Import java.util.List;

Import Org.apache.catalina.tribes.util.Logs;
Import Sun.net.TelnetInputStream;
Import Sun.net.TelnetOutputStream;

Import sun.net.ftp.FtpClient;
	public class Downfileforftp {ftpclient ftpclient;
	Private String Server = "IP";
	private int port = 21;
	Private String UserName = "USN";

	Private String UserPassword = "pwd"; 
			/** * Link to Server * * @return/public Boolean open () {if (ftpclient!= null && ftpclient.serverisopen ())
		return true;
			try {ftpclient = new ftpclient ();
			Ftpclient.openserver (server, port);
			Ftpclient.login (UserName, UserPassword);
			Ftpclient.binary ();
		return true;
			catch (Exception e) {e.printstacktrace ();
			FtpClient = null;
		return false;
		} public boolean CD (String dir) {Boolean f = false; try {ftpclient.cd (DIR);
			catch (IOException e) {//logs.error (e.tostring ());
		return F;
	return true;            /** * Upload files to FTP server * * @param localpathandfilename * Local file directory and filename * @param ftpfilename * Uploaded filename * @param ftpdirectory * FTP directory such as:/path1/pathb2/, if directory does not exist back automatically create directory * @throws Exception/Public Boolean upload (String localdirectoryandfilename, String ftpfilename, String ftpdirectory) throws Exception {if (!ope
		N ()) return false;
		FileInputStream is = null;
		Telnetoutputstream OS = null;
			try {char ch = ';
			if (ftpdirectory.length () > 0) ch = ftpdirectory.charat (Ftpdirectory.length ()-1); for (; ch = =/' | | | ch = = ftpdirectory. CharAt (Ftpdirectory.length ()-1)) Ftpdirectory = Ftpdirectory

			. substring (0, Ftpdirectory.length ()-1);
			int slashindex = Ftpdirectory.indexof (47);
			int backslashindex = Ftpdirectory.indexof (92);
			int index = Slashindex;
			String dirall = ftpdirectory;if (Backslashindex!=-1 && (index = = 1 | | index > BACKSLASHINDEX)) index = Backslashindex;
			String directory = "";
					while (index!=-1) {if (Index > 0) {String dir = dirall.substring (0, index);
					Directory = Directory + "/" + dir;
					Ftpclient.sendserver ("XMKD" + directory + "\ r \ n");
				Ftpclient.readserverresponse ();
				} Dirall = dirall.substring (index + 1);
				Slashindex = Dirall.indexof (47);
				Backslashindex = Dirall.indexof (92);
				index = Slashindex;
			if (Backslashindex!=-1 && (index = = 1 | | index > BACKSLASHINDEX)) index = Backslashindex;
			} ftpclient.sendserver ("XMKD" + ftpdirectory + "\ r \ n");

			Ftpclient.readserverresponse ();
			OS = ftpclient.put (ftpdirectory + "/" + ftpfilename);
			File file_in = new file (localdirectoryandfilename);
			is = new FileInputStream (file_in);
			byte bytes[] = new byte[1024];
			int i;
			while ((i = Is.read (bytes))!=-1) os.write (bytes, 0, i); Clean up the rubbish

			return true;
			catch (Exception e) {e.printstacktrace ();
		return false;
			Finally {if (is!= null) is.close ();
		if (OS!= null) os.close (); }/** * Downloading files from the FTP server and returning the download file length * * @param ftpdirectoryandfilename * @param localdirectoryandfilename * @ret Urn * @throws Exception/public long Download (string ftpdirectoryandfilename, String localdirectoryandfilename)
		Throws Exception {long result = 0;
		if (!open ()) return result;
		Telnetinputstream is = null;
		FileOutputStream OS = null;
			try {is = Ftpclient.get (ftpdirectoryandfilename);
			Java.io.File outfile = new Java.io.File (localdirectoryandfilename);
			OS = new FileOutputStream (outfile);
			byte[] bytes = new byte[1024];
			int C;
				while ((c = is.read (bytes))!=-1) {os.write (bytes, 0, c);
			result = result + C;
		The catch (Exception e) {throw e;
			Finally {if (is!= null) is.close ();

		if (OS!= null) os.close ();
	return result; }

	/** * Returns the list of files in the FTP directory * * @param ftpdirectory * @return/Public list<string> getfilenamelist (String F
		Tpdirectory) {list<string> List = new arraylist<string> ();
		if (!open ()) return list;
			try {datainputstream dis = new DataInputStream (ftpclient.namelist (ftpdirectory));
			String filename = "";
				while (filename = Dis.readline ())!= null) {list.add (filename);
			SYSTEM.OUT.PRINTLN (filename);
		} catch (Exception e) {e.printstacktrace ();
	} return list; /** * Delete File on FTP * * @param ftpdirandfilename/public boolean deletefile (String ftpdirandfilename) {if (
		!open ()) return false;
		Ftpclient.sendserver ("DELE" + ftpdirandfilename + "\ r \ n");
	return true; /** * Delete FTP directory * * @param ftpdirectory * * Public boolean deletedirectory (String ftpdirectory) {if (!open ()
		) return false;
		Ftpclient.sendserver ("Xrmd" + ftpdirectory + "\ r \ n");
	return true; /** * Close Link * * Public voID close () {try {if (ftpclient!= null && ftpclient.serverisopen ()) ftpclient.closeserver (); catch (Exception e) {}} public static void Main (String []args) throws exception{downfileforftp df = new Down
		Fileforftp ();
		Df.getfilenamelist ("E:\\uploadfiles\\cat"); Df.download ("E:\\uploadfiles\\cat\\2012-03-29.11.00.00.012.xml", "f:\\temp\\ftpdown\\2012-03-29.11.00.00.012.")

	XML "); }
	
}

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.