我试图将以下命令行目录列表公开为带有纯文本的 RestApi。它确实使用 system.out.println 在控制台中打印 c 目录列表,但未在 uri 页面上显示输出,它是空的。示例:
http://localhost:Project/show/dir
为空白。关于哪里出错的任何建议。下面是代码片段。
public class ShowDirectory {
String s=null;
public String showDir() {
try{
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("cmd /c dir");
BufferedReader input = new BufferedReader(new InputStreamReader
(pr.getInputStream()));
String line=null;
StringBuffer directory= new StringBuffer();
while((line=input.readLine()) != null) {
directory.append(line+ "\n");
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
return s;
}
}
服务等级:
@Path("show")
public class StartDirectory {
@GET
@Path("dir")
@Produces({"text/plain","application/xml","application/json"})
@Consumes({ "application/xml", "application/json",
"application/x-www-form-urlencoded" })
public String getLog(){
ShowDirectory status = new ShowDirectory();
return status.showDir();
}
}