Apache Commons exec交互模式

时间:2018-10-03 18:39:36

标签: java exiftool

我尝试使用选项stay_open和apache commons exec lib调用exiftool来处理多个请求。在输出中,您可以看到该处理有效,但是由于异步调用,getTags方法仅返回一个空字符串。如何获得返回预期结果的方法?

我已经尝试过thread.sleep和while循环,但是没有输出。仅当关闭主线程时,才调用ResultLogger回调。

感谢帮助。

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-exec</artifactId>
        <version>1.3</version>
    </dependency>

-

20:15:01.282 [main] INFO ExifToolProcess - Result -> 
20:15:02.105 [Exec Stream Pumper] INFO ExifToolProcess - Line -> [{
20:15:02.105 [Exec Stream Pumper] INFO ExifToolProcess - Line ->   "SourceFile": "test.mp4",
20:15:02.106 [Exec Stream Pumper] INFO ExifToolProcess - Line ->   "FileCreateDate": "2018:09:06 22:08:25+02:00"
20:15:02.106 [Exec Stream Pumper] INFO ExifToolProcess - Line -> }]
20:15:02.106 [Exec Stream Pumper] INFO ExifToolProcess - Line -> {ready}

-

import org.apache.commons.exec.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;


public class ExifToolProcess {

    private static Logger logger = LoggerFactory.getLogger(ExifToolProcess.class);

    private final ExecuteResultHandler resultHandler;
    private final PipedOutputStream processWriter;
    private final Executor executor;
    private final ResultLogger resultLogger;

    public ExifToolProcess() {
        executor = new DefaultExecutor();
        resultHandler = new DefaultExecuteResultHandler();
        processWriter = new PipedOutputStream();
        resultLogger = new ResultLogger();
        try {
            PipedInputStream in = new PipedInputStream(processWriter);

            CommandLine cmdLine = new CommandLine("PATH_TO\\exiftool.exe");
            cmdLine.addArgument("-json");
            cmdLine.addArgument("-FileCreateDate");
            cmdLine.addArgument("-stay_open");
            cmdLine.addArgument("True");
            cmdLine.addArgument("-@");
            cmdLine.addArgument("-");

            executor.setStreamHandler(new PumpStreamHandler(resultLogger, null, in));
            executor.execute(cmdLine, resultHandler);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getTags(File f) {
        try {
            processWriter.write(f.getAbsolutePath().concat("\n").getBytes());
            processWriter.write("-execute\n".getBytes());
            processWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return resultLogger.result;
    }

    public static void main(String[] args) {
        ExifToolProcess p = new ExifToolProcess();
        String res = p.getTags(new File("test.mp4"));

        logger.info("Result -> {}", res);
    }

    private class ResultLogger extends LogOutputStream {
        String result = "";
        @Override
        protected void processLine(String line, int logLevel) {
            logger.info("Line -> {}", line);
            result.concat(line);
        }
    }
}

0 个答案:

没有答案