Category Archives: apache-commons-net

How to handle ‘Connection closed without indication’ for scheduled FTP requests?

There is a JBoss scheduler runs at every 5 minute and have 40 ftp hosts . Each time, scheduler will try to get files from these ftp hosts and process that .

Now there is a situation like, each scheduled interval one ftphost throws org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication. and next interval another host throws same exception.

I checked the reply code and I'm getting 0 .

Apache Commons Net FTPClient Will Not Execute listFiles()

I'm modifying some code that was previously working with an FTPS library I wrote myself. I've been asked to start using the Apache Commons Net library (FTPClient and FTPSClient mainly) and I'm running into problems doing a file listing. I've read other questions and it's not the enterLocalPassiveMode problem (Apache Commons Net FTPClient and listFiles()), as I'm using that after connecting, but before logging in. The same code works fine on a test server I set up (also using Apache FTP), but doesn't work on the server I need it for.

I've also tried using the "PBSZ 0" and "PROT P" commands, but they're not implemented on the remote system.

502 PBSZ Command not implemented.
502 PROT Command not implemented.

Code:

FTPSClient ftpsclient = new FTPSClient(true); // Implicit SSL
ftpsclient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
ftpsclient.connect(HOST_ADDR, HOST_PORT); // Using port 990
ftpsclient.enterLocalPassiveMode();
ftpsclient.user(USERID);
ftpsclient.pass(PASSWORD);
ftpsclient.setFileType(FTP.BINARY_FILE_TYPE);
ftpsclient.changeWorkingDirectory(REMOTE_DIR);
ftpsclient.printWorkingDirectory();
FTPFile[] ftpfiles = ftpsclient.listFiles(); // This is where it breaks

I've tried specifying the directory explicitly and using the default:

FTPFile[] ftpfiles = ftpsclient.listFiles();
FTPFile[] ftpfiles = ftpsclient.listFiles(REMOTE_DIR);

... but both give the same result. This is the output of the debugging info:

220 FTPS (Version  Thu Dec 10 17:23:00 2015) server ready.
USER ****
331 Password required for ****.
PASS ****
230 User **** logged in.
TYPE I
200 Type set to I
CWD outbound/directory
250 CWD Command successful.
PWD
257 "/usr/path/to/outbound/directory" is current directory.
SYST
215 UNIX
PASV
227 Entering Passive Mode (XX,XX,XX,XX,24,140) ***Edit: port 6284
LIST
150 Opening data connection for '/bin/ls'.

Then it times out after 30 seconds with this stack trace:

Stack Trace: org.apache.commons.net.ftp.FTPConnectionClosedException: Connection closed without indication.
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:317)
    at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:294)
    at org.apache.commons.net.ftp.FTP.getReply(FTP.java:692)
    at org.apache.commons.net.ftp.FTPClient.completePendingCommand(FTPClient.java:1813)
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3308)
    at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3271)
    at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:2930)

I've checked my firewall settings and that host is allowed to connect via port 990 and 6200-6300.

I've also read FTPClient.listFiles not working and Java application hanging during LIST command to FTP Server (Apache Commons FTPClient) and neither of these have helped with my problem.

Any ideas?

Apache FTPClient listFiles() vs listFileNames()

I am using apache-commons-net 3.4 and i found a strange issue i cant explain. I want to get the FTPFile entries of an directory but when i use FTPClient#listFiles() an empty array is returned, but FTPClient#listNames() list all entries.

    FTPClient client = new FTPClient();

    client.connect("192.168.1.4");
    client.enterLocalPassiveMode();
    client.login("ftpuser", "ftpuser");

    System.out.println( client.listFiles("/").length); // prints 0
    System.out.println( client.listNames("/").length); // prints 21

What is going on here how to use listFiles() correct ?

Related questions that didn't answer my question:

Apache FTPClient listFiles() vs listFileNames()

I am using apache-commons-net 3.4 and i found a strange issue i cant explain. I want to get the FTPFile entries of an directory but when i use FTPClient#listFiles() an empty array is returned, but FTPClient#listNames() list all entries.

    FTPClient client = new FTPClient();

    client.connect("192.168.1.4");
    client.enterLocalPassiveMode();
    client.login("ftpuser", "ftpuser");

    System.out.println( client.listFiles("/").length); // prints 0
    System.out.println( client.listNames("/").length); // prints 21

What is going on here how to use listFiles() correct ?

Related questions that didn't answer my question:

When trying apache commons net for FTP, what do you input for server?

Here's my code:

