StaxEventItemWriter错误 - org.springframework.batch.item.ItemStreamException:文件已存在:

我试图创建一个springboot批处理作业,它必须从队列中读取数据,并写入xml格式 . 我正在使用 StaxEventItemWriter 进行写入,并且在运行应用程序时出现以下错误 . 我坚信错误发生在itemwriter部分,因为使用相同的阅读器,我可以使用Filefileitemwriter写入平面文件 . 我无法找出我做错了什么 . 我真的很高兴你的帮助 .

[org.springframework.batch.core.step.AbstractStep.execute]在job importUserJob org.springframework.batch.item.ItemStreamException中遇到执行步骤step1的错误:文件已存在:[C:\ folder \ Sync_0.xml] at org.springframework.batch.item.util.FileUtils.setUpOutputFile(FileUtils.java:61)在org.springframework.batch.item.xml.StaxEventItemWriter.open(StaxEventItemWriter.java:428)在org.springframework.batch.item . xml.StaxEventItemWriter.open(StaxEventItemWriter.java:400)在org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)在org.springframework.batch.core.step.tasklet.TaskletStep.open( TaskletStep.java:310)org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)at org org.springframework.batch.core.job.flow.support.sta上的.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) te.tepState.handle(StepState.java:67)位于org.springframework.batch.core.job.flow的org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) . 在org.springframework.batch.core.job.AbstractJob.execute的org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)上的support.SimpleFlow.start(SimpleFlow.java:144) AbstractJob.java:306)在org.springframework.batch.core.launch.support.SimpleJobLauncher $ 1.run(SimpleJobLauncher.java:135)在org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)在org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)在sun.reflect.NativeMethodAccessorImpl.invoke(来源不明)在sun.reflect . 在org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)的java.lang.reflect.Method.invoke(未知来源)中删除了MetAccessorImpl.invoke(未知来源) t org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)org.springframework.batch.core.configuration.annotation .simpleBatchConfiguration $ PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208 )在com.sun.proxy . $ Proxy40.run(来源不明)在org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:216)在org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner . executeLocalJobs(JobLauncherCommandLineRunner.java:233)at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner . java:125)org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:119)org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:806)org.springframework.boot.SpringApplication .callRunners(SpringApplication.java:790)在org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:777)在org.springframework.boot.SpringApplication.run(SpringApplication.java:308)在org.springframework.boot . SpringApplication.run(SpringApplication.java:1191)在org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)

下面提供了项目撰写人的代码 .

@Bean
public ItemWriter<WorkerXML> staxEventItemWriter() throws XmlMappingException, Exception{
  StaxEventItemWriter<WorkerXML> staxEventItemWriter = new StaxEventItemWriter<WorkerXML>();

  try{

    XStreamMarshaller marshaller = new XStreamMarshaller(); 

    staxEventItemWriter.setName("StaxEventItemWriter");
          staxEventItemWriter.setResource("C://folder//Sync_0.xml"));

    staxEventItemWriter.setRootTagName("WorkerXML");
    staxEventItemWriter.setOverwriteOutput(false);
    staxEventItemWriter.setMarshaller(marshaller);
    staxEventItemWriter.setEncoding(StandardCharsets.UTF_8.toString());

    staxEventItemWriter.setVersion("XML");//    

    staxEventItemWriter.setSaveState(true);        //                             staxEventItemWriter.open(new ExecutionContext());

    ExecutionContext executionContext = new ExecutionContext();
    staxEventItemWriter.open(executionContext);

    }
    catch(Exception e){
        System.out.println("staxEventItemWriter " + e);
    }
    return staxEventItemWriter;
  }

  @Bean 
  public Marshaller marshaller() 
  { 
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(QVCWorkerXML.class);
    return marshaller;
  }

回答(0)