一、背景
我们在开发web程序时,端口占用情况时有发生,如何解决?
1.重启
(重启治百病~,),就是代价过大!
2.注销用户
ctrl+alt+del进入如下界面,选择注销用户后,重新登录!
3.Java代码方式
根据网上大佬写的代码,运行后,输入要sha死的端口后即可!B++
效果如下
代码如下:
packagecom.tab343.tools; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.util.ArrayList; importjava.util.HashSet; importjava.util.List; importjava.util.Scanner; importjava.util.Set; importjava.util.regex.Matcher; importjava.util.regex.Pattern; /*** 强杀端口号*/publicclassKillServer { privateSet<Integer>ports; publicstaticvoidmain(String[] args) throwsInterruptedException { System.out.println("请输入要杀掉的windows进程的端口号,如果有多个,则以逗号相隔"); System.out.println("Please input kill port"); Scannerscanner=newScanner(System.in); Stringinput=scanner.next(); scanner.close(); String[] split=input.split(","); Set<Integer>ports=newHashSet<Integer>(); for (Stringspid : split) { try{ intpid=Integer.parseInt(spid); ports.add(pid); }catch(Exceptione){ System.out.println("错误的端口号,请输入一个或者多个端口,以英文逗号隔开"); try { Thread.sleep(5000); } catch (InterruptedExceptione1) { e1.printStackTrace(); } System.exit(0); } } KillServerkill=newKillServer(); kill.ports=ports; System.out.println("need kill "+ports.size() +" num"); for (Integerpid : ports) { kill.start(pid); } System.out.println("清理完毕,程序即将退出"); System.out.println("SUCCESS"); Thread.sleep(5000); System.exit(0); } publicvoidstart(intport){ Runtimeruntime=Runtime.getRuntime(); try { //查找进程号Processp=runtime.exec("cmd /c netstat -ano | findstr \""+port+"\""); InputStreaminputStream=p.getInputStream(); List<String>read=read(inputStream, "UTF-8"); if(read.size() ==0){ System.out.println("找不到该端口的进程"); try { Thread.sleep(6000); System.exit(0); } catch (InterruptedExceptione) { e.printStackTrace(); } }else{ for (Stringstring : read) { System.out.println(string); } System.out.println("找到"+read.size()+"个进程,正在准备清理"); kill(read); } } catch (IOExceptione) { e.printStackTrace(); } } /*** 验证此行是否为指定的端口,因为 findstr命令会是把包含的找出来,例如查找80端口,但是会把8099查找出来* @param str* @return*/privatebooleanvalidPort(Stringstr){ Patternpattern=Pattern.compile("^ *[a-zA-Z]+ +\\S+"); Matchermatcher=pattern.matcher(str); matcher.find(); Stringfind=matcher.group(); intspstart=find.lastIndexOf(":"); find=find.substring(spstart+1); intport=0; try { port=Integer.parseInt(find); } catch (NumberFormatExceptione) { System.out.println("查找到错误的端口:"+find); returnfalse; } if(this.ports.contains(port)){ returntrue; }else{ returnfalse; } } /*** 更换为一个Set,去掉重复的pid值* @param data*/publicvoidkill(List<String>data){ Set<Integer>pids=newHashSet<Integer>(); for (Stringline : data) { intoffset=line.lastIndexOf(" "); Stringspid=line.substring(offset); spid=spid.replaceAll(" ", ""); intpid=0; try { pid=Integer.parseInt(spid); } catch (NumberFormatExceptione) { System.out.println("获取的进程号错误:"+spid); } pids.add(pid); } killWithPid(pids); } /*** 一次性杀除所有的端口* @param pids*/publicvoidkillWithPid(Set<Integer>pids){ for (Integerpid : pids) { try { Processprocess=Runtime.getRuntime().exec("taskkill /F /pid "+pid+""); InputStreaminputStream=process.getInputStream(); Stringtxt=readTxt(inputStream, "GBK"); System.out.println(txt); } catch (IOExceptione) { e.printStackTrace(); } } } privateList<String>read(InputStreamin,Stringcharset) throwsIOException{ List<String>data=newArrayList<String>(); BufferedReaderreader=newBufferedReader(newInputStreamReader(in, charset)); Stringline; while((line=reader.readLine()) !=null){ booleanvalidPort=validPort(line); if(validPort){ data.add(line); } } reader.close(); returndata; } publicStringreadTxt(InputStreamin,Stringcharset) throwsIOException{ BufferedReaderreader=newBufferedReader(newInputStreamReader(in, charset)); StringBuffersb=newStringBuffer(); Stringline; while((line=reader.readLine()) !=null){ sb.append(line); } reader.close(); returnsb.toString(); } }