⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sysutil.java

📁 天乙社区6.0是一套基于JAVA技术的网络虚拟社区
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.laoer.bbscs.sys;

import java.util.*;
import java.io.*;
import javax.servlet.http.*;
import java.net.*;
import org.apache.struts.util.*;
import org.apache.struts.upload.*;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import com.laoer.comm.util.*;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * <p>Title: TianYi BBS</p>
 * <p>Description: TianYi BBS System</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: LAOER.COM/TIANYISOFT.NET</p>
 * @author laoer
 * @version 6.0
 */

public class SysUtil {

  private static final Log logger = LogFactory.getLog(SysUtil.class);

  /**
   * Get Spring WebApplicationContext
   * @return WebApplicationContext
   */
  /*
     public static WebApplicationContext getWebApplicationContext() {
   return WebApplicationContextUtils.getRequiredWebApplicationContext(Constant.
        SC);
     }*/

  /**
   * Get Struts Text Message
   * @param key String
   * @param request HttpServletRequest
   * @return String
   */
  public static String getMsg(String key, HttpServletRequest request) {
    return Constant.MESSAGE.getMessage(request.getLocale(), key);
  }

  public static String[] getPagesign(MessageResources messages,
                                     HttpServletRequest request) {
    String[] pagessign = {
        messages.getMessage(request.getLocale(), "bbscs.pages.first"),
        messages.getMessage(request.getLocale(), "bbscs.pages.previous"),
        messages.getMessage(request.getLocale(), "bbscs.pages.next"),
        messages.getMessage(request.getLocale(), "bbscs.pages.end")};
    return pagessign;
  }

  public static String[] getPagesign(HttpServletRequest request) {
    String[] pagessign = {
        Constant.MESSAGE.getMessage(request.getLocale(), "bbscs.pages.first"),
        Constant.MESSAGE.getMessage(request.getLocale(), "bbscs.pages.previous"),
        Constant.MESSAGE.getMessage(request.getLocale(), "bbscs.pages.next"),
        Constant.MESSAGE.getMessage(request.getLocale(), "bbscs.pages.end")};
    return pagessign;
  }

