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

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

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

  1. 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文件

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:int="http://www.springframework.org/schema/integration"
  4. xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
  5. xsi:schemaLocation="http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp.xsd
  6. http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  8. <bean id="ftpClientFactory"
  9. class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
  10. <property name="host" value="ftp.webaddress.com"/>
  11. <property name="port" value="21"/>
  12. <property name="username" value="ticket@webaddress.com"/>
  13. <property name="password" value="mypassword"/>
  14. <property name="clientMode" value="0"/>
  15. </bean>
  16. <int:channel id="ftpChannel" />
  17. <int-ftp:outbound-channel-adapter id="ftpOutbound"
  18. channel="ftpChannel"
  19. remote-directory="/home2/etc/public_html/test"
  20. session-factory="ftpClientFactory"/>

ftptransportservice.java文件

  1. @Service
  2. public class FtpTransportService {
  3. public void ftpTransport(List<File> files){
  4. ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ftp-config.xml");
  5. try {
  6. // create ftpChannel
  7. MessageChannel ftpChannel = context.getBean("ftpChannel", MessageChannel.class);
  8. Message<File> message = null;
  9. // iterative the files and transfer
  10. for (File file : files) {
  11. // build message payload
  12. message = MessageBuilder.withPayload(file).build();
  13. // transfer the file
  14. ftpChannel.send(message);
  15. }
  16. } finally {
  17. if (context != null) {
  18. context.close();
  19. }
  20. }
  21. }
  22. }

完整堆栈跟踪

  1. Exception in thread "main" org.springframework.integration.MessageDeliveryException: Error handling message for file [Ticket Dump 2015-04-28 10:15.csv]
  2. at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:129)
  3. at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:78)
  4. at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:110)
  5. at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97)
  6. at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:61)
  7. at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:157)
  8. at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:128)
  9. at ie.service.ftp.FtpTransportService.ftpTransport(FtpTransportService.java:34)
  10. at ie.service.ticket.TicketReportService.runReport(TicketReportService.java:60)
  11. at ie.ManualRunner.main(ManualRunner.java:33)
  12. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  13. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
  14. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  15. at java.lang.reflect.Method.invoke(Method.java:606)
  16. at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
  17. 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
  18. at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.sendFileToRemoteDirectory(FileTransferringMessageHandler.java:205)
  19. at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:118)
  20. ... 14 more
  21. 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
  22. at org.springframework.integration.ftp.session.FtpSession.write(FtpSession.java:81)
  23. at org.springframework.integration.file.remote.session.CachingSessionFactory$CachedSession.write(CachingSessionFactory.java:141)
  24. at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.sendFileToRemoteDirectory(FileTransferringMessageHandler.java:200)
  25. ... 15 more
cunj1qz1

cunj1qz11#

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

  1. import org.apache.commons.net.ftp.FTP;
  2. import org.apache.commons.net.ftp.FTPClient;
  3. import org.springframework.stereotype.Service;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.util.List;
  9. @Service
  10. public class FtpTransportService {
  11. public void ftpTransport(List<File> files){
  12. String server = "webaddress.com";
  13. int port = 21;
  14. String user = "username";
  15. String pass = "password";
  16. FTPClient ftpClient = new FTPClient();
  17. try {
  18. ftpClient.connect(server, port);
  19. ftpClient.login(user, pass);
  20. ftpClient.enterLocalPassiveMode();
  21. ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  22. // iterative the files and transfer
  23. for (File file : files) {
  24. String RemoteFile = file.getName().toString();
  25. InputStream inputStream = new FileInputStream(file);
  26. System.out.println("Start uploading first file");
  27. boolean done = ftpClient.storeFile(RemoteFile, inputStream);
  28. inputStream.close();
  29. if (done) {
  30. System.out.println("The first file is uploaded successfully.");
  31. }
  32. }
  33. } catch (IOException ex) {
  34. System.out.println("Error: " + ex.getMessage());
  35. ex.printStackTrace();
  36. } finally {
  37. try {
  38. if (ftpClient.isConnected()) {
  39. ftpClient.logout();
  40. ftpClient.disconnect();
  41. }
  42. } catch (IOException ex) {
  43. ex.printStackTrace();
  44. }
  45. }
  46. }
  47. }
展开查看全部

相关问题