/* 

  * To change this template, choose Tools | Templates 

  * and open the template in the editor. 

  */ 

  package Servlets; 

  import Java.io.ByteArrayOutputStream; 

  import java.io.File; 

  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; 

  /** 

  * 

  * @author Administrator 

  */ 

  public class ToHtmlPath extends HttpServlet { 

  /** 

  * Processes requests for both HTTP GET and POST methods. 

  * @param request servlet request 

  * @param response servlet response 

  */ 

  protected void proce***equest(HttpServletRequest request, HttpServletResponse response) 

  throws ServletException, IOException { 

  } 

  @Override 

  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

  response.setContentType("text/html;charset=UTF-8"); 

  PrintWriter out = response.getWriter(); 

  String url =""; 

  String name =""; 

  ServletContext servletContext = getServletContext(); 

  final String CONTEXT_PATH=servletContext.getRealPath("/"); 

  String file_name = request.getParameter("file_name");// 你要访问的JSP文件,如news.jsf。 

  file_name=file_name+".jsp";  //目前只设置了jsp页面的生成。 

  // file_name如:fileDetail.jsf?fileId=56.要是有参数, 只有一个参数。并且以参数名作为文件名。 

  String build_name = request.getParameter("build_name");// 要生存的文件名。如index; 可以没有这个参数。 

  String path = request.getParameter("path"); // 你要访问的jsp文件路径。如news。可以没有这个参数。 

  String real_path = request.getParameter("real_path");// 你要生存的文件路径,如html-news.留意可以没有这个参数。 

  if (build_name==null || build_name.equals("")) { 

  int a = 0; 

  a = file_name.indexOf("=") + 1; 

  build_name = file_name.substring(a); 

  if (build_name.indexOf(".")>0) { 

  build_name = file_name.substring(0, file_name.indexOf(".")); 

  } 

  } 

  // 下面结构要访问的页面。 

  if (path ==null || path.equals("")) { 

  url = "/"+ file_name;// 这是你要生成HTML的jsp文件,如 

  } else { 

  url ="/" + path + File.separator + file_name;// 这是你要生成HTML的jsp文件,如 

  } 

  // 下面结构要生存的文件名,及路径。 

  // 1、如果有real_path,则生存在real_path下。 

  // 2、如果有path则生存在path下。 

  // 3、否则,生存在根目录下。 

  if (real_path == null || real_path.equals("")) { 

  if (path == null || path.equals("")) { 

  name =CONTEXT_PATH + File.separator + build_name + ".html";// 这是生成的html文件名,如index.htm.说明: ConfConstants.CONTEXT_PATH为你的上下文路径。 

  } else { 

  name =CONTEXT_PATH + File.separator + path + File.separator 

  + build_name + ".html";// 这是生成的html文件名,如index.htm. 

  } 

  } else { 

  name =CONTEXT_PATH + File.separator + real_path +File.separator 

  + build_name + ".html";// 这是生成的html文件名,如index.htm. 

  } 

  // 访问请求的页面,并生成指定的文件。 

  RequestDispatcher dispatcher = servletContext.getRequestDispatcher(url); //对这个URL请求 

  final ByteArrayOutputStream byteos = new ByteArrayOutputStream(); 

  final ServletOutputStream stream = new ServletOutputStream() {  //只是处置惩罚字节流,而PrintWriter则是处置惩罚字符流,和 

  @Override //重写了里面的方法 

  public void write(byte[] data, int offset, int length) { 

  byteos.write(data, offset, length); 

  } 

  @Override//重写了里面的方法 

  public void write(int b) throws IOException { 

  byteos.write(b); 

  } 

  }; 

  final PrintWriter printw; 

  printw= new PrintWriter(new OutputStreamWriter(byteos,"UTF-8")); //进行编码转换,当输出流从比特流转换为字符流的时候设置才是有效的。 

  HttpServletResponse rep = new HttpServletResponseWrapper(response) { 

  @Override 

  public ServletOutputStream getOutputStream() { 

  return stream; 

  } 

  @Override 

  public PrintWriter getWriter() { 

  return printw; 

  } 

  }; 

  dispatcher.include(request, rep); 

  printw.flush(); 

  FileOutputStream fileos = new FileOutputStream(name); // 把jsp输出的内容写到xxx.htm 

  //out.print(name); 

  byteos.writeTo(fileos); 

  fileos.close(); 

  out.print("发布成功!"); 

  } 

  //  

  /** 

  * Handles the HTTP GET method. 

  * @param request servlet request 

  * @param response servlet response 

  */ 

  @Override 

  protected void doGet(HttpServletRequest request, HttpServletResponse response) 

  throws ServletException, IOException { 

  proce***equest(request, response); 

  } 

  /** 

  * Handles the HTTP POST method. 

  * @param request servlet request 

  * @param response servlet response 

  */ 

  @Override 

  protected void doPost(HttpServletRequest request, HttpServletResponse response) 

  throws ServletException, IOException { 

  proce***equest(request, response); 

  } 

  /** 

  * Returns a short description of the servlet. 

  */ 

  @Override 

  public String getServletInfo() { 

  return "Short description"; 

  }//  

  }

本文引自:SQL吧|企业网站源码下载(www.sql8.net) 原文参考:http://www.sql8.net/a/course/chengxu/java/2013/0314/7036.html