MuleSoft File and FTP Connectors
File-based integration is one of the oldest and most common patterns in enterprise systems. Banks send daily transaction files. ERPs export data as CSV overnight. Partners exchange XML invoices via SFTP. MuleSoft's File Connector handles local filesystem operations, while the FTP and SFTP Connectors handle remote file transfers over the network.
File Connector — Local Filesystem Operations
The File Connector reads, writes, moves, copies, renames, and deletes files on the local filesystem of the server where Mule runs.
File Connector Operations
File Connector │ ├── On New File (trigger) → fires when a new file appears in a folder ├── Read → read a specific file by path ├── Write → write content to a file ├── Copy → copy a file to another location ├── Move → move a file to another location ├── Rename → rename a file ├── Delete → delete a file └── List → list all files in a directory
File Listener — Polling a Folder for New Files
The On New File trigger polls a folder at a set interval. When a new file appears, the trigger fires and the flow starts with the file content as the payload.
File Listener Configuration and Flow
[File Listener: On New File]
Directory: /opt/data/inbox/orders/
Poll Frequency: 60 SECONDS
Move to: /opt/data/processing/orders/ (move while processing)
Auto Delete: false
When a file arrives (e.g., orders_2024-01-15.csv):
payload = file contents as stream
attributes.fileName = "orders_2024-01-15.csv"
attributes.size = 48320 (bytes)
attributes.creationTime = 2024-01-15T08:00:00
│
▼
[Transform: parse CSV to JSON array]
%dw 2.0
output application/json
---
payload
│
▼
[For Each: process each order record]
[Database: insert into orders table]
│
▼ (after all records processed)
[File: Move processed file]
Source: /opt/data/processing/orders/orders_2024-01-15.csv
Target Dir: /opt/data/archive/orders/
Rename To: #[attributes.fileName replace ".csv" with "_done_" ++ (now() as String {format:"yyyyMMddHHmmss"}) ++ ".csv"]
Writing Files
Use the File Write operation to create output files — reports, exports, or data files for partner systems.
Write a Report File
[Scheduler: every day at 11 PM]
│
▼
[Database: SELECT product, sum(quantity) as sold FROM order_lines GROUP BY product]
│
▼
[Transform: convert to CSV]
%dw 2.0
output application/csv header=true
---
payload map (row) -> {
"Product": row.PRODUCT,
"Units Sold": row.SOLD
}
│
▼
[File: Write]
Path: /opt/reports/daily_sales_#[now() as String {format:"yyyy-MM-dd"}].csv
Mode: CREATE_NEW
Encoding: UTF-8
FTP Connector — Remote File Transfer
The FTP Connector connects to remote servers over the FTP protocol. Use it when a partner or external system provides files on their FTP server, or when you need to deposit files on a partner's FTP server.
FTP Configuration
FTP Connection Config:
Name: FTP_Partner_Config
Host: ftp.partner-company.com
Port: 21
Username: mule_integration
Password: ${ftp.password}
Working Dir: /outbound/orders/
Passive Mode: true (required for most corporate firewalls)
Download Files from FTP
[Scheduler: every morning at 6 AM]
│
▼
[FTP: List Files]
Directory: /inbound/invoices/
Matcher: filename matches "invoice_*.xml"
│
▼ (payload = list of file metadata)
[For Each: process each file]
│
├── [FTP: Read]
│ Path: /inbound/invoices/#[payload.name]
│ (payload becomes file content)
│
├── [Transform: parse XML invoice]
│
├── [Database: insert invoice to local DB]
│
└── [FTP: Move]
Source: /inbound/invoices/#[vars.currentFileName]
Target: /inbound/processed/#[vars.currentFileName]
SFTP Connector — Secure File Transfer
SFTP (SSH File Transfer Protocol) encrypts the data during transfer. Use SFTP for any sensitive data — financial records, personal information, healthcare data. Many banks and healthcare partners require SFTP.
SFTP Configuration with Key-Based Authentication
SFTP Connection Config:
Host: sftp.bank.com
Port: 22
Username: mule_bank_integration
Authentication: Identity File (SSH private key)
Identity File: /opt/keys/bank_integration_rsa
Passphrase: ${sftp.keyPassphrase}
Known Hosts File: /opt/keys/known_hosts
Upload Files to SFTP
[Scheduler: every day at midnight]
│
▼
[Database: SELECT * FROM daily_settlements WHERE date = :today]
│
▼
[Transform: convert to fixed-width format for bank]
│
▼
[Set Variable: filename = "SETTLE_" ++ (now() as String {format:"yyyyMMdd"}) ++ ".dat"]
│
▼
[SFTP: Write]
Path: /outbound/settlements/#[vars.filename]
Mode: CREATE_NEW
(payload = the formatted file content)
│
▼
[Logger: "Uploaded settlement file: #[vars.filename]"]
File Locking and Concurrency
When multiple Mule application instances run simultaneously (common in CloudHub with multiple workers), two instances might try to read the same file at the same time. Use the Move to directory while processing setting on the File/FTP Listener. The first instance moves the file before processing it, so the second instance never sees it.
Safe File Processing with Move-Before-Process
File Listener Settings: Directory: /inbox/ Move to: /processing/ ← moves file immediately when trigger fires Sequence: t=0: File "order.csv" appears in /inbox/ t=1: Instance A trigger fires → moves order.csv to /processing/ t=2: Instance B trigger fires → /inbox/ is now empty → nothing to process t=3: Instance A processes order.csv safely t=4: Instance A moves order.csv to /archive/
Matching Only Specific Files
The File and FTP Listeners support a Matcher to filter which files trigger the flow. This avoids picking up temporary files, partially-written files, or files intended for other processes.
Matcher Configuration:
Filename Pattern: invoice_*.xml (only XML invoice files)
Minimum File Age: 5000 ms (skip files less than 5 seconds old)
(avoids picking up files being written)
Maximum File Size: 50 MB (skip suspiciously large files)
