使用 SystemCommandTasklet 拆分文件 [英] Using SystemCommandTasklet to split file

查看:41
本文介绍了使用 SystemCommandTasklet 拆分文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 SystemCommandTasklet 运行系统命令.使用下面的示例代码尝试了此操作,但出现错误.

我认为这是因为命令参数,但我无法修复它.

如果有帮助,我会很高兴.

参考例子;

错误详情;

<块引用>

CreateProcess error=2,系统找不到指定的文件"

代码示例;

 @Bean@StepScopepublic SystemCommandTasklet fileSplitterSystemCommandTasklet(@Value("#{jobParameters['file']}") File file) 抛出异常 {final String fileSeparator = System.getProperty(file.separator");String outputDirectory = file.getPath().substring(0, file.getPath().lastIndexOf(fileSeparator)) + fileSeparator + out"+ 文件分隔符;文件输出 = 新文件(输出目录);如果(!输出.存在()){输出.mkdir();}final String command = String.format(split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);var fileSplitterTasklet = new SystemCommandTasklet();fileSplitterTasklet.setCommand(command);fileSplitterTasklet.setTimeout(60000L);fileSplitterTasklet.setWorkingDirectory(outputDirectory);fileSplitterTasklet.setTaskExecutor(new SimpleAsyncTaskExecutor());fileSplitterTasklet.setSystemProcessExitCodeMapper(touchCodeMapper());fileSplitterTasklet.afterPropertiesSet();fileSplitterTasklet.setInterruptOnCancel(true);fileSplitterTasklet.setEnvironmentParams(新字符串[]{"JAVA_HOME=/java",BATCH_HOME=/Users/batch"});返回 fileSplitterTasklet;}

解决方案

您需要使用 file.getAbsolutePath() 而不是 file.getPath().>

此外,您在命令中使用了 file.getName():

final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);

您应该传递文件的绝对路径或确保设置正确设置工作目录,以便执行 split 命令在与文件相同的目录中.

I want to run System Commands via SystemCommandTasklet.Itried this with the sample code below but I get an error.

I think this because of command parameter,But I could not fix it.

I would be very glad if it will help.

Reference Examples ;

Error Detail ;

"CreateProcess error=2, The system cannot find the file specified"

Code Sample ;

 @Bean
@StepScope
public SystemCommandTasklet fileSplitterSystemCommandTasklet(@Value("#{jobParameters['file']}") File file) throws Exception {

    final String fileSeparator = System.getProperty("file.separator");
    String outputDirectory = file.getPath().substring(0, file.getPath().lastIndexOf(fileSeparator)) + fileSeparator + "out" + fileSeparator;

    File output = new File(outputDirectory);

    if (!output.exists()) {
        output.mkdir();
    }

    final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);

    var fileSplitterTasklet = new SystemCommandTasklet();
    fileSplitterTasklet.setCommand(command);
    fileSplitterTasklet.setTimeout(60000L);
    fileSplitterTasklet.setWorkingDirectory(outputDirectory);
    fileSplitterTasklet.setTaskExecutor(new SimpleAsyncTaskExecutor());
    fileSplitterTasklet.setSystemProcessExitCodeMapper(touchCodeMapper());
    fileSplitterTasklet.afterPropertiesSet();
    fileSplitterTasklet.setInterruptOnCancel(true);
    fileSplitterTasklet.setEnvironmentParams(new String[]{
            "JAVA_HOME=/java",
            "BATCH_HOME=/Users/batch"});
    return fileSplitterTasklet;
}

解决方案

You need to use file.getAbsolutePath() instead of file.getPath().

Also, you are using file.getName() in the command:

final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);

You should pass the absolute path of the file or make sure to set the working directory correctly so that the split command is executed in the same directory as the file.

这篇关于使用 SystemCommandTasklet 拆分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