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

本文总结了Java WebShell在实现命令行输出时遇到的问题及解决方案,包括读取组赛、乱码处理、目录转移转义、管道符号使用、重定向、压缩解压操作等,并提供了具体代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值