我目前正在尝试从Unix服务器检索JSON文件。
我使用JSch进行连接,效果很好。现在我的挑战是向服务器发送命令(或多或少几个cd和ls,这是一个非常简单的命令)。
问题是我收到了错误,而且我被困住了。
以下是servlet的代码:
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.System.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.lang.Runtime;
@WebServlet("/ConnexionRecette")
public class ConnexionRecette extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user = "xxxx";
String password = "xxxxxx!";
String host = "xxxxxx";
int port = 22;
// String remoteFile = "xxxxxxxxxx";
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html>");
out.println("<head><title>Connect to serv test</title></head>");
out.println("<body>");
out.println("<br>print avant try");
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
out.println("<br>Establishing Connection...");
session.connect();
out.println("<br>Connection established.");
out.println("<br>Crating SFTP Channel.");
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
out.println("<br>SFTP Channel created.");
out.println("<br> ********** Ok on serv, end of connection code, end of try. **********");
Runtime runtime = Runtime.getRuntime();
String[] args = { "cd .."};
final Process process = runtime.exec(args);
BufferedReader output = getOutput(process);
BufferedReader error = getError(process);
String line = "";
while((line = output.readLine()) != null) {
out.println(line);
}
while((line = error.readLine()) != null) {
out.println(line);
}
process.waitFor();
} catch (IOException e) {
out.println("<br>Print in catch IO");
out.println("<br>*********************<br>");
e.printStackTrace();
out.println("<br>*********************<br>");
} catch (InterruptedException e) {
out.println("<br>Print in catch Interrupted");
out.println("<br>*********************<br>");
out.println("<br>*********************<br>");
e.printStackTrace();
}
out.println("<br>print après le try catch");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
调用servlet时出错:
无法运行程序“ssh”:CreateProcess error = 2,Lefichierspécifiéestintrouvable
java.io.IOException: Cannot run program "cd ..": CreateProcess error=2, Le fichier spécifié est introuvable
at java.lang.ProcessBuilder.start(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at java.lang.Runtime.exec(Unknown Source)
at ConnexionRecette.doGet(ConnexionRecette.java:92)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
引起:java.io.IOException:CreateProcess error = 2,Lefichierspécifiéestintrouvable at java.lang.ProcessImpl.create(Native Method) 在java.lang.ProcessImpl。(未知来源) 在java.lang.ProcessImpl.start(未知来源)
提前致谢!
答案 0 :(得分:0)
java.io.IOException:无法运行程序“cd ..”:CreateProcess error = 2,Lefichierspécifiéestintrouvable
cd
不是一个程序。它是一个内部命令,用于更改正在运行的shell的当前目录。
每个正在运行的进程都有一个当前目录。启动外部程序无法更改。如果要更改代码的当前目录,则应使用由您使用的语言提供的库函数。我不了解Java,但是如果您搜索文档中的chdir
或changeDirectory
,我确信您找到了正确的方法。
另一方面,如果要更改刚刚打开的远程连接上的当前目录,则应该通过sftpChannel
对象发出命令,或者更可能的是,使用其中一种方法来更改远程目录。 (同样,这只是编程常识,我对Java或sftpChannel
)一无所知。