package com.jetsum.util;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* 用于将jsp生产html静态页面
* @author ShaoJiang
*
*/
public class GenerateHtml extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置响应编码
response.setContentType("text/html; charset=UTF-8");
//获取响应流
PrintWriter out = response.getWriter();
try{
//生成html
generate(request, response);
//返回成功标识
out.print("1");
}catch(Exception e){
//出现异常返回失败标识
out.print("0");
}
}
/**
* 创建HTML
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @throws FileNotFoundException
*/
private void generate(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException,
FileNotFoundException {
//获得servlet上下文
ServletContext servletContext = getServletContext();
//获得要生成的jsp文件路径
String jspPath = request.getParameter("jspPath");
//获取jsp文件名
String jspName = jspPath.substring(jspPath.lastIndexOf("/")+1,jspPath.lastIndexOf("."));
//获得要保存的html文件路径
String htmlPath = request.getParameter("htmlPath");
//创建html文件名
String htmlName = htmlPath+"/"+jspName+".html";
//通过服务器端重定向获取jsp文件内容
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher(jspPath);
//创建自定义字节输出流
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final ServletOutputStream servletOutputStream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
byteArrayOutputStream.write(data, offset, length);
}
public void write(int b) throws IOException {
byteArrayOutputStream.write(b);
}
};
//创建自定义字符输出流
final PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream));
//创建自定义response对象
HttpServletResponse httpServletResponse = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return servletOutputStream;
}
public PrintWriter getWriter() {
return printWriter;
}
};
//包含当前响应对象
requestDispatcher.include(request, httpServletResponse);
//将文件内容写入到字节流
printWriter.flush();
//创建html文件
FileOutputStream fos = new FileOutputStream(htmlName);
//将字节流数据写入到html文件中
byteArrayOutputStream.writeTo(fos);
//关闭文件
fos.close();
}
}
package com.jetsum.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
public class CallGenerate {
private static String url;
public static void main(String[] args) {
url = "http://localhost:8080/jsp_template/GenerateHtml";
if(call("/test.jsp","E:")){
System.out.println("生成成功!");
}
}
/**
* 调用生成器
* @param jspPath
* @param htmlPath
*/
public static Boolean call(String jspPath,String htmlPath){
Map<String,String> map = new HashMap<String,String>();
map.put("jspPath",jspPath);
map.put("htmlPath",htmlPath);
String exc_result = post(url,map);
if(exc_result.trim().equals("0")){
return false;
}else{
return true;
}
}
/**
* 设置请求参数
*
* @param map
* @return
*/
private static List<NameValuePair> setHttpParams(Map<String, String> map) {
List<NameValuePair> formparams = new ArrayList<NameValuePair>();
Set<Map.Entry<String, String>> set = map.entrySet();
for (Map.Entry<String, String> entry : set) {
formparams.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
return formparams;
}
/**
* 发送post请求调用生成器
* @param url
* @param map
*/
private static String post(String url, Map<String, String> map) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
List<NameValuePair> formparams = setHttpParams(map);
HttpResponse response = null;
try {
UrlEncodedFormEntity param = new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(param);
response = httpclient.execute(httppost);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if(response==null){
return null;
}
String httpEntityContent = getEntityContentText(response);
httppost.abort();
return httpEntityContent;
}
/**
* 获得响应实体的文本
*
* @param response
* @return
*/
private static String getEntityContentText(HttpResponse response) {
InputStream is = getEntityContent(response);
BufferedReader br;
try {
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = br.readLine();
StringBuilder sb = new StringBuilder();
while (line != null) {
sb.append(line + "\n");
line = br.readLine();
}
br.close();
return sb.toString();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 获得响应实体的字节流
*
* @param response
* @return
*/
private static InputStream getEntityContent(HttpResponse response) {
HttpEntity entity = response.getEntity();
if (entity != null) {
try {
return entity.getContent();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void setUrl(String url) {
CallGenerate.url = url;
}
public static String getUrl() {
return url;
}
}