使用Apache Commons 2.0调用FTPClient下载文件时遇“Access is denied”问题
Hey there, let's tackle this "Access is denied" issue you're hitting with Apache Commons FTPClient. It's strange you can list files but can't download—let's break down the most likely fixes step by step:
1. Check Server-Side File Permissions
Even though you can list the file, the FTP user might not have read permissions for that specific file on the server:
- For Unix/Linux servers: Verify the file's permissions (e.g.,
ls -l file.txt). Ensure thefrancisuser has at least read access (permission bits like644). - For Windows servers: Check the NTFS permissions on the file/folder—make sure the FTP user account has "Read" permissions enabled.
2. Enable Passive Mode (Critical for Firewalled Networks)
Most modern FTP servers require passive mode to establish data connections, especially if your client is behind a firewall. Your current code doesn't set this, which can lead to connection failures mislabeled as "access denied." Add this right after logging in:
client.enterLocalPassiveMode();
3. Verify Local File Write Permissions
Don't overlook your local system! If your Java program doesn't have permission to write to the target local directory, it will throw an "Access is denied" error too. Double-check:
- The local path you're writing to (e.g.,
C:/temp/or/home/user/downloads/) allows write access for the user running your program.
4. Confirm File Path & Working Directory
Sometimes the file list shows a filename, but the file is in a subdirectory you haven't navigated to. Use changeWorkingDirectory to switch to the correct folder before downloading:
// Replace with the actual path on your FTP server if (!client.changeWorkingDirectory("/path/to/your/files")) { System.err.println("Failed to navigate to target directory!"); return; }
Here's a revised version of your code with key fixes included:
import org.apache.commons.net.ftp.FTPClient; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.InetAddress; public class FTP extends Thread { @Override public void run() { FTPClient client = new FTPClient(); try { // Connect to FTP server client.connect(InetAddress.getByName("192.168.0.100"), 2221); System.out.println("Connection reply code: " + client.getReplyCode()); // Login with credentials boolean loginSuccess = client.login("francis", "francis"); if (!loginSuccess) { System.err.println("Login failed! Check username/password."); client.disconnect(); return; } // Enable passive mode to avoid firewall blocks client.enterLocalPassiveMode(); // Switch to the directory containing your file boolean dirChanged = client.changeWorkingDirectory("/your/target/directory"); if (!dirChanged) { System.err.println("Could not access target directory!"); client.disconnect(); return; } // Prepare local output stream (ensure local path has write access) File localFile = new File("/local/path/to/save/file.txt"); FileOutputStream outputStream = new FileOutputStream(localFile); // Download the file boolean downloadSuccess = client.retrieveFile("server-file.txt", outputStream); if (downloadSuccess) { System.out.println("File downloaded successfully!"); } else { System.err.println("Download failed. Server response: " + client.getReplyString()); } // Cleanup resources outputStream.close(); client.logout(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (client.isConnected()) { client.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } } }
Test the same file download using a standard FTP client (like FileZilla) with the francis credentials:
- If the client also fails: The issue is definitely on the server side (permissions, folder access, etc.).
- If the client succeeds: The problem is in your code's configuration (likely missing passive mode or incorrect path handling).
内容的提问来源于stack exchange,提问作者Imran Shad

