tomcat修改端口,idea生成jar文件

⒈tomcat修改端口, 需要改动tomcat/conf/server.xml中3个地方的端口

<Server port="8005" shutdown="SHUTDOWN">

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

⒉idea中单文件class生成jar文件

  

  

  

⒊jar文件放到tomcat/下就可以执行

链接:https://pan.baidu.com/s/18ioVIkdraF35MT1j_2wJ8w
提取码:i51o

 

⒋java原生操作dom小例子

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 class TomcatChangePort {

    static String shutdown_port="000";
    static String http_port="000";
    static String ajp_port="000";
    static int port_random=10240;// 随机端口 会在 11240 -12240 随机获取

    public static void main(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 = new Random();
        int a=0;int b=0;
        while (true){
            if(a<port_random+1000||b<port_random+1000||a==b){
                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 void getPort(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 void setPort(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(new File(filePath)));
        } catch (TransformerException e) {
            e.printStackTrace();
        }

    }

    /**
    获取xml文件的准备
     */
    public static Document getDocument(final String fileName) {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        DocumentBuilder builder=null;
        Document document=null;
        try {
            builder = factory.newDocumentBuilder();
            document=builder.parse(new File(fileName));
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
        //document.setXmlStandalone(true);
        return document;
    }


}
View Code

 

posted @ 2020-03-18 19:14  YaY-G  阅读(420)  评论(0编辑  收藏  举报