Skip to main content
Level 1
Nov 4, 2026 12:21 PM UTC
Question

Export a Content pages as a package(.zip) from AEM to FTP (port 21) location

From AEM I am trying to export a content package from AEM to FTP location of port 21 I am having FTP server credentials, from AEM local instance I am able to export the content package to FTP location verified in FileZilla, configured the dynamic variables and deployed to DEV. In DEV I am getting error like:-
 

ERROR [sling-threadpool-dfa082d7-49da-4b80-baf5-c55e7572c283-(apache-sling-job-thread-pool)-29-<main queue>(com/upcom/jobs/export-last24hr-package)] com.upcom.core.schedulers.contentpackageexport.ContentPackageExportJobConsumer FTP upload failure. Host=[proxy.tunnel], Port=[30021], Error=[Connection refused]

java.net.ConnectException: Connection refused

at java.base/sun.nio.ch.Net.pollConnect(Native Method)

at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:690)

at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)

at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)

at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327)

at java.base/java.net.Socket.connect(Socket.java:751)

at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:769) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:946) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3274) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3295) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at com.upcom.core.schedulers.contentpackageexport.ContentPackageExportJobConsumer.uploadToFtp(ContentPackageExportJobConsumer.java:226) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at com.upcom.core.schedulers.contentpackageexport.ContentPackageExportJobConsumer.exportLast24HourPackage(ContentPackageExportJobConsumer.java:126) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at com.upcom.core.schedulers.contentpackageexport.ContentPackageExportScheduler.process(ContentPackageExportScheduler.java:99) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

at org.apache.sling.event.impl.jobs.JobConsumerManager$JobConsumerWrapper.process(JobConsumerManager.java:566) [org.apache.sling.event:4.4.2]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.startJob(JobQueueImpl.java:381) [org.apache.sling.event:4.4.2]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl.access$100(JobQueueImpl.java:58) [org.apache.sling.event:4.4.2]

at org.apache.sling.event.impl.jobs.queues.JobQueueImpl$1.run(JobQueueImpl.java:308) [org.apache.sling.event:4.4.2]

at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)

at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)

at java.base/java.lang.Thread.run(Thread.java:1583) 
Getting above error at boolean stored = ftpClient.storeFile(fileName, inputStream);

below is the code I am using to upload the package to FTP

private void uploadToFtp(String fileName, byte[] data) throws IOException {    FTPClient ftpClient = new FTPClient();    try {        ftpClient.connect(ftpHost, ftpPort);        int replyCode = ftpClient.getReplyCode();        if (!FTPReply.isPositiveCompletion(replyCode)) {            throw new IOException("FTP server refused connection. Host: " + ftpHost                    + ". Port: " + ftpPort + ". Reply code: " + replyCode);        }        if (!ftpClient.login(ftpUsername, ftpPassword)) {            throw new IOException("FTP login failed for user [" + ftpUsername                    + "] on host [" + ftpHost + "]. Reply: " + ftpClient.getReplyString());        }        ftpClient.enterLocalPassiveMode();        ftpClient.setFileType(FTP.BINARY_FILE_TYPE);        String environment = resolveEnvironment();        String remoteDirectory = normalizeFtpPath(ftpFilePath) + environment;        changeToRemoteDirectoryFtp(ftpClient, remoteDirectory);        try (InputStream inputStream = new ByteArrayInputStream(data)) {            boolean stored = ftpClient.storeFile(fileName, inputStream);            if (!stored) {                throw new IOException("FTP upload failed for file [" + fileName                        + "]. Server reply: " + ftpClient.getReplyString());            }        }        LOG.info("Uploaded package [{}] to FTP path [{}]", fileName, remoteDirectory);    } catch (IOException e) {        throw new IOException(                "Unable to upload package to FTP. Host: " + ftpHost                        + ". Port: " + ftpPort                        + ". Error: " + e.getMessage(), e);    } finally {        try {            if (ftpClient.isConnected()) {                ftpClient.logout();                ftpClient.disconnect();            }        } catch (IOException ignored) {        }    }}

5 replies

shrutid56001193
User Group Leader
User Group Leader
Nov 4, 2026 12:52 PM UTC

The error indicates a network connectivity issue, not an issue with storeFile() itself.

AEM DEV is connecting to proxy.tunnel:30021, but the connection is being refused.

Check whether the DEV environment has network access to the FTP server and whether the proxy/port is correctly configured and allowlisted, if FTP is behind a VPN. Also verify the FTP data ports are reachable, since the stack trace shows the failure occurs while opening the data connection.

Translate
https://www.shrutidamle.com/
Level 2
Nov 5, 2026 1:43 PM UTC

The FTP user able to perform operations like:-
1.Successfully Login to FTP server
2.FTP reply code got 220 
3.Able to create environment specific directory at the FTP path provided
4.At establishing Data port connection getting Connection Refused

Verified above steps using log statements and any resolution to make Data port connection Successful to transmit the Data package Successfully.

