htmlgenerator.java

来自「cwbbs 云网论坛源码」· Java 代码 · 共 104 行

JAVA
104
字号
package com.cloudwebsoft.framework.test;import javax.servlet.*;import javax.servlet.http.*;import java.io.*;import cn.js.fan.util.ParamUtil;import java.util.Calendar;public class HTMLGenerator extends HttpServlet {    private static final String CONTENT_TYPE = "text/html; charset=utf-8";        public void init() throws ServletException {    }        public void doGet(HttpServletRequest request, HttpServletResponse response) throws            ServletException, IOException {        response.setContentType(CONTENT_TYPE);        service(request, response);    }        public void doPost(HttpServletRequest request, HttpServletResponse response) throws            ServletException, IOException {        doGet(request, response);    }    public void destroy() {    }    public void service(HttpServletRequest request,                        HttpServletResponse response) throws            ServletException, IOException {                String url = "";        String name = "";        ServletContext sc = getServletContext();        String file_name = request.getParameter("fileName");                 String p0 = ParamUtil.get(request, "p0");         String v0 = ParamUtil.get(request, "v0");         String p1 = ParamUtil.get(request, "p1");        String v1 = ParamUtil.get(request, "v1");        Calendar cal = Calendar.getInstance();        String year = "" + (cal.get(cal.YEAR));        String month = "" + (cal.get(cal.MONTH) + 1);        String filepath = "article/" + year + "/" + month;        url = "/" + file_name + ".jsp?" + p0 + "=" + v0;        if (!p1.equals(""))            url = url + "&" + p1 + "=" + v1;                String realPath = sc.getRealPath("/");        name = v0 + ".htm";         String visualPath = filepath + "/" + name;        String fullPath = realPath + visualPath;         System.out.println(getClass() + " url=" + url);        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,                "utf-8"));        HttpServletResponse rep = new HttpServletResponseWrapper(response) {            public ServletOutputStream getOutputStream() {                return stream;            }            public PrintWriter getWriter() {                return pw;            }        };        rd.include(request, rep);        pw.flush();        FileOutputStream fos = new FileOutputStream(fullPath);         os.writeTo(fos);        fos.close();        response.sendRedirect(visualPath);     }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?