package com.yss.framework.api.util; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.math.BigDecimal; import java.util.Date; import java.util.Properties; import javax.servlet.http.HttpServletRequest; import com.yss.framework.api.logger.LogManager; /** * * <p> * Title: * </p> * <p> * Description: 純粹與web或者java相關的處理 * </p> * <p> * Copyright: Copyright (c) 2003 * </p> * <p> * Company: Ysstech * </p> * * @author alex * @version 1.0 */ public class SysUtil { // 應用服務器枚舉 public static enum WebServer { TOMCAT, WEBSPHERE, WEBLOGIC } // 服務端操做系統枚舉 public static enum OS { WINDOWS, LINUX, UNIX } /** * 返回操做類型枚舉 * * @return 操做類型枚舉 */ public static OS getOSName() { String os = System.getProperty("os.name"); // if (os.toLowerCase().indexOf("linux") >= 0) { return OS.LINUX; } else if (os.toLowerCase().indexOf("windows") >= 0) { return OS.WINDOWS; } else if (os.toLowerCase().indexOf("unix") >= 0) { return OS.UNIX; } else { return null; } // end if } /** * 以字節流方式獲取request內的數據,返回字節數組 * * @param request * @return 字節數組 * @throws IOException */ public static final byte[] getBytesFromClient(HttpServletRequest request) throws IOException { InputStream is = (InputStream) request.getInputStream(); // 請求字節輸入流 int size = request.getContentLength(); // 請求數據大小 int count = 0; // int lenread = 0; // if (size < 0) { return new byte[0]; } byte[] buffer = new byte[size]; // 緩衝大小 while (count < size) { // 用while循環考慮到斷線等因素一次沒法讀完。。。。 lenread = is.read(buffer, count, size - count); if (lenread < 0) { // 考慮可能存在的提早結束狀況,是否須要報錯? break; } // end if count += lenread; } // end while if (count == size) { return buffer; } byte[] buf = new byte[count]; // 考慮可能存在的提早結束狀況 System.arraycopy(buffer, 0, buf, 0, count); return buf; } /** * 以字節流方式獲取request內的數據,返回String * * @param request * @return 請求字符串 * @throws IOException */ public static final String getBytesStringFromClient( HttpServletRequest request) throws IOException { return new String(getBytesFromClient(request), "UTF-8"); } /** * 將指定的字節流轉換成字符串 * * @param byteRequest * byte類型的字節流 * @return * @throws IOException */ public static final String getBytesStringFromClient(byte[] byteRequest) throws IOException { return new String(byteRequest, "UTF-8"); } /** * 返回request對應的Web應用的Root部分地址,包含了/,用於提供相對路徑以組成URL * * @param request * @param bSlash * @return request對應的Web應用的Root部分地址 */ public static final String getURLRoot(HttpServletRequest request, boolean bSlash) { StringBuffer surl = request.getRequestURL(); // 請求URL String stmp = request.getServletPath(); // 請求路徑 surl.setLength(surl.length() - stmp.length() + (bSlash ? 1 : 0)); return surl.toString(); } /** * 返回request對應的Web應用的Root部分地址 * * @param request * HTTP Request * @return request對應的Web應用的Root部分地址 */ public static final String getURLRoot(HttpServletRequest request) { return getURLRoot(request, false); } public static boolean isBaseType(Class<?> clazz) { boolean flag = false; if (Integer.class.equals(clazz)) { flag = true; } else if (Double.class.equals(clazz)) { flag = true; } else if (Short.class.equals(clazz)) { flag = true; } else if (Long.class.equals(clazz)) { flag = true; } else if (Float.class.equals(clazz)) { flag = true; } else if (Date.class.equals(clazz)) { flag = true; } else if (String.class.equals(clazz)) { flag = true; } else if (java.sql.Date.class.equals(clazz)) { flag = true; } else if (BigDecimal.class.equals(clazz)) { flag = true; } else if (String[].class.equals(clazz)) { flag = true; } else { flag = clazz.isPrimitive(); } return flag; } public static Properties ReadProperties(String filePath) { File pFile = null; Properties prop = null; FileInputStream in = null; try { pFile = new File(filePath); in = new FileInputStream(pFile); if (in != null) { prop = new Properties(); prop.load(in); } } catch (Exception e) { LogManager.getLogger(SysUtil.class).log("文件讀取出錯!", e); } return prop; } }