我正在尝试编写一个FTP客户端和服务器,它允许我通过匿名FTP将文件从客户端发送到服务器。但是,我一直在550 Permission Denied
。我可以做其他事情,比如从服务器下载文件,或者获取目录中的文件列表,但每当我尝试下载时,它都会显示550 Permission Denied
。无论我是登录还是使用匿名FTP,结果都是一样的。
我没有看到我的代码有任何问题,但我尝试在不同的网络和计算机上运行它,结果相同。我看不到的代码是否有问题,或者我必须对路由器/防火墙做些什么?
我用Java编写客户端和服务器并运行Windows。我使用的库是Apache Commons FTP Client
和Apache 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
非常感谢任何有关如何使其发挥作用的建议。
答案 0 :(得分:8)
我最终找到了遗漏的东西。我只需要在Server类中添加以下代码行。
List<Authority> authorities = new ArrayList<Authority>();
authorities.add(new WritePermission());
user.setAuthorities(authorities);