Chinaunix首页 | 论坛 | 博客
  • 博客访问: 18099
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
  • 认证徽章:
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-11 16:40:12

 wemall doraemon中Android app商城系统工具集合类,包含各种程序中用到的静态方法,可用于其他商城或者系统的编程参考

点击(此处)折叠或打开

  1. package cn.zzu.edu.wemall.utils;

  2. import java.io.File;
  3. import java.security.MessageDigest;
  4. import java.text.DecimalFormat;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.Random;

  8. import com.alibaba.fastjson.JSONArray;
  9. import com.alibaba.fastjson.JSONObject;

  10. import android.content.Context;
  11. import android.net.ConnectivityManager;
  12. import android.net.NetworkInfo;

  13. /*
  14.  *规范化数据类型
  15.  * 工具集合类,包含各种程序中用到的静态方法
  16.  *
  17.  * Edit By 幻舞奇影
  18.  *
  19.  */
  20. public class Utils {
  21.     // 判断是否处于联网状态
  22.     public static boolean isNetworkConnected(Context context) {
  23.         if (context != null) {
  24.             ConnectivityManager mConnectivityManager = (ConnectivityManager) context
  25.                     .getSystemService(Context.CONNECTIVITY_SERVICE);
  26.             NetworkInfo mNetworkInfo = mConnectivityManager
  27.                     .getActiveNetworkInfo();
  28.             if (mNetworkInfo != null) {
  29.                 return mNetworkInfo.isAvailable();
  30.             }
  31.         }
  32.         return false;
  33.     }

  34.     // 删除指定路径的文件夹
  35.     public static void delFolder(String folderPath) {
  36.         try {
  37.             delAllFile(folderPath); // 删除完里面所有内容
  38.             String filePath = folderPath;
  39.             filePath = filePath.toString();
  40.             java.io.File myFilePath = new java.io.File(filePath);
  41.             myFilePath.delete(); // 删除空文件夹
  42.         } catch (Exception e) {
  43.             e.printStackTrace();
  44.         }
  45.     }

  46.     // 删除指定路径文件夹及其所有内容
  47.     public static boolean delAllFile(String path) {
  48.         boolean flag = false;
  49.         File file = new File(path);
  50.         if (!file.exists()) {
  51.             return flag;
  52.         }
  53.         if (!file.isDirectory()) {
  54.             return flag;
  55.         }
  56.         String[] tempList = file.list();
  57.         File temp = null;
  58.         for (int i = 0; i < tempList.length; i++) {
  59.             if (path.endsWith(File.separator)) {
  60.                 temp = new File(path + tempList[i]);
  61.             } else {
  62.                 temp = new File(path + File.separator + tempList[i]);
  63.             }
  64.             if (temp.isFile()) {
  65.                 temp.delete();
  66.             }
  67.             if (temp.isDirectory()) {
  68.                 delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件
  69.                 delFolder(path + "/" + tempList[i]);// 再删除空文件夹
  70.                 flag = true;
  71.             }
  72.         }
  73.         return flag;
  74.     }

  75.     // 获取指定路径目录/文件的总大小(如目录包含目录下所有文件)
  76.     public static long getTotalSizeOfFilesInDir(String path) {
  77.         File file = new File(path);
  78.         if (file.isFile())
  79.             return file.length();
  80.         final File[] children = file.listFiles();
  81.         long total = 0;
  82.         if (children != null)
  83.             for (final File child : children)
  84.                 total += getTotalSizeOfFilesInDir(child.getPath());
  85.         return total;
  86.     }

  87.     // 大小格式化工具,传入long型值
  88.     public static String FomatFilesize(long size) {
  89.         /**
  90.          * 返回byte的数据大小对应的文本
  91.          *
  92.          * @param size
  93.          * @return
  94.          */
  95.         DecimalFormat formater = new DecimalFormat("####.00");
  96.         if (size < 1024) {
  97.             return size + "B";
  98.         } else if (size < 1024 * 1024) {
  99.             float kbsize = size / 1024f;
  100.             return formater.format(kbsize) + "KB";
  101.         } else if (size < 1024 * 1024 * 1024) {
  102.             float mbsize = size / 1024f / 1024f;
  103.             return formater.format(mbsize) + "MB";
  104.         } else if (size < 1024 * 1024 * 1024 * 1024) {
  105.             float gbsize = size / 1024f / 1024f / 1024f;
  106.             return formater.format(gbsize) + "GB";
  107.         } else {
  108.             return "size: error";

  109.         }
  110.     }

  111.     // list转json字符串
  112.     public static String ArrayListToJsonString(
  113.             ArrayList<HashMap<String, Object>> order) {
  114.         JSONArray json = (JSONArray) JSONArray.toJSON(order);
  115.         return json.toString();
  116.     }

  117.     // json字符串转arraylist
  118.     public static ArrayList<HashMap<String, String>> JsonStringToArrayList(
  119.             String data) {
  120.         JSONObject jsonobject = (JSONObject) JSONObject.parse("{order:" + data
  121.                 + "}");
  122.         ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
  123.         for (Object item : jsonobject.getJSONArray("order")) {
  124.             HashMap<String, String> thisitem = new HashMap<String, String>();
  125.             thisitem.put("name", ((JSONObject) item).get("name").toString());
  126.             thisitem.put("num", ((JSONObject) item).get("num").toString());
  127.             thisitem.put("price", ((JSONObject) item).get("price").toString());
  128.             thisitem.put("name", ((JSONObject) item).get("name").toString());
  129.             list.add(thisitem);
  130.         }
  131.         return list;
  132.     }

  133.     // MD5加密
  134.     public static String MD5(String inStr) {
  135.         MessageDigest md5 = null;
  136.         try {
  137.             md5 = MessageDigest.getInstance("MD5");
  138.         } catch (Exception e) {
  139.             System.out.println(e.toString());
  140.             e.printStackTrace();
  141.             return "";
  142.         }
  143.         char[] charArray = inStr.toCharArray();
  144.         byte[] byteArray = new byte[charArray.length];

  145.         for (int i = 0; i < charArray.length; i++)
  146.             byteArray[i] = (byte) charArray[i];

  147.         byte[] md5Bytes = md5.digest(byteArray);

  148.         StringBuffer hexValue = new StringBuffer();

  149.         for (int i = 0; i < md5Bytes.length; i++) {
  150.             int val = ((int) md5Bytes[i]) & 0xff;
  151.             if (val < 16)
  152.                 hexValue.append("0");
  153.             hexValue.append(Integer.toHexString(val));
  154.         }
  155.         return hexValue.toString();
  156.     }

  157.     // 获取6位随机26字母
  158.     public static String getRandomSix() {
  159.         Random rd = new Random();
  160.         String n = "";
  161.         int getNum;
  162.         do {
  163.             getNum = Math.abs(rd.nextInt()) % 26 + 97;// 产生字母a-z的随机数
  164.             char num1 = (char) getNum;
  165.             String dn = Character.toString(num1);
  166.             n += dn;
  167.         } while (n.length() < 6);
  168.         return n;
  169.     }

  170.     // 获取字符串的base值,用于网络传输避免乱码
  171.     public static String getBASE64(String s) {
  172.         if (s == null)
  173.             return null;
  174.         return (new Decoder.BASE64Encoder()).encode(s.getBytes());
  175.     }
  176.     //防止快速多次点击
  177.     public static boolean isFastDoubleClick() {
  178.         long lastClickTime = 0;
  179.         long time = System.currentTimeMillis();
  180.         long timeD = time - lastClickTime;
  181.         if ( 0 < timeD && timeD < 500) {
  182.             return true;
  183.         }
  184.         lastClickTime = time;
  185.         return false;
  186.     }

  187. }

 原文详情地址:http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

阅读(56) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册