import org.w3c.dom.*;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.Scanner;public classTomcatChangePort {static String shutdown_port="000";static String http_port="000";static String ajp_port="000";static int port_random=10240;//随机端口 会在 11240 -12240 随机获取
public static voidmain(String[] args) {
String jarFilePath= TomcatChangePort.class.getProtectionDomain().getCodeSource().getLocation().getFile();//URL Decoding
try{
jarFilePath= java.net.URLDecoder.decode(jarFilePath, "UTF-8");
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}if(jarFilePath.indexOf(":")>-1){
jarFilePath=jarFilePath.substring(jarFilePath.indexOf("/")+1,jarFilePath.lastIndexOf("/"));
}else{
jarFilePath=jarFilePath.substring(0,jarFilePath.lastIndexOf("/"));
}
String filePath=jarFilePath+"/conf/server.xml";
System.out.println(jarFilePath);
Document doc=getDocument(filePath);
getPort(doc);//获取
System.out.println("input new http port:");
Scanner scanner= new Scanner(System.in);int http_new_port=scanner.nextInt();if((""+http_new_port).equals(http_port)){ return;}
Random random= newRandom();int a=0;int b=0;while (true){if(a
a=random.nextInt(port_random+2000);
b=random.nextInt(port_random+2000);continue;
}break;
}
setPort(doc,http_new_port+"",a+"",b+"",filePath);//修改
}//读取 获取相对目录的tomcat/conf/server.xml中 shutdown端口 http端口 ajp端口
private static voidgetPort(Document doc){//获取根元素
Element element =doc.getDocumentElement();
shutdown_port=element.getAttribute("port");
NodeList list=element.getChildNodes();for (int iloop = 0; iloop < list.getLength(); iloop++) {
Node node=list.item(iloop);if("service".equalsIgnoreCase(node.getNodeName())){//循环serice
NodeList childNodes =node.getChildNodes();for (int i = 0; i < childNodes.getLength(); i++) {
Node item=childNodes.item(i);
NamedNodeMap attributes=node.getAttributes();if("connector".equalsIgnoreCase(item.getNodeName())){
String c_port="000";
boolean h_port=false;
boolean a_port=false;for (int j = 0; j < item.getAttributes().getLength(); j++) {
Node item1=item.getAttributes().item(j);if("port".equalsIgnoreCase(item1.getNodeName())){
c_port=item1.getNodeValue();
}if(item1.getNodeValue().indexOf("http")>-1||item1.getNodeValue().indexOf("HTTP")>-1){
h_port=true;
}if(item1.getNodeValue().indexOf("ajp")>-1||item1.getNodeValue().indexOf("AJP")>-1){
a_port=true;
}if("port".equalsIgnoreCase(item1.getNodeName())){
c_port=item1.getNodeValue();
}
}if(h_port){ http_port=c_port; }if(a_port){ ajp_port=c_port; }
}
}
}
}
System.out.println("old http port:"+http_port+"( shutdown port:"+shutdown_port+"ajp port:"+ajp_port+")");
}//修改
private static voidsetPort(Document doc,String http_new_port,String porta,String portb,String filePath){//获取根元素
Element element =doc.getDocumentElement();
element.setAttribute("port",porta);
NodeList list=element.getChildNodes();for (int iloop = 0; iloop < list.getLength(); iloop++) {
Node node=list.item(iloop);if("service".equalsIgnoreCase(node.getNodeName())){//循环serice
NodeList childNodes =node.getChildNodes();for (int i = 0; i < childNodes.getLength(); i++) {
Node item=childNodes.item(i);
NamedNodeMap attributes=node.getAttributes();if("connector".equalsIgnoreCase(item.getNodeName())){
boolean h_port=false;
boolean b_port=false;int port_j=-1;for (int j = 0; j < item.getAttributes().getLength(); j++) {
Node item1=item.getAttributes().item(j);if(item1.getNodeValue().indexOf("http")>-1||item1.getNodeValue().indexOf("HTTP")>-1){
h_port=true;
}if(item1.getNodeValue().indexOf("ajp")>-1||item1.getNodeValue().indexOf("AJP")>-1){
b_port=true;
}if("port".equalsIgnoreCase(item1.getNodeName())){
port_j=j;
}
}if(h_port){
list.item(iloop).getChildNodes().item(i).getAttributes().item(port_j).setNodeValue(http_new_port);
}if(b_port){
list.item(iloop).getChildNodes().item(i).getAttributes().item(port_j).setNodeValue(portb);
}
}
}
}
}
TransformerFactory transformerFactory=TransformerFactory.newInstance();
Transformer transformer=null;try{
transformer=transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT,"yse");
transformer.transform(new DOMSource(doc), new StreamResult(newFile(filePath)));
}catch(TransformerException e) {
e.printStackTrace();
}
}/**
获取xml文件的准备*/
public staticDocument getDocument(final String fileName) {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=null;
Document document=null;try{
builder=factory.newDocumentBuilder();
document=builder.parse(newFile(fileName));
}catch (ParserConfigurationException | SAXException |IOException e) {
e.printStackTrace();
}//document.setXmlStandalone(true);
returndocument;
}
}