Pages Navigation Menu

Coding is much easier than you think

Java code to preallocate space for destination mainframe file using FTP

Posted by in FTP, FTP Tutorial

 
In my project I have faced a scenario such that , I need to upload a csv file from my java application to a mainframe system, I have Java code that uses the Apache commons net Ftp Client library to send a file to a mainframe. The code works perfectly for small sized files under 5 Mb. However, when it encounters a file over 5 Mb, it fails with a CopyStreamException.
 
** UPDATE: FTP Complete tutorial now available here.
 
Then I have gotten assistance from a mainframe expert, the mainframe person informed me that the format parameters and block-size of the file created on the mainframe were incorrect.
 
And so with the help of the mainframe expert , I have included the following codes in my ftp program,
 

ftpclient.sendSiteCommand("RECFM=FB");
ftpclient.sendSiteCommand("LRECL=2000");
ftpclient.sendSiteCommand("BLKSIZE=27000");
ftpclient.sendSiteCommand("CY");
ftpclient.sendSiteCommand("PRI= 50");
ftpclient.sendSiteCommand("SEC=25");

 
where
RECFM=FB -> record format , which implies the file format
LRECL=2000 -> Logical record length , which decides the length of a record
BLKSIZE=27000 -> Block size command use to set the block size of the content transferred
CY -> Allocated space in cylinders, and PRI= 50 is the primary allocation and SEC=25 is the secondary allocation , and RLSE is the release command.

Now my ftp program could able to transfer files over 5 MB to the main frame system.

 
File : FtpFileUpload.java
 


import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FTPFileUpload
{
        public static void main(String[] args) throws IOException
        {
                FTPClient ftpclient = new FTPClient();
                FileInputStream fis = null;
                boolean result;
                String ftpServerAddress = "localhost";
                String userName = "admin";
                String password = "admin";

                try
                {
                    ftpclient.connect(ftpServerAddress);
                    result = ftpclient.login(userName, password);

                    if (result == true)
                    {
                    System.out.println("Logged in Successfully !");
                    }
                    else
                    {
                    System.out.println("Login Fail!");
                    return;
                    }
                ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
                // preallocate space in mainframe server using FTP
                        ftpclient.sendSiteCommand("RECFM=FB");
                        ftpclient.sendSiteCommand("LRECL=2000");
                        ftpclient.sendSiteCommand("BLKSIZE=27000");
                        ftpclient.sendSiteCommand("CY");
                        ftpclient.sendSiteCommand("PRI= 50");
                        ftpclient.sendSiteCommand("SEC=25");
                        ftpclient.changeWorkingDirectory("/");

                        File file = new File("D:/File.csv");
                        String testName = "PUBLIC.FILENAME.FILE1";
                        fis = new FileInputStream(file);

                        // Upload file to the ftp server
                        result = ftpclient.storeFile(testName, fis);

                        if (result == true) {
                        System.out.println("File is uploaded successfully");
                        }
                        else {
                        System.out.println("File uploading failed");
                        }
                        ftpclient.logout();
                } catch (FTPConnectionClosedException e) {
                        System.out.println(e);
                } finally {
                        try {
                                ftpclient.disconnect();
                        } catch (FTPConnectionClosedException e) {
                                System.out.println(e);
                        }
                }
        }
}

 

Read More

Getting Positive Completion reply from FTP Server using Java

Posted by in FTP, FTP Tutorial

 

package com.simplecode.com;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

public class FtpLogin {
	public static void main(String[] args) throws IOException {
		FTPClient ftpClient = new FTPClient();
		String errorMessage = null;
		boolean result;
		try {
			// Connect to the localhost
			ftpClient.connect("localhost");
			int reply = ftpClient.getReplyCode();

	        if (!FTPReply.isPositiveCompletion(reply)) {
	            errorMessage = "FTP server refused connection: " + ftpClient.getReplyString();
	        }
	        else
	        {
			// login to ftp server
			result = ftpClient.login("admin", "admin");
			if (result == true) {
				System.out.println("Successfully logged in!");
			} else {
				System.out.println("Login Fail!");
				return;
			}
		}
		}catch (FTPConnectionClosedException e) {
			e.printStackTrace();
		} finally {
			try {
				ftpClient.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.out.println(e);
			}
		}

	}
}

 

The function ftpClient.getReplyCode(); is used to get reply code on sending ftp connection, and the command FTPReply.isPositiveCompletion(reply) is used to check weather the reply code is positive or not.
Read More

Listing System Help Using FTP

Posted by in FTP Tutorial

 

String listHelp() :This method retrieves the list of help information provided by the server and it returns the content in String. If there is no help information from the server then it will return null.

public String listHelp(String command) : Retrieves the help info from the ftp server for the specified command. Its return value in String.It returns null if there is no information.

command represents what you ask for help.

These methods throw FTPConnectionClosedException if server is closed before completion of operation and IOException, if there is any I/O error.

Example :

This example print the list of all the help info provided by the ftp server. We can list it by using FTPClient class method listHelp().

File : FtpHelpList.java

 

