java - FTP客户端不断获得服务器上载的拒绝权限

我正在尝试编写一个ftp客户端和服务器,允许我通过匿名ftp从客户端向服务器发送文件。然而,我总是得到550 Permission Denied。我可以做其他事情,比如从服务器下载文件,或者获取目录中的文件列表,但是每当我尝试下载时,它都会说550 Permission Denied。无论我是登录还是使用匿名ftp,结果都是一样的。
我看不出我的代码有什么问题,但我试过在不同的网络和计算机上运行它,结果是一样的。是我看不到的代码有问题,还是我必须对路由器/防火墙做些什么?
我用Java编写客户机和服务器,并运行Windows。我使用的库是Apache Commons FTP ClientApache FTP Server
这是客户。注释掉的代码用于上载和获取目录中的文件列表。

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

这是服务器。
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();
    }
}

这是我不断从服务器得到的输出。
[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

任何关于我如何让这个工作的建议都是非常感谢的。


最佳答案:

我最终发现了丢失的东西。我只需要在服务器类中添加以下代码行。

List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);

译文:来源   文章分类: java ftp permission-denied ftp-client ftp-server

相关文章:

java - Java:try-catch后缺少return语句[重复]

java - JSoup的替代方法或如何清除空白

java - Android项目使用httpclient - > http.client(apache),post / get方法

java - StereoBM没有提供适当的输出

java - 三个测试容器从哪里来?

java - Java / Jetty / Wildfly:正常处理中止的客户端连接

java - 尽管我的课程已加载,但Class.forName会引发ClassNotFoundException

java - 如何使用fabric sdk创建android studio库?

java - 会话超时重定向到登录Java Maven项目

java - Enumeration变量如何工作?