使用 java 代码将 war 文件远程部署到 Tomcat
Remotely deploy war file to Tomcat using java code
我正在尝试使用 Eclipse
中的 Tomcat 8
将 RESTful
网络服务部署到网络服务器。
我尝试使用 HttpClient
使用此 post 的第二个答案中的代码:Tomcat: remote programmatic deploy? 但我收到此异常
java.net.SocketException: Connection reset by peer: socket write error.
我也尝试使用 HttpURLConnection
使用此 post: how to upload, download file from tomcat server with username,password in swing 第一个答案中的代码,但我也收到错误.
可能是什么原因?还有别的办法吗?谢谢。
... it is in a different machine. I can use Tomcat Manager to deploy the web service but I would like to do it from java code with http PUT request.
为了使其成为可能,部署文件夹需要可从 HTTP 服务器或 Web 应用程序访问。出于安全原因,这通常不是一个好主意。
您仍然可以使用 Java(或其他语言)通过调用许多用于文件传输的实用程序中的任何一个来以编程方式完成此操作:ftp、scp、网络文件系统等
请注意,将工件(例如 war 文件)复制到 Tomcat 主机后,您可以告诉 Tomcat 通过部署管理器远程部署它 url。来自 documentation:
In this example the web application located in the directory /path/to/foo on the Tomcat server is deployed as the web application context named /footoo.
http://localhost:8080/manager/text/deploy?path=/footoo&war=file:/path/to/foo
In this example the ".war" file /path/to/bar.war on the Tomcat server is deployed as the web application context named /bar. Notice that there is no path parameter so the context path defaults to the name of the web application archive file without the ".war" extension.
http://localhost:8080/manager/text/deploy?war=jar:file:/path/to/bar.war!/
您的代码可以通过 scp(或其他方式)复制工件,如果成功,使用适当的参数调用管理器 URL。单个代码中的两步过程 运行.
非常感谢您的回答!有用!我使用 sftp 将文件上传到 Tomcat 服务器的 webapps 文件夹中。由于在 server.xml autodeploy=true 中,我不必执行 HTTP PUT 请求。这是我的代码,基于此 link:
String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "root";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "/home/username/apache-tomcat-8.0.23/webapps/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("..");
channelSftp.cd(SFTPWORKINGDIR);
File f = new File("path/to/war");
channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
ex.printStackTrace();
}
我正在尝试使用 Eclipse
中的 Tomcat 8
将 RESTful
网络服务部署到网络服务器。
我尝试使用 HttpClient
使用此 post 的第二个答案中的代码:Tomcat: remote programmatic deploy? 但我收到此异常
java.net.SocketException: Connection reset by peer: socket write error.
我也尝试使用 HttpURLConnection
使用此 post: how to upload, download file from tomcat server with username,password in swing 第一个答案中的代码,但我也收到错误.
可能是什么原因?还有别的办法吗?谢谢。
... it is in a different machine. I can use Tomcat Manager to deploy the web service but I would like to do it from java code with http PUT request.
为了使其成为可能,部署文件夹需要可从 HTTP 服务器或 Web 应用程序访问。出于安全原因,这通常不是一个好主意。
您仍然可以使用 Java(或其他语言)通过调用许多用于文件传输的实用程序中的任何一个来以编程方式完成此操作:ftp、scp、网络文件系统等
请注意,将工件(例如 war 文件)复制到 Tomcat 主机后,您可以告诉 Tomcat 通过部署管理器远程部署它 url。来自 documentation:
In this example the web application located in the directory /path/to/foo on the Tomcat server is deployed as the web application context named /footoo.
http://localhost:8080/manager/text/deploy?path=/footoo&war=file:/path/to/fooIn this example the ".war" file /path/to/bar.war on the Tomcat server is deployed as the web application context named /bar. Notice that there is no path parameter so the context path defaults to the name of the web application archive file without the ".war" extension.
http://localhost:8080/manager/text/deploy?war=jar:file:/path/to/bar.war!/
您的代码可以通过 scp(或其他方式)复制工件,如果成功,使用适当的参数调用管理器 URL。单个代码中的两步过程 运行.
非常感谢您的回答!有用!我使用 sftp 将文件上传到 Tomcat 服务器的 webapps 文件夹中。由于在 server.xml autodeploy=true 中,我不必执行 HTTP PUT 请求。这是我的代码,基于此 link:
String SFTPHOST = "1.2.3.4";
int SFTPPORT = 22;
String SFTPUSER = "root";
String SFTPPASS = "password";
String SFTPWORKINGDIR = "/home/username/apache-tomcat-8.0.23/webapps/";
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
JSch jsch = new JSch();
session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
session.setPassword(SFTPPASS);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
channelSftp.cd("..");
channelSftp.cd(SFTPWORKINGDIR);
File f = new File("path/to/war");
channelSftp.put(new FileInputStream(f), f.getName());
} catch (Exception ex) {
ex.printStackTrace();
}