Question

I am trying to write an FTP Client and Server that will allow me to send a file from the client to the server via anonymous FTP. However, I keep getting 550 Permission Denied. I am able to do other things like download a file from the server, or get a list of the files in the directory, but whenever I try to do a download it says 550 Permission Denied. The result is the same whether I log in or use anonymous FTP.

I don't see any problems with my code, but I have tried running it on different networks and computers with the same result. Is there a problem with the code that I don't see, or do I have to do something with the router/firewall?

I am writing both the client and server in Java and running Windows. The libraries I am using are Apache Commons FTP Client and Apache FTP Server.

Here is the client. The commented out code is for uploading and getting a list of the files in the directory.

import org.apache.commons.net.ftp.*;
import java.io.*;
import java.net.*;

public class Client
{
    public Client()
    {
        // Do nothing
    }

    public void transferFile(String ipAddress)
    {
        // For uploading
        FileInputStream file = null;
        // For downloading
        // FileOutputStream file = null;

        try
        {
            InetAddress address = InetAddress.getByName(ipAddress);

            FTPClient ftpClient = new FTPClient();
            ftpClient.connect(address, 5000);
            ftpClient.login("anonymous", "");
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            ftpClient.enterLocalPassiveMode();

            // For uploading
            file = new FileInputStream(new File("C:/SoundFiles/Send/Test1.txt"));
            // For downloading
            // file = new FileOutputStream(new File("C:/SoundFiles/Receive/Test2.txt"));

            // For uploading
            boolean success = ftpClient.storeFile("/Receive/Test2.txt", file);
            // For downloading
            // boolean success = ftpClient.retrieveFile("/Send/Test1.txt", file);

            // For listing the files on the server's directory
            /*
            FTPFile[] directoryFiles = ftpClient.listFiles();  
            System.out.println("There are " + directoryFiles.length + " files");
            for(int i = 0; i < directoryFiles.length; i++)
            {
                System.out.println(i + ": " + directoryFiles[i].getName());
            }
            */

            if(success)
                System.out.println("Success.");
            else
                System.out.println("Fail.");

            ftpClient.logout();
            ftpClient.disconnect();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if(file != null)
                    file.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args)
    {
        Client client = new Client();
        client.transferFile("Enter your IP address here");
    }
}

Here is the server.

import org.apache.ftpserver.FtpServer;
import org.apache.ftpserver.FtpServerFactory;
import org.apache.ftpserver.ConnectionConfigFactory;
import org.apache.ftpserver.usermanager.impl.BaseUser;
import org.apache.ftpserver.listener.ListenerFactory;
import org.apache.ftpserver.ftplet.FtpException;
import org.apache.ftpserver.ftplet.UserManager;

public class Server
{
    public Server()
    {
        // Do nothing
    }

    public void startServer()
    {
        FtpServer server = null;

        try
        {
            FtpServerFactory ftpServerFactory = new FtpServerFactory();

            ListenerFactory listenerFactory = new ListenerFactory();
            listenerFactory.setPort(5000);
            ftpServerFactory.addListener("default", listenerFactory.createListener());

            ConnectionConfigFactory configFactory = new ConnectionConfigFactory();
            configFactory.setAnonymousLoginEnabled(true);
            ftpServerFactory.setConnectionConfig(configFactory.createConnectionConfig());

            BaseUser user = new BaseUser();
            user.setName("anonymous");
            user.setPassword("");
            user.setHomeDirectory("C:/SoundFiles");
            UserManager userManager = ftpServerFactory.getUserManager();
            userManager.save(user);

            server = ftpServerFactory.createServer();
            server.start();
        }
        catch (FtpException e)
        {
            e.printStackTrace();
        }

        // Stop the server after 10 seconds
        try
        {
            Thread.sleep(10000);
        }
        catch(InterruptedException e)
        {
            e.printStackTrace();
        }

        if(server != null)
            server.stop();
    }

    public static void main(String[] args)
    {
        Server server = new Server();
        server.startServer();
    }
}

This is the output I keep getting from the server.

[main] INFO org.apache.ftpserver.impl.DefaultFtpServer - FTP server started
[NioProcessor-3] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CREATED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - OPENED
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 220 Service ready for new user.

[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: USER anonymous
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 331 Guest login okay, send your complete e-mail address as password.

[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASS 
[pool-3-thread-1] INFO org.apache.ftpserver.command.impl.PASS - Anonymous login success - null
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 230 User logged in, proceed.

[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: TYPE I
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 200 Command TYPE okay.

[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: PASV
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 227 Entering Passive Mode

[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: STOR /Receive/Test2.txt
[pool-3-thread-2] WARN org.apache.ftpserver.impl.PassivePorts - Releasing unreserved passive port: 2133
[pool-3-thread-2] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 550 /Receive/Test2.txt: Permission denied.

[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - RECEIVED: QUIT
[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - SENT: 221 Goodbye.

[pool-3-thread-1] INFO org.apache.ftpserver.listener.nio.FtpLoggingFilter - CLOSED

Any suggestions on how I can get this to work are much appreciated.

Was it helpful?

Solution

I ended up finding out what was missing. I just needed to add the following lines of code in the Server class.

List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top