-1

我试图将以下命令行目录列表公开为带有纯文本的 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();


}
}
4

1 回答 1

1

您正在从 showDir 返回 null。您的所有输出都被发送到标准输出。

于 2013-10-01T20:14:22.107 回答