我需要在工作中实现将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();
}
}
}
}
完整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
答案 0 :(得分:0)
我无法弄清楚为什么会出现这个错误,所以我放弃了Spring FTP,我想出了一个不同的实现。希望这有助于某人。
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();
}
}
}
}