spring-ftp-integration-server回复:553无法打开该文件:没有这样的文件或目录

c9qzyr3d  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(340)

我在工作中有一个要求,实现2个csv文件到远程服务器的ftp传输。我已经成功地实现了sftp,文件正在传输,但是当我尝试ftp时,我得到了以下错误(见下文)。我用我的hostgator ftp帐户测试了代码。我有书面许可。还尝试用winscp(ftp客户端)将文件传输到相同的位置,效果很好。

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

这是我的密码:
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="ticket@webaddress.com"/>
    <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"/>

ftptransportservice.java文件

@Service
public class FtpTransportService {

    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();
            }
        }
    }
}

完整堆栈跟踪

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
cunj1qz1

cunj1qz11#

我不知道为什么会出现这个错误,所以我放弃了springftp,并提出了一个不同的实现。希望这对别人有帮助。

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();
            }
        }
    }
}

相关问题