相关文章推荐
爱喝酒的核桃  ·  关于Kafka ...·  1 年前    · 
奔跑的啄木鸟  ·  vue v-for 定义变量-掘金·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Spring Ftp integration - Server replied with: 553 Can't open that file: No such file or directory

Ask Question

I have a requirement in work to implement an FTP transfer of 2 CSV files to a remote server. I have managed to implement the SFTP successfully and files are getting transferred but when I try the FTP i get the following error (see below). I tested the code using my Hostgator FTP account. I have write permission and all. Also tried to transfer the files with WinSCP (ftp client) to the same location and it works fine.

java.io.IOException: Failed to write to '/home2/etc/public_html/test/Ticket Dump 2015-04-28 09:51.csv.writing'. Server replied with: 553 Can't open that file: No such file or directory

Here is my code:

ftp-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:int="http://www.springframework.org/schema/integration"
   xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
   xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
            http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="ftpClientFactory"
      class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
    <property name="host" value="ftp.webaddress.com"/>
    <property name="port" value="21"/>
    <property name="username" value="[email protected]"/>
    <property name="password" value="mypassword"/>
    <property name="clientMode" value="0"/>
</bean>
<int:channel id="ftpChannel" />
<int-ftp:outbound-channel-adapter id="ftpOutbound"
                                  channel="ftpChannel"
                                  remote-directory="/home2/etc/public_html/test"
                                  session-factory="ftpClientFactory"/>
    public void ftpTransport(List<File> files){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ftp-config.xml");
        try {
            // create ftpChannel
            MessageChannel ftpChannel = context.getBean("ftpChannel", MessageChannel.class);
            Message<File> message = null;
            // iterative the files and transfer
            for (File file : files) {
                // build message payload
                message = MessageBuilder.withPayload(file).build();
                // transfer the file
                ftpChannel.send(message);
        } finally {
            if (context != null) {
                context.close();

Full Stacktrace

    Exception in thread "main" org.springframework.integration.MessageDeliveryException: Error handling message for file [Ticket Dump 2015-04-28 10:15.csv]
    at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:129)
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110)
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
    at ie.service.ftp.FtpTransportService.ftpTransport(FtpTransportService.java:34)
    at ie.service.ticket.TicketReportService.runReport(TicketReportService.java:60)
    at ie.ManualRunner.main(ManualRunner.java:33)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Caused by: org.springframework.integration.MessagingException: Failed to write to '/home2/etc/public_html/test/Ticket Dump 2015-04-28 10:15.csv.writing' while uploading the file
    at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.sendFileToRemoteDirectory(FileTransferringMessageHandler.java:205)
    at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:118)
    ... 14 more
Caused by: java.io.IOException: Failed to write to '/home2/etc/public_html/test/Ticket Dump 2015-04-28 10:15.csv.writing'. Server replied with: 553 Can't open that file: No such file or directory
    at org.springframework.integration.ftp.session.FtpSession.write(FtpSession.java:81)
    at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:141)
    at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.sendFileToRemoteDirectory(FileTransferringMessageHandler.java:200)
    ... 15 more

I wasn't able to figure out why this error so I gave up at Spring FTP and I came up with a different implementation. Hope this helps someone.

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
@Service
public class FtpTransportService {
    public void ftpTransport(List<File> files){
        String server = "webaddress.com";
        int port = 21;
        String user = "username";
        String pass = "password";
        FTPClient ftpClient = new FTPClient();
        try {
            ftpClient.connect(server, port);
            ftpClient.login(user, pass);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
            // iterative the files and transfer
            for (File file : files) {
                String RemoteFile = file.getName().toString();
                InputStream inputStream = new FileInputStream(file);
                System.out.println("Start uploading first file");
                boolean done = ftpClient.storeFile(RemoteFile, inputStream);
                inputStream.close();
                if (done) {
                    System.out.println("The first file is uploaded successfully.");
        } catch (IOException ex) {
            System.out.println("Error: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                if (ftpClient.isConnected()) {
                    ftpClient.logout();
                    ftpClient.disconnect();
            } catch (IOException ex) {
                ex.printStackTrace();
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.