5

如何解决 LogOutputStream 中文乱码问题?

 2 years ago
source link: https://www.v2ex.com/t/847184
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  Java

如何解决 LogOutputStream 中文乱码问题?

  ubuntuGary · 5 小时 50 分钟前 · 130 次点击

不管是 org.apache.commons.exec 工具包,还是 org.zeroturnaround.exec 工具包,都会出现中文乱码问题,请问有办法解决吗? 例子: org.apache.commons.exec 工具包的 demo:

package utils.exec;

import cn.hutool.core.io.LineHandler;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.exec.*;

import java.util.concurrent.TimeUnit;

@Slf4j
public class LocalCommandUtils {
    private static final long TIMEOUT = 1;
    private static final TimeUnit TIME_UNIT = TimeUnit.DAYS;

    public static String execCmd(String cmd) {
        return execCmd(cmd, line -> {});
    }

    public static String execCmd(String cmd, LineHandler lineHandler) {
        return execCmd(cmd, lineHandler, TIMEOUT, TIME_UNIT);
    }

    public static String execCmd(String cmd, LineHandler lineHandler, @NonNull long timeout, @NonNull TimeUnit unit) {
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(unit.toMillis(timeout));
        StringBuilder output = new StringBuilder();
        try {
            CommandLine cmdLine = CommandLine.parse(cmd);
            executor.setWatchdog(watchdog);
            executor.setStreamHandler(new PumpStreamHandler(new LogOutputStream() {
                @Override
                protected void processLine(String line, int logLevel) {
                    lineHandler.handle(line);
                    output.append(output.length() > 0 ? "\n" : "").append(line);
                }
            }));
            executor.execute(cmdLine);
            log.info("exec command:[{}] output:[\n{}\n]", cmd, output);
        } catch (Exception e) {
            log.error("exec command:[{}] failed, output:[\n{}\n]", cmd, output, e);
        }
        return output.toString();
    }

    public static void main(String[] args) {
        execCmd("ping 127.0.0.1", log::info);
    }
}

org.zeroturnaround.exec 工具包的 demo:

package utils.ztexec;

import cn.hutool.core.io.LineHandler;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.zeroturnaround.exec.ProcessExecutor;
import org.zeroturnaround.exec.stream.LogOutputStream;

import java.util.concurrent.TimeUnit;

@Slf4j
public class LocalCommandUtils {
    private static final long TIMEOUT = 1;
    private static final TimeUnit TIME_UNIT = TimeUnit.DAYS;

    public static String execCmd(String cmd) {
        return execCmd(cmd, line -> {});
    }

    public static String execCmd(String cmd, LineHandler lineHandler) {
        return execCmd(cmd, lineHandler, TIMEOUT, TIME_UNIT);
    }

    public static String execCmd(String cmd, LineHandler lineHandler, @NonNull long timeout, @NonNull TimeUnit unit) {
        StringBuilder output = new StringBuilder();
        try {
            new ProcessExecutor()
                    .commandSplit(cmd)
                    .timeout(timeout, unit)
                    .redirectOutput(new LogOutputStream() {
                        @Override
                        protected void processLine(String line) {
                            lineHandler.handle(line);
                            output.append(output.length() > 0 ? "\n" : "").append(line);
                        }
                    })
                    .execute();
            log.info("exec command:[{}] output:[\n{}\n]", cmd, output);
        } catch (Exception e) {
            log.error("exec command:[{}] failed, output:[\n{}\n]", cmd, output, e);
        }

        return output.toString();
    }

    public static void main(String[] args) {
        execCmd("ping 127.0.0.1", log::info);
    }
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK