Search code examples
mainframeftp-clientapache-commons-net

Apache commons net FTPClient storeFile() creating file with the username prefixed


Happy New Year! :)

So, I'm using the Apache Commons FTPClient for the first time to FTP a file to the Mainframes FTP server. I have the code below to setup required configurations and eventually FTP the file using storeFile() which seems to work except one small problem below.

            ftpClient.login(username, password);
            System.out.println(ftpClient.getReplyString());

            File blankFile = new File("./src/main/resources/BLANK.DAT");
            System.out.println("File exists: "+ blankFile.exists());
            InputStream inputStream = new FileInputStream(blankFile);

            ftpClient.sendSiteCommand("sbdataconn=****.****.*****"); --Hidden from post
            System.out.println(ftpClient.getReplyString());

            ftpClient.sendCommand(FTPCmd.PWD);
            System.out.println(ftpClient.getReplyString());

            ftpClient.sendSiteCommand("lrecl=80 blksize=3120 recfm=FB");
            System.out.println(ftpClient.getReplyString());

            ftpClient.sendSiteCommand("pri=5 sec=15");
            System.out.println(ftpClient.getReplyString());

            ftpClient.storeFile("Destination.File.Name",inputStream);
            System.out.println(ftpClient.getReplyString());

The corresponding console output logs say:

Connected to ****ftp.****.com.
220-FTPD1 IBM FTP CS V2R3 at *****, 06:46:21 on 2021-01-05.
220 Connection will close if idle for more than 15 minutes.
230 USERNAME is logged on.  Working directory is "USERNAME.".

File exists: true
200 SITE command was accepted

257 "'USERNAME.'" is working directory.

200 SITE command was accepted

200 SITE command was accepted

250 Transfer completed successfully.

The Mainframes team confirms that they are seeing the file but the file is named as 'USERNAME.Destination.File.Name', but we need the file to be named as 'Destination.File.Name' to kick off the next step in processing. Am I missing something in my configs? Or is this an expected behaviour when FTPing to Mainframes using Apache Commons. How do I go further from here? Any help is appreciated. Thanks!


Solution

  • Well, it appears the filename in the storeFile() method of FTPClient had to be enclosed within single quotes('') to have the file created with exactly that name. So, the solve was to simply replace

    ftpClient.storeFile("Destination.File.Name",inputStream);

    with

    ftpClient.storeFile("'Destination.File.Name'",inputStream);

    and it worked as expected.

    Thanks and have a wonderful 2021.