文章出處
文章列表
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; public class Utility { private static Logger logger = Logger.getLogger(Utility.class); private static Integer iCode = Integer.valueOf(1020); public static HashMap<String, String> getParamByRequest(String sParam, String sValueSplit, String sParamSplit) { logger.debug("傳遞過來的參數 => " + sParam); HashMap map = new HashMap(); if ((sParam == null) || ("".equals(sParam))) { logger.warn("Class=>Utility Method=>getParamByRequest"); logger.warn("傳遞的Request參數字符串為NULL或空"); return map; } if ((sValueSplit == null) || ("".equals(sValueSplit))) { logger.warn("Class=>Utility Method=>getParamByRequest"); logger.warn("變量與值的分隔符號為NULL或空"); return map; } if ((sParamSplit == null) || ("".equals(sParamSplit))) { logger.warn("Class=>Utility Method=>getParamByRequest"); logger.warn("變量與變量之間的分隔符號為NULL或空"); return map; } String[] sArgs = sParam.split(sParamSplit); for (int i = 0; (sArgs != null) && (i < sArgs.length); i++) { String s = sArgs[i]; String[] sVars = s.split(sValueSplit); if ((sVars != null) && (sVars.length > 1)) { String name = sVars[0]; String value = sVars[1]; map.put(name, value); } } return map; } public static String parseString(Boolean b) { String s; String s; if (b.booleanValue()) s = "1"; else { s = "0"; } return s; } public static List getFileListByPath(String sPath) { List list = new ArrayList(); if ((sPath == null) || ("".equals(sPath))) return list; try { File root = new File(sPath); File[] fileList = root.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isHidden()) { continue; } if (fileList[i].isDirectory()) { getChildFileListByPath(fileList[i].getPath(), list); } if (fileList[i].isFile()) { String name = fileList[i].getAbsolutePath(); int index = name.lastIndexOf(File.separator); int lastIndex = 0; if ("\\".equals(File.separator)) index++; else if ("//".equals(File.separator)) { index += 2; } if ((lastIndex = name.lastIndexOf(".jsp")) > 0) { name = name.substring(index); if ((name.indexOf("attributes_DW") != -1) || (name.indexOf("filters_DW") != -1) || (name.indexOf("buttons_DW") != -1) || (name.indexOf("dataWindow_DW") != -1)) continue; Map map = new HashMap(); map.put("name", name); map.put("value", fileList[i].getAbsolutePath()); list.add(map); } } } } catch (NullPointerException npe) { logger.error("NullPointerException \n" + npe.getMessage()); } catch (SecurityException se) { logger.error("SecurityException \n" + se.getMessage()); } return list; } private static void getChildFileListByPath(String sParentPath, List<Map<String, String>> list) { try { File root = new File(sParentPath); File[] fileList = root.listFiles(); for (int i = 0; i < fileList.length; i++) { if (fileList[i].isHidden()) { continue; } if (fileList[i].isDirectory()) { getChildFileListByPath(fileList[i].getPath(), list); } if (fileList[i].isFile()) { String name = fileList[i].getAbsolutePath(); int index = name.lastIndexOf(File.separator); int lastIndex = 0; if ("\\".equals(File.separator)) index++; else if ("//".equals(File.separator)) { index += 2; } if ((lastIndex = name.lastIndexOf(".jsp")) > 0) { name = name.substring(index); if ((name.indexOf("attributes_DW") != -1) || (name.indexOf("filters_DW") != -1) || (name.indexOf("buttons_DW") != -1) || (name.indexOf("dataWindow_DW") != -1)) continue; Map map = new HashMap(); map.put("name", name); map.put("value", fileList[i].getAbsolutePath()); list.add(map); } } } } catch (NullPointerException npe) { logger.error("NullPointerException \n" + npe.getMessage()); } catch (SecurityException se) { logger.error("SecurityException \n" + se.getMessage()); } } public byte[] convertObjectToByteArray(Object obj) { byte[] b = (byte[])null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out); oos.writeObject(obj); b = out.toByteArray(); oos.flush(); } catch (IOException e) { logger.error("把對象串行化為字節數組時出錯"); logger.error(e.getMessage()); e.printStackTrace(); } return b; } public Object convertByteArrayToObject(byte[] b) { Object obj = null; try { ByteArrayInputStream in = new ByteArrayInputStream(b); ObjectInputStream ois = new ObjectInputStream(in); obj = ois.readObject(); ois.close(); in.close(); } catch (Exception e) { logger.error("把字節數組反串行化為對象時出錯"); logger.error(e.getMessage()); } return obj; } }
HashMap<String, String> map = Utility.getParamByRequest(param, "=", "###");
param="a=av###b=bv###c=cv"
分割成map (a,av) (b,bv) (c,cv)
文章列表
全站熱搜