-1
다음 명령 줄 디렉토리 목록을 일반 텍스트로 RestApi로 표시하려고 시도하고 있습니다. console.out.println을 사용하여 콘솔의 c 디렉토리 목록을 인쇄하지만 표시하지 않습니다. 그 empty.Example, URI를 페이지의 출력을 보여줍니다REST API로 명령 줄 디렉토리 목록을 표시하면 빈 페이지가 표시됨
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();
}
}
: 답장을 보내 주셔서 감사합니다. 조금 더 자세히 설명해 주시겠습니까? – user2821894
@ user2821894 당신은 반환 값's'을 덧붙이 지 않고'System.out.println'을 호출하고 있습니다. 그건 그렇고, 당신은 더 나은 성능을 위해'StringBuilder'를 사용해야합니다. 그러나 그것은 지금 당신의 문제 중 가장 적은 것입니다. –
: 나는 추가를 시도했지만 요청한 URI는 아직 빈 페이지를 표시하지 않습니다. 위의 코드를 수정했습니다. – user2821894