package com.simplecode.net;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpHelpList {
	public static void main(String[] args) throws IOException {
		FTPClient ftpClient = new FTPClient();
		boolean result;
		try {
			// Connect to the localhost
			ftpClient.connect("localhost");

			// login to ftp server
			result = ftpClient.login("admin", "admin");

			if (result == true) {
				System.out.println("User logged in successfully !");
			} else {
				System.out.println("Login failed !");
				return;
			}

			// Get List of system help

			System.out.println("System Help Information.... ");
			System.out.println(ftpClient.listHelp());

		} catch (FTPConnectionClosedException e) {
			System.err.println(e);
		} finally {
			try {
				ftpClient.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.err.println(e);
			}
		}
	}
}

 

Read More

Get Buffer size on FTP Server

Posted by in FTP Tutorial

 

You can find out the current internal buffer size for your data socket by using the FTPClient class method.

int getBufferSize() : This method returns int value. It fetches the present internal buffer size for your data sockets.

Example :

Here is an example to print the internal buffer size for our data socket.For that we are calling method client.getBufferSize().

File : FtpGetBufferSize.java
 

package com.simplecode.net;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpGetBufferSize {
	public static void main(String[] args) throws IOException {
		FTPClient client = new FTPClient();
		boolean result;
		try {
			// Connect to the localhost
			client.connect("localhost");

			// login to ftp server
			result = client.login("admin", "admin");

			if (result == true) {
				System.out.println("User logged in successfully !");
			} else {
				System.out.println("Login failed!");
				return;
			}

			// Get buffer size
			System.out.println("Current Buffer size : "
					+ client.getBufferSize());

		} catch (FTPConnectionClosedException e) {
			System.err.println(e);
		} finally {
			try {
				client.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.err.println(e);
			}
		}
	}
}
Read More

Change working directory on FTP Server

Posted by in FTP Tutorial

 

FTPClient class provides method to replace the parent directory.

boolean changeToParentDirectory() : Working of this method is to replace the parent directory of the present working directory. It is of boolean type. Returns true if method completed properly otherwise returns false.

It throws FTPConnectionClosedException and IOException.
 
** UPDATE: FTP Complete tutorial now available here.
 
Example :

This example contains code to change the current working directory using client.changeWorkingDirectory(newDir) and change the parent directory using method client.changeToParentDirectory().

Here is program to demonstrate  –

 
File : FtpChangeParentDir.java
 


package com.simplecode.net;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpChangeParentDir {
public static void main(String[] args) throws IOException {

FTPClient ftpClient = new FTPClient();
boolean result;
try {
	// Connect to the localhost
	ftpClient.connect("localhost");

	// login to ftp server
	result = ftpClient.login("admin", "admin");
	if (result == true) {
		System.out.println("User logged in successfully !");
	} else {
		System.out.println("Login Fail !");
		return;
	}
	String newDir = "/newDirectory";

	// Changing working directory
	result = ftpClient.changeWorkingDirectory(newDir);

	if (result == true) {
   System.out.println("Working directory is changed.Your New working directory:"+newDir);
	} else {
		System.out.println("Unable to change");
	}
	result = ftpClient.changeToParentDirectory();
	if (result == true) {
		System.out.println("Parent directory is changed");
	} else {
		System.out.println("Unable to change Parent directory");
		}
	} catch (FTPConnectionClosedException e) {
		System.err.println(e);
	} finally {
		try {
			ftpClient.disconnect();
		} catch (FTPConnectionClosedException e) {
			System.err.println(e);
		}
	}
}
}

 

Read More

Get all file size on FTP Server

Posted by in FTP Tutorial

 

FTPClient class does not provide any direct method to find the size of file. So we will use another way to list the file size. FTPFile class provide you method getSize() to get the size of the current file.

FTPFile class handles files stored on FTP server. It shows you different information about the files.

long getSize() : This method returns size of file in bytes. Its return type is long.

Example :

In this example, we are listing all files and their size. For that first list all files and store it in FTPFile array. Now iterate and check if it is file by usinf method isFile() then get its name using method getName()(as file.getName()) and get its size using method getSize() (as file.getSize()).

Here is complete source code –
 
** UPDATE: FTP Complete tutorial now available here.
 
FtpFileSize.java
 

package com.simplecode.net;

import java.io.IOException;

import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPConnectionClosedException;

class FtpFileSize {
	public static void main(String[] arg) throws IOException {
		FTPClient ftpClient = new FTPClient();
		boolean result;
		try {

			// Connect to the localhost
			ftpClient.connect("localhost");

			// login to ftp server
			result = ftpClient.login("admin", "admin");

			if (result == true) {
				System.out.println("User logged in successfully !");
			} else {
				System.out.println("Login failed !");
				return;
			}

			FTPFile[] files = ftpClient.listFiles();
			System.out.println("Files and their size on Ftp Server : ");
			for (FTPFile file : files) {
			if (file.isFile()) {
			     System.out.print("File name : " + file.getName());
			     System.out.print("t size : " + file.getSize() + " Bytes");
			     System.out.println();
			}
			}

		} catch (FTPConnectionClosedException e) {
			System.err.println(e);
		} finally {
			try {
				ftpClient.disconnect();
			} catch (FTPConnectionClosedException e) {
				System.err.println(e);
			}
		}
	}
}

 

Read More
Page 1 of 41234