/*
* Created on 2006-12-1
*/
package com.learndiary.website.util;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.learndiary.website.model.UserInfo;
public class ToHtml extends HttpServlet {
private static Log __log = LogFactory.getFactory().getInstance("com.learndiary.website.util.ToHtml");
public void service(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
String url = "";
String name = "";
ServletContext sc = getServletContext();
String fileName = request.getParameter("fileName");
String artID = request.getParameter("artID");
String pageNum = request.getParameter("pageNum");
String webPath = null;
if (fileName.equals("main")){
url = "/main.do";
webPath = "/index.htm";
__log.info("main url is: " + url +" ; static html file name is: " + webPath);
}else if(fileName.equals("goal")){
url = "/disGoalContentAction.do?goalID=" + artID; // 你要生成的页面的文件名。
webPath = "/goals/" + artID + ".htm";
__log.info("goal url is: " + url +" ; static html file name is: " + webPath);
}else if(fileName.equals("diary")){
url = "/disDiaryContentAction.do?goalID=" + artID; // 你要生成的页面的文件名。
webPath = "/diaries/" + artID + ".htm";
__log.info("diary url is: " + url +" ; static html file name is: " + webPath);
}else if(fileName.equals("sitemap")){
url = "/mapGenerateAction.do?artID=" + artID + "&pageNum=" + pageNum ; // 你要生成的页面的文件名。
int articleID = Integer.parseInt(artID);
if (articleID == 0){
webPath = "/sitemaps/goals-" + pageNum + ".htm";
}else if (articleID > 0){
webPath = "/sitemaps/goal" + artID + "-" + pageNum + ".htm";
}
__log.info("sitemap url is: " + url +" ; static html file name is: " + webPath);
}
name = request.getRealPath(webPath);
__log.info("real name is: " + name);
RequestDispatcher rd = sc.getRequestDispatcher(url);
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final ServletOutputStream stream = new ServletOutputStream() {
public void write(byte[] data, int offset, int length) {
os.write(data, offset, length);
}
public void write(int b) throws IOException {
os.write(b);
}
};
final PrintWriter pw = new PrintWriter(new OutputStreamWriter(os));
HttpServletResponse rep = new HttpServletResponseWrapper(response) {
public ServletOutputStream getOutputStream() {
return stream;
}
public PrintWriter getWriter() {
return pw;
}
};
rep.setContentType("text/html; charset=UTF-8");
rd.include(request, rep);
pw.flush();
FileOutputStream fos = new FileOutputStream(name);
byte[] ob= os.toByteArray();
//String string =new String(ob, "GB2312");//在本站如果输入繁体字等这样不能显示繁体字(应该是只能正确处理GB2312的内容)
String string =new String(ob);//这样才能正确处理繁体字
byte[] ob2 = string.getBytes("UTF-8");
fos.write(ob2);
//os.writeTo(fos);
fos.close();
String jspName = webPath.replaceAll(".htm", ".jsp");
if (!HtmlsManager.isExist(jspName, request)){ //write another same name's jsp static file that will include html file
FileOutputStream jspFos = new FileOutputStream(name.replaceAll(".htm", ".jsp"));
String content = "<%@ include file=\"/common/jsp301.jsp\" %>".concat("<jsp:include page=\"" + webPath + "\" />");
byte[] jspOb = content.getBytes("UTF-8");
jspFos.write(jspOb);
jspFos.close();
__log.info("wrote static file name in ToHtml.java is: " + jspName + ", and content is: " + content);
}
__log.info("wrote static file name in ToHtml.java is: " + name);
}
}
|