I am pretty new in Spring and I have the following situation.
I have implemented a simple console Spring application that perform the upload of a file into an FTP server using Spring Integration FTP project: http://docs.spring.io/spring-integration/reference/html/ftp.html
So I have:
1) MainApp class:
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
MessageChannel ftpChannel = context.getBean("ftpChannel", MessageChannel.class);
File file = new File("C:\\test.txt");
Message<File> fileMessage = MessageBuilder.withPayload(file).build();
ftpChannel.send(fileMessage);
((AbstractApplicationContext) context).close();
}
}
2) And this is the Beans.xml configuration file in which I configure the FTP connection:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-ftp="http://www.springframework.org/schema/integration/ftp"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration/ftp http://www.springframework.org/schema/integration/ftp/spring-integration-ftp-4.2.xsd
http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.2.xsd">
<bean id="helloWorld" class="it.myCompany.myProject.batch.HelloWorld">
<property name="message" value="Hello World!"/>
</bean>
<bean id="ftpClientFactory" class="org.springframework.integration.ftp.session.DefaultFtpSessionFactory">
<property name="host" value="MY HOST"/>
<property name="username" value="USERNAME"/>
<property name="password" value="PSWD"/>
<property name="clientMode" value="0"/>
<property name="fileType" value="2"/>
<property name="bufferSize" value="10000"/>
</bean>
<int:channel id="ftpChannel"/>
<!--
<int-ftp:outbound-channel-adapter
id="sendFileToServer"
auto-create-directory="true"
session-factory="ftpSessionFactory"
remote-directory="/test"/>
-->
<int-ftp:outbound-channel-adapter id="outFtpAdapter"
channel="ftpChannel"
session-factory="ftpClientFactory"
remote-directory="/home/myFolder"/>
</beans>
From my local Windows machine (performin it from Eclipse) it works fine and the selected file is putted into the /home/myFolder directory on my FTP server.
The problem is that I have to perform it as an runnable JAR file on a Linux machine.
So I have simply change this line of the MainApp class:
File file = new File("C:\\test.txt");
in this way:
File file = new File("/home/myUser/provaVPC.txt");
(in this way I simply select a specific .txt file into the Linux filesystem).
And then I exported my project as a runnable JAR file (so it contains all the required dependencies needed to work).
So I put the obtained jar file into the Linux machine but when I try to perform it in this environment I obtain the following error message:
[maquilanti@jboss_svil ~]$ java -jar TestFtp.jar
Feb 15, 2016 4:25:58 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@490b8a8c: startup date [Mon Feb 15 16:25:58 CET 2016]; root of context hierarchy
Feb 15, 2016 4:25:58 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Feb 15, 2016 4:25:59 PM org.springframework.beans.factory.config.PropertiesFactoryBean loadProperties
INFO: Loading properties file from URL [jar:rsrc:spring-integration-core-4.2.4.RELEASE.jar!/META-INF/spring.integration.default.properties]
Feb 15, 2016 4:25:59 PM org.springframework.integration.config.IntegrationRegistrar registerHeaderChannelRegistry
INFO: No bean named 'integrationHeaderChannelRegistry' has been explicitly defined. Therefore, a default DefaultHeaderChannelRegistry will be created.
Feb 15, 2016 4:26:00 PM org.springframework.integration.config.DefaultConfiguringBeanFactoryPostProcessor registerErrorChannel
INFO: No bean named 'errorChannel' has been explicitly defined. Therefore, a default PublishSubscribeChannel will be created.
Feb 15, 2016 4:26:00 PM org.springframework.integration.config.DefaultConfiguringBeanFactoryPostProcessor registerTaskScheduler
INFO: No bean named 'taskScheduler' has been explicitly defined. Therefore, a default ThreadPoolTaskScheduler will be created.
Feb 15, 2016 4:26:00 PM org.springframework.beans.factory.config.PropertiesFactoryBean loadProperties
INFO: Loading properties file from URL [jar:rsrc:spring-integration-core-4.2.4.RELEASE.jar!/META-INF/spring.integration.default.properties]
Feb 15, 2016 4:26:00 PM org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler initialize
INFO: Initializing ExecutorService 'taskScheduler'
Feb 15, 2016 4:26:00 PM org.springframework.context.support.DefaultLifecycleProcessor start
INFO: Starting beans in phase 0
Feb 15, 2016 4:26:00 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {message-handler:outFtpAdapter} as a subscriber to the 'ftpChannel' channel
Feb 15, 2016 4:26:00 PM org.springframework.integration.channel.DirectChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.context.support.ClassPathXmlApplicationContext@490b8a8c.ftpChannel' has 1 subscriber(s).
Feb 15, 2016 4:26:00 PM org.springframework.integration.endpoint.EventDrivenConsumer start
INFO: started outFtpAdapter
Feb 15, 2016 4:26:00 PM org.springframework.integration.endpoint.EventDrivenConsumer logComponentSubscriptionEvent
INFO: Adding {logging-channel-adapter:_org.springframework.integration.errorLogger} as a subscriber to the 'errorChannel' channel
Feb 15, 2016 4:26:00 PM org.springframework.integration.channel.PublishSubscribeChannel adjustCounterIfNecessary
INFO: Channel 'org.springframework.context.support.ClassPathXmlApplicationContext@490b8a8c.errorChannel' has 1 subscriber(s).
Feb 15, 2016 4:26:00 PM org.springframework.integration.endpoint.EventDrivenConsumer start
INFO: started _org.springframework.integration.errorLogger
Your Message : Hello World!
Exception in thread "main" java.lang.reflect.InvocationTargetException
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 org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.messaging.MessageDeliveryException: Error handling message for file [/home/maquilanti/provaVPC.txt -> provaVPC.txt]; nested exception is org.springframework.messaging.MessagingException: Failed to write to '/home/eds/provaVPC.txt.writing' while uploading the file; nested exception is java.io.IOException: Failed to write to '/home/eds/provaVPC.txt.writing'. Server replied with: 500 Illegal PORT command.
at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:320)
at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:282)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:410)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:282)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:272)
at org.springframework.integration.file.remote.RemoteFileTemplate.send(RemoteFileTemplate.java:264)
at org.springframework.integration.file.remote.handler.FileTransferringMessageHandler.handleMessageInternal(FileTransferringMessageHandler.java:142)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127)
at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116)
at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:147)
at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:120)
at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:442)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:392)
at it.hp.miur.batch.MainApp.main(MainApp.java:23)
... 5 more
Caused by: org.springframework.messaging.MessagingException: Failed to write to '/home/eds/provaVPC.txt.writing' while uploading the file; nested exception is java.io.IOException: Failed to write to '/home/eds/provaVPC.txt.writing'. Server replied with: 500 Illegal PORT command.
at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:533)
at org.springframework.integration.file.remote.RemoteFileTemplate.access$500(RemoteFileTemplate.java:59)
at org.springframework.integration.file.remote.RemoteFileTemplate$1.doInSession(RemoteFileTemplate.java:305)
... 19 more
Caused by: java.io.IOException: Failed to write to '/home/eds/provaVPC.txt.writing'. Server replied with: 500 Illegal PORT command.
at org.springframework.integration.ftp.session.FtpSession.write(FtpSession.java:123)
at org.springframework.integration.file.remote.RemoteFileTemplate.sendFileToRemoteDirectory(RemoteFileTemplate.java:505)
... 21 more
So it seems that, from this Linux environment, it can't send the file because the obtained exception is:
Caused by: org.springframework.messaging.MessagingException: Failed to write to '/home/eds/provaVPC.txt.writing' while uploading the file; nested exception is java.io.IOException: Failed to write to '/home/eds/provaVPC.txt.writing'. Server replied with: 500 Illegal PORT command.
How is it possible if from the Windows environment works fine. I think that the problem is not in the File object creation from a textual file on the file system but it happens when this File object is sended a Message to my FTP server. But why? What can be the cause of this problem? How can I try to solve this issue? What am I missing?
You need to switch to passive mode, because DefaultFtpSessionFactory use active mode by default. In your config use clientMode 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.