2013-12-30 10:07:01
来 源
kejihao
Apache
本文介绍Java程序实现Apache动态配置域名,希望对于初学Apache服务器相关的朋友有帮助,更多Apache安装、配置、报错处理等资源请本站内搜索。

1     配本机域名

C:/WINDOWS/system32/drivers/etc下hosts 

127.0.0.1       localhost

127.0.0.1       www.a.com

127.0.0.1       www.b.com

127.0.0.1       www.c.com

2.配apache

C:/Program Files/Apache Software Foundation/Apache2.2/conf/extra下httpd-vhosts.conf(注意去掉httpd.conf加载httpd-vhosts.conf的#)。httpd.conf中加上NameVirtualHost *:80 (这里是一个ip多域名配置)

#begin-www.a.com

<VirtualHost *:80>

ServerName www.a.com

ServerAlias domain.tld *.domain.tld

DocumentRoot D:/a

<Directory "D:/a/">

AllowOverride None

Options None

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

#end-www.a.com

#begin-www.b.com

<VirtualHost *:80>

ServerName www.b.com

DocumentRoot D:/b

<Directory "D:/b/">

AllowOverride None

Options None

Order allow,deny

Allow from all

</Directory>

</VirtualHost>

#end-www.b.com

3.写重启apache的.bat文件(我放c:/1.bat)

@echo on

c:          

cd C:/Program Files/Apache Software Foundation/Apache2.2/bin

httpd -k restart

exit

4.动态添加域名的java类

import java.io.IOException;

import java.io.RandomAccessFile;

public class Example

{

/**

  * @param args

  */

 public static void main(String[] args)

 {

  try

  {

   RandomAccessFile file = new RandomAccessFile("C://Program Files//Apache Software Foundation//Apache2.2//conf//extra//httpd-vhosts.conf", "rw");

   long filePoint = 0;

   long fileLength = file.length();

   String str = "#begin-www.c.com"

+ "/r/n"

+ "<VirtualHost *:80>"

+ "/r/n"

+ "ServerName www.c.com"

+ "/r/n"

+ "DocumentRoot  D:/c"

+ "/r/n"

+ "<Directory /"D:/c//">"

+ "/r/n"

+ "AllowOverride None"

+ "/r/n"

+ "Options None"

+ "/r/n"

+ "Order allow,deny"

+ "/r/n"

+ "Allow from all"

+ "/r/n"

+ "</Directory>"

+ "/r/n"

+ "</VirtualHost>"

+ "/r/n"

+ "#end-www.c.com";

   while (filePoint < fileLength)

   {

String s = file.readLine();

if (s.startsWith("<VirtualHost"))

{

System.out.println(filePoint + "----------------------" + s);

}

filePoint = file.getFilePointer();

   }

   file.writeBytes(str);

   file.close();

  } catch (Exception e)

  {

  }

  Runtime runtime = Runtime.getRuntime();

  try

  {

   runtime.exec("cmd /C start C://1.bat");

  } catch (IOException e)

  {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

}

5.删除域名的java类(更改,先删除后增加)

package com.cn.apche;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.io.RandomAccessFile;

import org.apache.commons.digester.Digester;

public class Bo {

public static void main(String[] args) {

String filepath = "C://Program Files//Apache Software Foundation//Apache2.2//conf//extra//httpd-vhosts.conf";

StringBuffer buf = new StringBuffer();

  try{  

RandomAccessFile   file=new   RandomAccessFile(filepath,"rw");  

long   filePoint=0;  

long   fileLength=file.length();  

long [] point ;

String str = "<VirtualHost *:80>"+"/r/n"+

   "ServerName www.d.com"+"/r/n"+

   "DocumentRoot  D:/d"+"/r/n"+

"<Directory /"D:/d//">"+"/r/n"+

"AllowOverride None"+"/r/n"+

"Options None"+"/r/n"+

"Order allow,deny"+"/r/n"+

"Allow from all"+"/r/n"+

"</Directory>"+"/r/n"+

"</VirtualHost>";

point =  search("www.c.com",filepath);

if((point[1]-point[0])>0)

while(filePoint<fileLength)  

{

String   s=file.readLine();  

if(point[0]<=filePoint&&filePoint<=point[1])

{

   s ="";

System.out.println(filePoint+"----------------------"+s);

}

filePoint=file.getFilePointer(); 

buf.append(s+"/r/n");

}  

//    file.writeBytes(str);

file.close();  

}  

catch(Exception   e){}

File file =    new File(filepath);

if(file.exists())  file.delete();   

   try {

RandomAccessFile  mm = new RandomAccessFile(filepath, "rw");

mm.writeBytes(buf.toString());

System.out.println("++++++++++++++++--------"+buf.toString());

mm.close();

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

}

 public static long[] search(String name,String path){

long   start=0;

long   end=0;

long  result[]=new long[]{0,0};

  try{  

RandomAccessFile   file=new   RandomAccessFile(path,"rw");  

long   filePoint=0;

long   fileLength=file.length(); 

while(filePoint<fileLength)  

{

String   s=file.readLine();  

if(s.startsWith("#begin-"+name))

{

   start =filePoint;

// System.out.println(filePoint+"----------------------"+s);

}

if(s.startsWith("#end-"+name))

{

   end =file.getFilePointer();

//System.out.println(filePoint+"----------------------"+s);

}

filePoint=file.getFilePointer();  

}  

file.close(); 

}  

catch(Exception   e){}  

result[0] = start;

result[1] = end;

  return result;

 }

}

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。