Welcome 布拉格

          BlogJava 首頁(yè) 聯(lián)系 聚合 管理
            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) {//若目錄下沒(méi)有文件則直接刪除
          ??????????????????? f.delete();
          ??????????????? } else {//若有則把文件放進(jìn)數(shù)組,并判斷是否有下級(jí)目錄
          ??????????????????? File delFile[] = f.listFiles();
          ??????????????????? int i = f.listFiles().length;
          ??????????????????? for (int j = 0; j < i; j++) {
          ??????????????????????? if (delFile[j].isDirectory()) {
          ??????????????????????????? rm(delFile[j].getAbsolutePath());//遞歸調(diào)用del方法并取得子目錄路徑
          ??????????????????????? }
          ??????????????????????? delFile[j].delete();//刪除文件
          ??????????????????? }
          ??????????????? }
          ??????????? }
          ??????? }
          ??? }
          }


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

          package util.web;

          /**
          ?* 頁(yè)面計(jì)數(shù)器
          ?* @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;

          ??? }

          ??? /**
          ???? * 獲取數(shù)據(jù)查詢(xún)?cè)L問(wèn)的第一條記錄
          ???? * @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("執(zhí)行時(shí)間:" + ttime + "秒");
          ??????? }
          ??????? //System.out.print(htmlCode);
          ??????? return htmlCode;
          ??? }

          ??? //??? 存儲(chǔ)文件
          ??? 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 閱讀(320) 評(píng)論(0)  編輯  收藏

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 五指山市| 体育| 晋江市| 吉安县| 陵水| 江阴市| 黎川县| 九龙坡区| 剑河县| 申扎县| 共和县| 浦县| 巴东县| 六安市| 四子王旗| 抚远县| 游戏| 临高县| 太仆寺旗| 余庆县| 安仁县| 三江| 宝兴县| 米泉市| 常州市| 宜君县| 隆尧县| 德钦县| 迁西县| 突泉县| 沐川县| 建水县| 故城县| 来安县| 七台河市| 陆川县| 芜湖县| 东方市| 灵宝市| 丰都县| 南平市|