Pages Navigation Menu

Coding is much easier than you think

Java code to preallocate space for destination mainframe file using FTP

Java code to preallocate space for destination mainframe file using FTP

 
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);
                        }
                }
        }
}

 

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.