Skip to content
Advertisement

FTP using Apache commons not working – Error: 550 Dataset not found (FTPid that i am using is getting appended to filename)

I am trying to ftp the file from Linux VM to an AS400 server. I was able to login to server in passive mode but when trying to use the STOR command to upload the file getting below error:

STOR XX.YY600040.XXXZZZXXX        
**550 Dataset not found, DSN=FTPID.XX.YY600040.XXXZZZXXX**

Not sure why the ftpid that i am using is getting prefixed to the filename. Is there any way to avoid it? Below is the sample code that i am using:

private static String sendFTPFile(String fileName) throws Exception {
        StringBuffer ftpMessage = new StringBuffer();
        if (SHOW_DEBUG) ftpMessage.append("<ul>");
        FTPClient ftp = null;
        
        try {
            String server = "****";
            String username = "****";
            String password = "XXXXX";
            String hostDir = "";
            String localFileName = fileName;
            String localFilePath = "***/**/*";
            
            boolean binaryTransfer = false, error = false;
            
            FTPClient ftp = new FTPClient();
            ftp.addProtocolCommandListener(new PrintCommandListener(
                                               new PrintWriter(System.out)));
          
            int reply;
                ftp.connect(server)
                
                // After connection attempt, you should check the reply code to verify
                // success.
                reply = ftp.getReplyCode();

                if (!FTPReply.isPositiveCompletion(reply))
                {
                    ftp.disconnect();
                    error = true;
                }
            
          if(!error) {
                
                    if (!ftp.login(username, password))
                    {
                        ftp.logout();
                        error = true;
                    }
             if(!error) {
                    if (binaryTransfer)
                        ftp.setFileType(FTP.BINARY_FILE_TYPE);

                    // Use passive mode as default 
                    ftp.enterLocalPassiveMode();
                    InputStream input;
                    input = new FileInputStream(localFilePath+localFileName);
                    boolean ftpSuccess = ftp.storeFile(hostDir+localFileName, input);
                    input.close();
                       
                    if (!ftpSuccess) {
                        throw new Exception("File ftp error");
                    }else {
                        if (SHOW_DEBUG) ftpMessage.append("<li>isFtpSuccess()...success").append("</li>");
                    }
                    
                    ftp.logout();
                    if (SHOW_DEBUG) ftpMessage.append("<li>ftp.logout()...success").append("</li>");
                    
             }
        }    
                 
        }
        catch (Exception ex) {
            ex.printStackTrace();
            System.out.println("Exception occur while transfering file using ftp"+ ex.toString());
            throw new Exception(ftpMessage.toString(), ex);
        }
        finally {
            if (ftp!=null && ftp.isConnected())
            {
                try
                {
                    ftp.disconnect();
                }
                catch (Exception e)
                {
                    e.printStackTrace();
                    System.out.println("Exception occur while transfering file using ftp"+ e.toString());
                    throw new Exception(ftpMessage.toString(), e);
                }
            }
        
            if (SHOW_DEBUG) ftpMessage.append("</ul>");
        }
        return ftpMessage.toString();
    }

Advertisement

Answer

I did some more debugging on doing ftp from linux to as400. And when doing pwd after logging to ftp server its giving message as:
ftp> pwd
257 “‘FTPID.'” is current prefix

And thinking how to remove that prefix so i ran the below command got output as no prefix defined:

ftp> cd ..
200 “” no prefix defined

And after that when i uploaded the file using put command it was uploaded successfully. So i did some research on how to go back one directory using Apache commons.net api’s that i am using and found the CDUP method.

When i ran the FTPClient.cdup() method before uploading the file. I was able to successfully FTP the file from java code as well.

ftp.cdup();
input = new FileInputStream(localFilePath+localFileName);
boolean ftpSuccess = ftp.storeFile(hostDir+localFileName, input);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement