Welcome 布拉格

BlogJava 首页 联系 聚合 管理
  6 Posts :: 13 Stories :: 15 Comments :: 0 Trackbacks

package util.web;

import java.io.*;

public class Util {
   
    /**
     * 删除文件
    */
    public static void rm(String filepath) throws IOException {
        File f = new File(filepath);//定义文件路径
        if (f.exists()) {//判断是文件还是目录
            if (f.isFile()) {
                f.delete();
            } else if (f.isDirectory()) {
                if (f.listFiles().length == 0) {//若目录下没有文件则直接删除
                    f.delete();
                } else {//若有则把文件放进数组,并判断是否有下级目录
                    File delFile[] = f.listFiles();
                    int i = f.listFiles().length;
                    for (int j = 0; j < i; j++) {
                        if (delFile[j].isDirectory()) {
                            rm(delFile[j].getAbsolutePath());//递归调用del方法并取得子目录路径
                        }
                        delFile[j].delete();//删除文件
                    }
                }
            }
        }
    }
}


<-@@@@@@@@@@@@@@@@@@@@@>

package util.web;

/**
 * 页面计数器
 * @author Dave
 */
public class Pagination {

    public static final int PAGE_SIZE = 15;

    private int totalPages = 1;

    private int currPage = 1;

    private int totalRecords = 0;

    private int firstRecord = 1;

    private int lastRecord = 1;

    private int pageSize = PAGE_SIZE;

    public Pagination(int firstRecoder, int pageSize) {
        this.firstRecord = firstRecoder;
        this.pageSize = pageSize;
    }

    public int getFirstRecord() {
        return firstRecord;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalRecords() {
        return totalRecords;
    }

    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
        if(totalRecords > 0){
            init();
        }
    }

    public int getCurrPage() {
        return currPage;
    }

    public int getLastRecord() {
        return lastRecord;
    }

    public int getTotalPages() {
        return totalPages;
    }

    private void init() {
        int test;
        test = totalRecords % pageSize;
        totalPages = test == 0 ? totalRecords / pageSize : totalRecords / pageSize + 1;

        if (firstRecord >= totalRecords) {
            firstRecord = (totalPages - 1) * pageSize + 1;
        }

        currPage = firstRecord / pageSize + 1;

        lastRecord = (firstRecord + pageSize) > totalRecords ? totalRecords : firstRecord + pageSize - 1;

    }

    /**
     * 获取数据查询访问的第一条记录
     * @return
     */
    public int getStartRow() {
        if(firstRecord<=1)
            return 0;
        return this.firstRecord - 1;
    }

    public String getHeader() {
        return "<script language=\"JavaScript\">writeHeader(" + firstRecord + "," + lastRecord + "," + totalRecords + "," + pageSize + "," + currPage + "," + totalPages + ")</script>";
    }

    public String getFooter() {
        return "<script language=\"JavaScript\">writeFooter(" + currPage + "," + totalPages + "," + firstRecord + "," + pageSize + "," + totalRecords + ")</script>";
    }

}
<#####################>
package util.web;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * @author Dave
 */
public class CacheMappingFilter implements Filter {

    //     ----------------------------------------------------- Instance Variables

    /**
     * The default character encoding to set for requests that pass through
     * this filter.
     */
    protected String enable = null;

    /**
     * The filter configuration object we are associated with. If this value
     * is null, this filter instance is not currently configured.
     */
    protected FilterConfig filterConfig = null;

    /**
     * Should a character encoding specified by the client be ignored?
     */
    protected boolean ignore = true;

    //     --------------------------------------------------------- Public Methods

    /**
     * Take this filter out of service.
     */
    public void destroy() {

        this.enable = null;
        this.filterConfig = null;

    }

    /**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * @param request The servlet request we are processing
     * @param result The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        //     Pass control on to the next filter
        //

        //request.setCharacterEncoding("utf-8");

        HttpServletRequest req = (HttpServletRequest) request;
        String path = req.getServletPath();
        String pageCacheEnable = (String)StaticValue.cacheMapping.get(path);
        if ((!path.startsWith("/admin")) && path.endsWith(".jsp")&&req.getParameter("makeCache")==null&&"true".equals(pageCacheEnable)) {
                path = path.replaceAll(".jsp", "_cache.html");
                  //  System.out.println(path);
                    //   System.out.println(req.getRequestURI());
                     //  System.out.println(req.getRequestURL());
                request.getRequestDispatcher("/cache"+path).forward(request, response);
        } else {
            chain.doFilter(request, response);
        }

    }

    /**
     * Place this filter into service.
     *
     * @param filterConfig The filter configuration object
     */
    public void init(FilterConfig filterConfig) throws ServletException {

        this.filterConfig = filterConfig;
        this.enable = filterConfig.getInitParameter("enable");

    }

}
<%%%%%%%%%%%>


package util.web;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

public class StaticHtmlCache {

    private static long star = 0;

    private static long end = 0;

    private static long ttime = 0;

    //    返回html代码
    public static String getHtmlCode(String httpUrl) {
        Date before = new Date();
        star = before.getTime();
        String htmlCode = "";
        try {
            InputStream in;
            URL url = new java.net.URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestProperty("User-Agent", "Mozilla/4.0");
            connection.connect();
            in = connection.getInputStream();
            java.io.BufferedReader breader = new BufferedReader(new InputStreamReader(in, "UTF8"));
            String currentLine;
            while ((currentLine = breader.readLine()) != null) {
                htmlCode = htmlCode + currentLine + '\n';
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Date after = new Date();
            end = after.getTime();
            ttime = end - star;
            System.out.println("执行时间:" + ttime + "秒");
        }
        //System.out.print(htmlCode);
        return htmlCode;
    }

    //    存储文件
    public static synchronized void writeHtml(String filePath, String info, boolean override,String encoding) {

        Writer out = null;
        File afile = new File(filePath);
        try {
            boolean isExit = afile.exists();
            if (isExit) {
                if (override){
                    afile.delete();
                    afile.createNewFile();
                }
            } else {
                afile.createNewFile();
            }
           

            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(afile), encoding));
            out.write(info);
            out.flush();
           
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        } finally {
            try{out.close();}catch (Exception e) {
            }
        }
    }

    public static void main(String[] args) {
        String url = "http://localhost:18080/zzesweb/index.jsp";
        writeHtml("d:/in2.htm", getHtmlCode(url), true,"utf-8");
    }

}

posted on 2007-09-12 12:08 Welcome 阅读(313) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: