简体   繁体   English

从sftp服务器读取文件

[英]Read file from sFtp server

I'm reading the file from sftp server and load that file into Database using Spring Batch Framework below is my code but im getting an Error 我正在从sftp服务器读取文件,并使用以下Spring Batch Framework将文件加载到数据库中,这是我的代码,但是我收到错误消息

Code: 码:

<bean id="cvsFileItemReaderMeta" class="org.springframework.batch.item.file.FlatFileItemReader">
    <!-- Read a csv file -->

    <property name="resource" value= "ftp://scmuser:scmuser%40123@172.18.228.32:22/home/scmuser/CSV/Meta.csv" />

Error: 错误:

org.springframework.batch.item.ItemStreamException: Failed to initialize the reader
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:142)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.item.ChunkMonitor.open(ChunkMonitor.java:115)
    at org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)
    at org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:306)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:192)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:137)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:60)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:152)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:131)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:135)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:301)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:134)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:49)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:127)
    at com.tcs.ceg.iforesee.service.DataLoader.loadMetaMapping(DataLoader.java:127)
    at com.tcs.ceg.iforesee.service.UploadFile.uploadFile(UploadFile.java:77)
    at com.tcs.ceg.iforesee.service.UploadFile.main(UploadFile.java:22)
Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode): URL [ftp://username:password@hostname.com:22/home/scmuser/CSV/Meta.csv]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:251)
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:139)

Caused by: java.lang.IllegalStateException: Input resource must exist (reader is in 'strict' mode) : URL [ ftp://username:password@hostname.com:22/home/scmuser/CSV/Meta.csv] 引起原因:java.lang.IllegalStateException: 输入资源必须存在(阅读器处于“严格”模式) :URL [ ftp:// username:password@hostname.com:22 / home / scmuser / CSV / Meta.csv]

Obviously the resource does not exist. 显然资源不存在。 You either have to ensure that the resource exists before attempting to open it or unset the reader's strict mode using FlatFileItemReader#setStrict and passing false 您必须先确保资源存在,然后再尝试打开它,或者使用FlatFileItemReader#setStrict并设置为false来取消设置读者的严格模式

In strict mode the reader will throw an exception on AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext) if the input resource does not exist. 在严格模式下,如果输入资源不存在,则读者将在AbstractItemCountingItemStreamItemReader.open(org.springframework.batch.item.ExecutionContext)上引发异常。

Another way to achive your goal and have a better error handling can be divide download+import in 2 step: 实现目标并更好地处理错误的另一种方法是将下载+导入分为2个步骤:

  1. Retrieve file from FTP (you can handle file missing using a dedicer/exit-status, incomplete file download, etc) 从FTP检索文件(您可以使用指示符/退出状态,不完整的文件下载等功能来处理丢失的文件)
  2. Import file from local without care about ftp transfer errors (broken connection in the middle of data import, for example) 从本地导入文件,无需担心ftp传输错误(例如,在数据导入过程中断开连接)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM