![](/img/trans.png)
[英]how to download file from sftp server using spring integration without using spring boot
[英]Issue in reading a excel file from SFTP server using spring integration
我是 Spring 集成的新手,所以對專家來說可能很傻。
我想每天早上讀取一個 SFTP 服務器上的 excel 文件(現在我設置為每分鍾讀取一次,如下所示)。 我已經編寫了所有接收 excel 輸入流並使用 POI 遍歷它的代碼。
如果我像從 JSP 上傳一樣手動提供文件,那么沒問題,一切正常。
但是當我使用下面的代碼啟用 spring 集成時,它會給我錯誤。
我發現“DefaultSftpSessionFactory”對“jsch-0.1.51.jar”有內部依賴性,並且它也被捆綁為我的工件的一部分。
我正在使用 spring 4.0.9。
僅供參考,直到我的“processFTPFile”方法才能控制。
<bean id="ultiProSftpSessionFactory" class="org.springframework.integration.sftp.session.DefaultSftpSessionFactory">
<property name="host" value="serverhost" />
<property name="user" value="user" />
<property name="password" value="password" />
</bean>
<int-ftp:inbound-channel-adapter id="ftpInboundChannelAdapter" session-factory="ultiProSftpSessionFactory" channel="fileAvailableChannel"
filename-pattern="* Consolidated Report.xlsx"
remote-directory="/folder1/folder2"
preserve-timestamp="true"
local-directory="."
auto-create-local-directory="true"
delete-remote-files="false"
auto-startup="true">
<int:poller cron="0 0/1 * * * *">
</int:poller>
</int-ftp:inbound-channel-adapter>
<int:service-activator id="serviceUpdateTrackingItem" method="processFTPFile" ref="cAPunchDataAdapter" input-channel="fileAvailableChannel" />
<int:publish-subscribe-channel id="fileAvailableChannel"/>
我想在每個文件上執行的代碼
@Component("cAPunchDataAdapter")
public class CAPunchDataAdapter {
public void processFTPFile(FTPFile file) {
//Code goes here to get inputstream of file.
}
}
下面是錯誤日志。
[3/4/19 18:20:00:731 EST] 00002560 SystemErr R [ERROR] LoggingHandler - -org.springframework.messaging.MessagingException: Problem occurred while synchronizing remote to local directory
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:209)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizingMessageSource.receive(AbstractInboundFileSynchronizingMessageSource.java:167)
at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:144)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:192)
at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298)
at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49)
at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:292)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:522)
at java.util.concurrent.FutureTask.run(FutureTask.java:277)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:191)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1153)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.lang.Thread.run(Thread.java:785)
Caused by: org.springframework.messaging.MessagingException: Failed to execute on session
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:321)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.synchronizeToLocalDirectory(AbstractInboundFileSynchronizer.java:167)
... 20 more
Caused by: java.lang.ClassCastException: com.jcraft.jsch.ChannelSftp$LsEntry incompatible with org.apache.commons.net.ftp.FTPFile
at org.springframework.integration.ftp.filters.FtpSimplePatternFileListFilter.getFilename(FtpSimplePatternFileListFilter.java:29)
at org.springframework.integration.file.filters.AbstractSimplePatternFileListFilter.accept(AbstractSimplePatternFileListFilter.java:49)
at org.springframework.integration.file.filters.AbstractFileListFilter.filterFiles(AbstractFileListFilter.java:40)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer.filterFiles(AbstractInboundFileSynchronizer.java:157)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer$1.doInSession(AbstractInboundFileSynchronizer.java:173)
at org.springframework.integration.file.remote.synchronizer.AbstractInboundFileSynchronizer$1.doInSession(AbstractInboundFileSynchronizer.java:167)
at org.springframework.integration.file.remote.RemoteFileTemplate.execute(RemoteFileTemplate.java:312)
... 21 more
JSch 用於sftp ,您正在使用ftp 。
你需要
<int-sftp:inbound-channel-adapter id="ftpInboundChannelAdapter" session-factory="ultiProSftpSessionFactory" channel="fileAvailableCh
( int-sftp:
, 不是int-ftp:
)
找到了修復程序,我沒想到會那樣。
取代
<int-ftp:inbound-channel-adapter
和
<int-sftp:inbound-channel-adapter
Spring 應該已經實現了更好的錯誤消息 :) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.