Atitit webshell java 实现 命令行输出读取问题总结 1.1. 读取组赛 或者读取了一部分。。使用cmd /c 模式,强制关闭刷新缓冲区 1 1.2. 乱码解决 1 1.3. /h

Atitit webshell java 实现 命令行输出读取问题总结

1.1. 读取组赛 或者读取了一部分。。使用cmd /c 模式,强制关闭刷新缓冲区 1

1.2. 乱码解决 1

1.3. /hislog/src/main/java/com/attilax/util/CliService.java 2

1.4. 目录转移转义问题 2

1.5. 管道符号问题 lastcmd="systeminfo | find \"内存\""; 2

1.6. 重定向 可以正常执行 2

1.7. 压缩解压 2

1.8. 指定当前目录方便搜素 3

2. 参考资料 3

1.1. 读取组赛 或者读取了一部分。。使用cmd /c 模式,强制关闭刷新缓冲区

String lastcmd = "cmd /c dir";

 

 

1.2. 乱码解决

public static String output_out_2str_ByIoutil(Process Process1, String charsetName) {

System.out.println("--start  getInputStream output");

InputStreamReader isr;

try {

isr = new InputStreamReader(Process1.getInputStream(), charsetName);

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

throw new RuntimeException(e);

}

String string = null;

try {

 string = IOUtils.toString(isr);

//List<String> li=IOUtils.readLines(isr);

//string = Joiner.on("\r\n").join(li);  

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

throw new RuntimeException(e);

}

return string;

}

1.3. /hislog/src/main/java/com/attilax/util/CliService.java

1.4. 目录转移转义问题

String lastcmd = "cmd /c dir c:\\0logs";

 

 

 

1.5. 管道符号问题 lastcmd="systeminfo | find \"内存\"";

貌似不能使用管道符,自己读取解析

 

1.6. 重定向 可以正常执行

 

cmd /c dir c:\0logs >dir.txt

 

 

 

1.7. 压缩解压

  "C:\Program Files\WinRAR\Rar.exe" a  c:\0logs\log23.rar C:\0wkspc\hislog\WebContent\log\

1.8. 指定当前目录方便搜素

lastcmd=" cmd.exe /c dir log4j*.proper* /s";

 

这个在console执行ok  cmd.exe /c dir log4j*.proper* /s

但是  process不能执行,报error=2

cmd变为strarryok了。。。

As mentioned you can't change the CWD of the JVM but if you were to launch another process using Runtime.exec() you can use the overloaded method that lets you specify the working directory. This is not really for running your Java program in another directory but for many cases when one needs to launch another program like a Perl script for example, you can specify the working directory of that script while leaving the working dir of the JVM unchanged.

See Runtime.exec javadocs

Specifically,

public Process exec(String[] cmdarray,String[] envp, File dir) throws IOException

where dir is the working directory to run the subprocess in

shareimprove this answer

Process p = null;

ProcessBuilder pb = new ProcessBuilder("do_foo.sh");

pb.directory(new File("/home"));

p = pb.start();

2. 参考资料

java - How to set working directory with ProcessBuilder - Stack Overflow.html

原文地址:https://www.cnblogs.com/attilax/p/15197628.html