//Import statements that are going to be used
import java.io.*;
import java.util.*;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.*;
import org.apache.commons.net.util.*;


//The Class
public class ftp {

/*
 *   The point of this function is to check if there was a reply from the
 *   Server.
 */
private static void showReply(FTPClient ftpClient) {
    String[] replies = ftpClient.getReplyStrings();
    if (replies != null && replies.length > 0) {
        for (String aReply: replies) {
            System.out.println("SERVER: " + aReply);
        } //end of for
    } else {
        System.out.println("There was no reply from the server detected.");
    }
} //end of the showReply function

/**
 * Creates a nested directory structure on a FTP server
 * @param FTPClient an instance of org.apache.commons.net.FTP.FTPClient class.
 * @param dirPath Path of the directory, i.e /projects/java/FTP/demo
 * @return true if the directory was created successfully, false otherwise
 * @throws IOException if any error occurred during client-server communication
 */
public static boolean makeDirectories(FTPClient ftpClient, String dirPath)
throws IOException {
    String[] pathElements = dirPath.split("/");
    if (pathElements != null && pathElements.length > 0) {
        for (String singleDir: pathElements) {
            boolean existed = ftpClient.changeWorkingDirectory(singleDir);
            if (!existed) {
                boolean created = ftpClient.makeDirectory(singleDir);
                if (created) {
                    System.out.println("CREATED directory: " + singleDir);
                    ftpClient.changeWorkingDirectory(singleDir);
                } else {
                    System.out.println("COULD NOT create directory: " + singleDir);
                    return false;
                }
            }
        }
    }
    return true;
}

static void listDirectory(FTPClient ftpClient, String parentDir, String currentDir, int level) throws IOException {
    String dirToList = parentDir;
    if (!currentDir.equals("")) {
        dirToList += "/" + currentDir;
    }
    FTPFile[] subFiles = ftpClient.listFiles(dirToList);
    if (subFiles != null && subFiles.length > 0) {
        for (FTPFile aFile: subFiles) {
            String currentFileName = aFile.getName();
            if (currentFileName.equals(".") || currentFileName.equals("..")) {
                // skip parent directory and directory itself
                continue;
            }
            for (int i = 0; i < level; i++) {
                System.out.print("\t");
            }
            if (aFile.isDirectory()) {
                System.out.println("[" + currentFileName + "]");
                listDirectory(ftpClient, dirToList, currentFileName, level + 1);
            } else {
                System.out.println(currentFileName);
            }
        }
    }
}


public static void main(String[] args) {
    //Server location
    //Scanner scan = new Scanner(System. in );
    String server = "www.mywebsiteisawesome.com";
    //The port we are going to use
    int port = 21;
    //Username that is going to be used to login
    //System.out.println("Enter your username: ");
    //String user = scan.nextLine();
    String user="username";
    System.out.println("Your username is " + user);
    //The password we are going to use to login
    //System.out.println("Enter your password: ");
    //String pass = scan.nextLine();
    String pass= "password";
    //close scanner
    //scan.close();

    //create a FTPClient
    FTPClient ftpClient=new FTPClient();
    try {
        //System.out.println("testing");
        ftpClient.connect(server, port);
        showReply(ftpClient);
        int reply =  ftpClient.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            System.out.println("The operation failed. The reply code is : " + reply);
            return;
        } //end of if
        boolean success = ftpClient.login(user, pass);
        showReply(ftpClient);
        if (!success) {
            System.out.println("Couldn't log into the server");
            return;
        } else {
            System.out.println("The login was successful");
        } //end of else
        //Create a directory
        Scanner scan=new Scanner(System.in);
        System.out.println("Enter the directory or directory path you want to create");
        String directory = "/" + scan.nextLine();
        ftp.makeDirectories(ftpClient, directory);

        //List a directory
        System.out.println("Enter the name of the directory you want to list: ");
        String dirToList= "/" + scan.nextLine();
        listDirectory(ftpClient, dirToList, "", 0);



        //Logout and disconnect of the server.
        ftpClient.logout();
        ftpClient.disconnect();
        scan.close();
        } catch (IOException ex) {
            System.out.println("Something wrong happened");
            ex.printStackTrace();
        } //end of catch
    } //end of main
} //end of the class FTP

My question is for

String server= "Some website"

Do I have to enter a website that I own? Do buy a domain name, and setup a server?

My goal with this at the end is to do something like go to a website, log in, and then have my files available to me.

Opening/Downloading files stored in FTP using Java

Please have a look at the below code

FTPDownload.java

package ftp;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