Translate
Raja_Reddy
Community Advisor
Community Advisor
Nov 7, 2026 10:23 AM UTC

Hi ​@Venkata_NarayanaBa 
 

Since login, cwd and directory creation all work, the control connection through the proxy is fine. The failure is only in the data connection, and this is a known FTP passive mode problem with AEMaaCS Advanced Networking.

Why it happens

With enterLocalPassiveMode(), the FTP server replies to PASV with a random data port (for example 50123). commons-net then connects to proxy.tunnel:50123. The AEM proxy only listens on the ports you mapped in portForwards (30000-30999), and the port mapped to 21 is the only one set up. Nothing is listening on the passive port, so you get Connection refused at storeFile. It works locally because there is no proxy in between.

  1. Use SFTP (recommended). It uses a single connection on port 22, so one port-forward rule is enough and there is no separate data channel.
  2. Stay on FTP and pin the passive ports.
    • Ask the FTP server admin to restrict passive ports to a small range, for example 30100-30110, and advertise those same ports.
    • Add a port-forward rule for each passive port in your Advanced Networking configuration, alongside the control port:

json

   "portForwards": [
{ "name": "your-ftp-host", "portDest": 21, "portOrig": 30021 },
{ "name": "your-ftp-host", "portDest": 30100, "portOrig": 30100 },
{ "name": "your-ftp-host", "portDest": 30101, "portOrig": 30101 }
]

Passive ports need portOrig and portDest to be the same value, because the client connects to whatever port the server advertises.

Active mode won’t work, since the server would have to connect back into AEM.

Hope this helps. If you can share the PASV reply from your logs, I can help check the port mapping.

Translate
Level 2
Nov 20, 2026 1:49 PM UTC

Hi ​@Raja_Reddy 
the following are the log statements I got from the DEV environment
FTP Host [proxy.tunnel] resolved to [127.0.0.1]
Connecting to FTP server [proxy.tunnel:30021]
FTP connection established in 116 ms
FTP server reply code: 220
Attempting FTP login for user
FTP login successful
FTP passive mode enabled (NAT workaround applied). PASV host=[null], PASV data port=[-1] (not yet negotiated, expect -1 here)
FTP upload failure. Host=[proxy.tunnel], Port=[30021], Error=[Connection refused]
java.net.ConnectException: Connection refused
    at java.base/sun.nio.ch.Net.pollConnect(Native Method)
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:690)
    at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542)
    at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:350)
    at java.base/java.net.Socket.connect(Socket.java:751)
    at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:769) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]
    at org.apache.commons.net.ftp.FTPClient._storeFile(FTPClient.java:946) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3274) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]
    at org.apache.commons.net.ftp.FTPClient.storeFile(FTPClient.java:3295) [com.upcom.aem-upcom-project.core:0.0.1.SNAPSHOT]

I got PASV data port -1 here...how can we resolve this to establish a Data port connection and export package from AEM to FTP location

Translate
Raja_Reddy
Community Advisor
Community Advisor
Nov 23, 2026 8:26 AM UTC

Hi ​@VENKATA_NARAYANABA1 

PASV data port=[-1] is expected in your log. You print getPassivePort() before storeFile() runs. The PASV command is only sent inside storeFile() → _openDataConnection_, which is exactly where your stack trace fails. So the log doesn’t show the real port yet.

Login works, so the control connection through the tunnel is fine. The Connection refused comes from the data connection. commons-net connects to proxy.tunnel:<port from the 227 reply>, and the AEM proxy only listens on ports declared in portForwards.

Step 1: log the real PASV reply

java
ftpClient.addProtocolCommandListener(new ProtocolCommandListener() {
    @Override public void protocolCommandSent(ProtocolCommandEvent e) {
        String m = e.getMessage().trim();
        if (!m.startsWith("PASS")) LOG.info("FTP > {}", m);
    }
    @Override public void protocolReplyReceived(ProtocolCommandEvent e) {
        LOG.info("FTP < {}", e.getMessage().trim());
    }
});

Look for 227 Entering Passive Mode (a,b,c,d,p1,p2). The data port is p1*256 + p2.

Step 2: fix

Option A: SFTP (recommended). It uses one connection with no data channel, so a single portForwards rule is enough.

Option B: stay on FTP and pin the passive ports. Ask the FTP admin to restrict passive ports to a small range inside 30000-30999, for example 30100-30110. Then add a forward for each port, with portOrig equal to portDest:

json
"portForwards": [
  { "name": "your-ftp-host", "portDest": 21,    "portOrig": 30021 },
  { "name": "your-ftp-host", "portDest": 30100, "portOrig": 30100 },
  { "name": "your-ftp-host", "portDest": 30101, "portOrig": 30101 }
  // ... through 30110
]

Also force the data connection to use the tunnel host rather than the IP the server advertises:

java
ftpClient.setPassiveNatWorkaroundStrategy(hostname -> ftpHost); // proxy.tunnel
ftpClient.enterLocalPassiveMode();

Active mode won’t work, because the server would have to connect back into AEM.

 

Translate