  public static Vector getUserType0(HttpServletRequest request) {
    Vector v = new Vector();
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "bbscs.user.guest"), "0"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "bbscs.user.user"), "1"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "bbscs.user.usermanager"), "2"));
    return v;
  }

  public static Vector getUserType1(HttpServletRequest request) {
    Vector v = new Vector();
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "bbscs.user.guest"), "0"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "bbscs.user.user"), "1"));
    return v;
  }

  public static Vector getBoardAttrib(HttpServletRequest request) {
    Vector v = new Vector();
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "admin.board.cb"), "1"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "admin.board.eb"), "2"));
    return v;
  }

  public static Vector getBoardsUseStat(HttpServletRequest request) {
    Vector v = new Vector();
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "admin.boards.usestat.yes"), "1"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "admin.boards.usestat.no"), "2"));
    return v;
  }

  public static Vector getNoteType(HttpServletRequest request) {
    Vector v = new Vector();
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "note.nstyle0"), "0"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "note.nstyle1"), "1"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "note.nstyle2"), "2"));
    v.add(new LabelValueBean(Constant.MESSAGE.getMessage(request.getLocale(),
        "note.nstyle3"), "3"));
    return v;
  }

  public static String getNoteType(short mtype, HttpServletRequest request) {
    String type = "";
    switch (mtype) {
      case 0:
        type = Constant.MESSAGE.getMessage(request.getLocale(), "note.nstyle0");
        break;
      case 1:
        type = Constant.MESSAGE.getMessage(request.getLocale(), "note.nstyle1");
        break;
      case 2:
        type = Constant.MESSAGE.getMessage(request.getLocale(), "note.nstyle2");
        break;
      case 3:
        type = Constant.MESSAGE.getMessage(request.getLocale(), "note.nstyle3");
        break;
      default:
        type = Constant.MESSAGE.getMessage(request.getLocale(), "note.nstyle0");
        break;
    }
    return type;
  }

  public static Vector getForbid(HashMap forbid) {
    Iterator i = forbid.values().iterator();
    Vector v = new Vector();
    String s;
    while (i.hasNext()) {
      s = (String) i.next();
      v.add(new LabelValueBean(s, s));
    }
    return v;
  }

  public static long getLongTime() {
    return System.currentTimeMillis();
  }

  public static String getResultUrl(String msg, String tourl) {
    if (tourl == null || tourl.length() == 0 ||
        tourl.equals(Constant.GOBACK)) {
      tourl = "tourl=" + Constant.GOBACK;
    }
    else {
      try {
        tourl = "tourl=" + java.net.URLEncoder.encode(tourl, Constant.CHARSET);
      }
      catch (Exception e) {
      }
    }
    return "/result.jsp?" + "msg=" + msg + "&" + tourl;
  }

  public static UserLevel getUserLevel(int exp) {
    List l = SysInfo.getInstance().getUserlevel();
    UserLevel ul;
    for (int i = 0; i < l.size(); i++) {
      ul = (UserLevel) l.get(i);
      if (ul.getMin() <= exp && exp <= ul.getMax()) {
        return ul;
      }
    }
    return (UserLevel) l.get(0);
  }

  public static String getIncludeFilePath() {
    String filePath = Constant.ROOTPATH + "file/include/";
    File ft = new File(filePath);
    if (!ft.exists()) {
      ft.mkdirs();
    }
    return filePath;
  }

  public static String getUserFilePath(String username, long userID) {
    int num = Math.abs(username.hashCode());
    String filePath = "";
    filePath = Constant.ROOTPATH + "file/user/" + (num % 100) + "/" + userID +
        "/";
    File ft = new File(filePath);
    if (!ft.exists()) {
      ft.mkdirs();
    }
    return filePath;
  }

  public static String getUserFileWebPath(String username, long userID) {
    int num = Math.abs(username.hashCode());
    String filePath = "";
    filePath = "file/user/" + (num % 100) + "/" + userID + "/";
    return filePath;
  }

  public static String getForumPath(long bid, long adate) {
    int num = Math.abs( (String.valueOf(bid)).hashCode());
    String filePath = "";
    filePath = Constant.ROOTPATH + "file/postfile/" + (num % 20) + "/" + bid +
        "/" + Util.formatDate4(new Date(adate)) + "/";
    File ft = new File(filePath);
    if (!ft.exists()) {
      ft.mkdirs();
    }
    return filePath;
  }

  public static String getUpFilePath(long bid, long adate) {
    int num = Math.abs( (String.valueOf(bid)).hashCode());
    String filePath = "";
    filePath = Constant.ROOTPATH + "file/imgup/" + (num % 20) + "/" + bid +
        "/" + Util.formatDate4(new Date(adate)) + "/";
    File ft = new File(filePath);
    if (!ft.exists()) {
      ft.mkdirs();
    }
    return filePath;
  }

  public static String getUpFileWebPath(long bid, long adate) {
    int num = Math.abs( (String.valueOf(bid)).hashCode());
    String filePath = "";
    filePath = "file/imgup/" + (num % 20) + "/" + bid + "/" +
        Util.formatDate4(new Date(adate)) + "/";
    return filePath;
  }

  public static void saveUpFile(String path, FormFile upfile) {
    try {
      InputStream stream = upfile.getInputStream();
      OutputStream bos = new FileOutputStream(path);
      int bytesRead = 0;
      byte[] buffer = new byte[8192];
      while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {
        bos.write(buffer, 0, bytesRead);
      }

      /*
             int len = stream.available();
             byte[] buffer = new byte[len];
             while ( (bytesRead = stream.read(buffer, 0, len)) != -1) {
        bos.write(buffer, 0, bytesRead);
             }*/
      bos.close();
      stream.close();
      bos = null;
      stream = null;
    }
    catch (FileNotFoundException ex) {
      logger.error(ex);
    }
    catch (IOException ex) {
      logger.error(ex);
    }
  }

  public static int getStrLength(String txt, String charset) {
    try {
      return txt.getBytes(charset).length;
    }
    catch (UnsupportedEncodingException ex) {
      return txt.length();
    }
  }

  public static short getCookieValue(String value, String perfix) {
    //if (value.length() == 2) {
    if (value.length() == 2 && value.startsWith(perfix)) {
      try {
        return Short.parseShort(value.substring(1, 2));
      }
      catch (NumberFormatException ex) {
        return (short) 0;
      }
    }
    return 0;
  }

  public static short getCookieValue(String value, String perfix,
                                     short defaultnum) {
    //if (value.length() == 2) {
    if (value.length() == 2 && value.startsWith(perfix)) {
      try {
        return Short.parseShort(value.substring(1, 2));
      }
      catch (NumberFormatException ex) {
        return (short) defaultnum;
      }
    }
    else if (value.length() > 2 && value.startsWith(perfix)) {
      try {
        return Short.parseShort(value.substring(1, value.length()));
      }
      catch (NumberFormatException ex) {
        return (short) defaultnum;
      }
    }
    return defaultnum;
  }

  public static short getGuestCookieValue(String value, String perfix) {
    //if (value.length() == 2) {
    if (value.length() == 2 && value.startsWith(perfix)) {
      try {
        return Short.parseShort(value.substring(1, 2));
      }
      catch (NumberFormatException ex) {
        return (short) 1;
      }
    }
    return 1;
  }

  public static String getClassName(String className, long userID) {
    className = Constant.BEANPERFIX + className + (userID % 10);
    return className;
  }

  public static String getForumClassName(long bid) {

⌨️ 快捷键说明

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