/**
 * A program demonstrates how to upload files from local computer to a remote
 * FTP server using Apache Commons Net API.
 * @author www.codejava.net
 */
public class FTPDownload {

    public void download() {
        String server = "ftp.***.com";
        int port = 21;
        String user = "****";
        String pass = "****";

        FTPClient ftpClient = new FTPClient();
        try {

            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

            // APPROACH #1: using retrieveFile(String, OutputStream)
            String remoteFile1 = "/doctor/Katiya Karun.mp3";
            File downloadFile1 = new File("D:/Katiya Karun.mp3");
            OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloadFile1));
            boolean success = ftpClient.retrieveFile(remoteFile1, outputStream1);
            outputStream1.close();

            if (success) {
                System.out.println("File #1 has been downloaded successfully.");
            }


        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

FTPDownloadServlet.java

package ftp;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FTPDownloadServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           out.println("Downloading");
           FTPDownload f = new FTPDownload();
           f.download();

        } finally {
            out.close();
        }
    }


    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}

index.jsp

<form action="FTPDownloadServlet" method="post">
            <input type="submit" value="Download" />
        </form>

The above code is using Apache Commons Net library. Using the above code you can download files from an FTP server.

However, This program has a "hard coded" location to download the files. Instead, I need to open the "Save as" dialog when the submit button is clicked. Even better, instead of downloading I prefer to open the file in a new tab of the web page so people can right click and download only if they want(my real files will be images and pdf only).

How can I do this?

url link for downloading apache commons-net library [on hold]

I want to get the date and time from NTP server and i'm unsuccessful to download the library. I downloaded the files from this link but non of the file seems to be a library. https://commons.apache.org/proper/commons-net/download_net.cgi
I also downloaded a jar from this link and added in my project https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar but still the errors in my project does'nt disappered. Please give my any easy downloading link and explain how to use it in the application.

Enumerating FTPFile from FTP using ZipFile

My usual approach without an FTP to read ZipFiles was the following:

private void getLogFromZip(File logZip){
  ZipFile zf = new ZipFile(logZip);
  Enumeration<?> entries = zf.entries();
  while (entries.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) entries.nextElement();
  //do something with entry    
}

Now that I have a connection to an FTP server, retrieving an FTPFile and using it makes things difficult:

private void getLogFromZip(FTPFile logZip){
  ZipFile zf = new ZipFile(logZip.getName()); //here's the problem
  Enumeration<?> entries = zf.entries();
  while (entries.hasMoreElements()) {
    ZipEntry ze = (ZipEntry) entries.nextElement();
  //do something with entry    
}

Directly in line 1 I am getting this:

java.io.FileNotFoundException: logger_150510_092333.zip (System cannot find the file? specified)

What is a workaround for that? How can I specify a path so that it knows where to look for the zip?

Many thanks in advance!

Error while Rename file in ftp folder Java

Following is my code to read the file and rename it afterwards. Im using apache commons.net 3.0.1.

client.connect(localhost);
boolean login = client.login("username", "password");
if(login){
    System.out.println("login successful");
    boolean chdir = client.changeWorkingDirectory("/home/folder1/child/");
    String url = client.printWorkingDirectory();   // EDIT
    FTPFile[] result = client.listFiles(url, filter);
    if (result != null && result.length > 0) {
        for (FTPFile aFile : result) {
            try{
                String filename = aFile.getName();
                InputStream is= client.retrieveFileStream(filename);
                br = new BufferedReader(new InputStreamReader(is));
                    while((line = br.readLine()) != null){
                        System.out.println("the line is"+line);
                     }
                }
                finally{
                    if(br!=null){
                        try{
                           br.close();
                           String oldFilename =url + "/" +aFile.getName();
                           String newFilename = "PRO"+aFile.getName();
                           boolean rename = client.rename(oldFilename, newFilename);
                            if(rename){
                                    System.out.println("renamed");
                            }   
                           else{
                                    System.out.println("Error in renaming");
                                }

                          }

The file deosn't get renamed and the program prints

error in renaming files (cz boolean rename = false).

I have refereed to different examples. But all seems to show the same problem. The file is picked after filter and read without any issues.

If anyone could point to what I'm doing wrong here, that'd be very helpful.

Here, the url is String url = client.printWorkingDirectory();

I have tried with both relative path and absolute path. And giving full path only to the oldFilename and just the filname to the newFilename. Both did not work.

EDIT

Before changing the directory, the url will be / which is root. After changing the directory, the url will be /home/folder1/child/. This is the where the files exists.