mirror of
https://github.com/PowerJob/PowerJob.git
synced 2025-07-17 00:00:04 +08:00
解决win平台bat脚本中文路径执行乱码问题
This commit is contained in:
parent
973322370a
commit
0aa06d1ae6
@ -13,6 +13,7 @@ import tech.powerjob.official.processors.util.CommonUtils;
|
||||
|
||||
import java.io.*;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ForkJoinPool;
|
||||
@ -30,6 +31,7 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
||||
private static final ForkJoinPool POOL = new ForkJoinPool(4 * Runtime.getRuntime().availableProcessors());
|
||||
private static final Set<String> DOWNLOAD_PROTOCOL = Sets.newHashSet("http", "https", "ftp");
|
||||
protected static final String SH_SHELL = "/bin/sh";
|
||||
protected static final String CMD_SHELL = "cmd.exe";
|
||||
|
||||
private static final String WORKER_DIR = System.getProperty("user.home") + "/powerjob/worker/official_script_processor/";
|
||||
|
||||
@ -62,7 +64,9 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
||||
omsLogger.info("[SYSTEM] chmod 755 authorization complete, ready to start execution~");
|
||||
}
|
||||
// 2. 执行目标脚本
|
||||
ProcessBuilder pb = new ProcessBuilder(getRunCommand(), scriptPath);
|
||||
ProcessBuilder pb = StringUtils.equals(getRunCommand(), CMD_SHELL) ?
|
||||
new ProcessBuilder(getRunCommand(), "/c", scriptPath)
|
||||
: new ProcessBuilder(getRunCommand(), scriptPath);
|
||||
Process process = pb.start();
|
||||
|
||||
StringBuilder inputBuilder = new StringBuilder();
|
||||
@ -71,10 +75,12 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
||||
boolean success = true;
|
||||
String result;
|
||||
|
||||
//解决windows平台中文乱码
|
||||
Charset loggerCharset = SystemUtils.IS_OS_WINDOWS? Charset.forName("GBK"):StandardCharsets.UTF_8;
|
||||
try (InputStream is = process.getInputStream(); InputStream es = process.getErrorStream()) {
|
||||
|
||||
POOL.execute(() -> copyStream(is, inputBuilder, omsLogger));
|
||||
POOL.execute(() -> copyStream(es, errorBuilder, omsLogger));
|
||||
POOL.execute(() -> copyStream(is, inputBuilder, omsLogger,loggerCharset));
|
||||
POOL.execute(() -> copyStream(es, errorBuilder, omsLogger,loggerCharset));
|
||||
|
||||
success = process.waitFor() == 0;
|
||||
|
||||
@ -108,16 +114,25 @@ public abstract class AbstractScriptProcessor extends CommonBasicProcessor {
|
||||
}
|
||||
|
||||
// 非下载链接,为 processInfo 生成可执行文件
|
||||
try (FileWriter fw = new FileWriter(script); BufferedWriter bw = new BufferedWriter(fw)) {
|
||||
bw.write(processorInfo);
|
||||
bw.flush();
|
||||
if(SystemUtils.IS_OS_WINDOWS)
|
||||
{
|
||||
try (Writer fstream = new OutputStreamWriter(new FileOutputStream(script), Charset.forName("GBK")); BufferedWriter out = new BufferedWriter(fstream)) {
|
||||
out.write(processorInfo);
|
||||
out.flush();
|
||||
}
|
||||
}
|
||||
else {
|
||||
try (FileWriter fw = new FileWriter(script); BufferedWriter bw = new BufferedWriter(fw)) {
|
||||
bw.write(processorInfo);
|
||||
bw.flush();
|
||||
}
|
||||
}
|
||||
return scriptPath;
|
||||
}
|
||||
|
||||
private static void copyStream(InputStream is, StringBuilder sb, OmsLogger omsLogger) {
|
||||
private static void copyStream(InputStream is, StringBuilder sb, OmsLogger omsLogger, Charset charset) {
|
||||
String line;
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
|
||||
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, charset))) {
|
||||
while ((line = br.readLine()) != null) {
|
||||
sb.append(line);
|
||||
// 同步到在线日志
|
||||
|
@ -6,15 +6,15 @@ package tech.powerjob.official.processors.impl.script;
|
||||
* @author fddc
|
||||
* @since 2021/5/14
|
||||
*/
|
||||
public class PowerShellProcessor extends AbstractScriptProcessor {
|
||||
public class CMDProcessor extends AbstractScriptProcessor {
|
||||
|
||||
@Override
|
||||
protected String getScriptName(Long instanceId) {
|
||||
return String.format("powershell_%d.bat", instanceId);
|
||||
return String.format("cmd_%d.bat", instanceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRunCommand() {
|
||||
return "powershell.exe";
|
||||
return "cmd.exe";
|
||||
}
|
||||
}
|
||||
|
@ -10,7 +10,7 @@ public class PowerShellProcessor extends AbstractScriptProcessor {
|
||||
|
||||
@Override
|
||||
protected String getScriptName(Long instanceId) {
|
||||
return String.format("powershell_%d.bat", instanceId);
|
||||
return String.format("powershell_%d.ps1", instanceId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
x
Reference in New Issue
Block a user