import java.io.File;
import java.io.FileWriter;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
/**
* 页面静态化
* @author guyinyihun
*
*/
public class StaticizeUtil {
public static void jspIndexToStatic() {
try {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("http://127.0.0.1:9090/"); //首页访问路径
String path=StaticizeUtil.class.getClassLoader().getResource("").getPath();
String path2 = path.replace("/target/classes/", "/src/main/webapp/static/");
client.executeMethod(getMethod);
File file = new File(path2+"index.html");//存到webapp的static目录下
FileWriter writer = new FileWriter(file);
writer.write(getMethod.getResponseBodyAsString());
writer.flush();
System.err.println("首页静态化已完成...");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void thyIndexToStatic() {
try {
HttpClient client = new HttpClient();
GetMethod getMethod = new GetMethod("http://127.0.0.1:9004/");//首页访问路径
String path=StaticizeUtil.class.getClassLoader().getResource("").getPath();
String path2 = path.replace("/target/classes/", "/src/main/resources/templates/");
client.executeMethod(getMethod);
File file = new File(path2+"index.html");//存到/src/main/resources/templates 下
FileWriter writer = new FileWriter(file);
writer.write(getMethod.getResponseBodyAsString());
writer.flush();
System.err.println("首页静态化已完成...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
需引入依赖
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1-rc1</version>
</dependency>