package helloFTP;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;



public class helloFTP {

	public static void main(String[] args) {

		FTPClient client = new FTPClient();
		FileInputStream fis = null;
		String lb = "<<IPAddress>>";
		try{
			client.disconnect();
			client.connect(lb,21);
		
		boolean login = client.login("<FTPUserName>","<FTPPassword>");
		if(login){restremo
			
			
			System.out.println("login successful!");
			
		
			System.out.println("client connection mode:"+client.getDataConnectionMode());
			System.out.println("passive port:"+client.getPassivePort());
			System.out.println("passive host:"+client.getPassiveHost());
			System.out.println("default port:"+client.getDefaultPort());
			System.out.println("connection time out:"+client.getConnectTimeout());
			client.enterLocalActiveMode();			
			System.out.println("entered active mode");
			
			String filename = "c:\\Folder\\test.txt";
			PrintWriter out = new PrintWriter(filename);
			String timenow = new SimpleDateFormat("dd-MM-yyyy").format(new Date());
			out.println(timenow);
			out.close();
			fis = new FileInputStream(filename);
			client.setFileType(FTP.BINARY_FILE_TYPE,FTP.BINARY_FILE_TYPE);
			client.setFileTransferMode(FTP.BINARY_FILE_TYPE);
			client.changeWorkingDirectory("/test");
			if(client.storeFile("test.txt", fis))
			{
				System.out.println("file uploaded successfully");
			}
			else
				client.logout();
			;
			
			
			boolean logout = client.logout();
			if(logout)
			{
				System.out.println("log out from FTP Server");
				
			}
		}else
			System.out.println("log in failed");
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}finally {
			try{
				client.disconnect();
				
			}catch (IOException e){
				e.printStackTrace();
			}
		}
		}
	}


 

About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.

THERE IS NO WAY BACK!!!

Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

Today13
Yesterday83
This week637
This month1933
Total416293

Visitor Info

  • IP: 54.221.73.186
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online

1
Online

2017-12-17

Login