Bladeren bron

实现逻辑

zhuoning 3 jaren geleden
bovenliggende
commit
44a000e135

+ 284 - 0
huimv-eartag-location/src/main/java/com/huimv/location/dao/entity/BeaconEntity.java

@@ -0,0 +1,284 @@
+package com.huimv.location.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "beacon")
+public class BeaconEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    /**
+     * mac地址
+     */
+    @Column(name = "mac")
+    private String mac;
+
+    /**
+     * x坐标
+     */
+    @Column(name = "x")
+    private String x;
+
+    /**
+     * y坐标
+     */
+    @Column(name = "y")
+    private String y;
+
+    /**
+     * z坐标
+     */
+    @Column(name = "z")
+    private String z;
+
+    /**
+     * 电池电量
+     */
+    @Column(name = "battery")
+    private Integer battery;
+
+    /**
+     * 最后上传时间
+     */
+    @Column(name = "up_last_time")
+    private Long upLastTime;
+
+    @Column(name = "up_last_time2")
+    private String upLastTime2;
+
+    /**
+     * 时间戳
+     */
+    @Column(name = "timestamp")
+    private Timestamp timestamp;
+
+    /**
+     * 最后上传基站
+     */
+    @Column(name = "last_gateway")
+    private String lastGateway;
+
+    @Column(name = "user_data_ts")
+    private String userDataTs;
+
+    @Column(name = "user_data")
+    private String userData;
+
+    @Column(name = "rssi")
+    private String rssi;
+
+    @Column(name = "map_id")
+    private String mapId;
+
+    @Column(name = "zone_id")
+    private String zoneId;
+
+    @Column(name = "updated_at")
+    private String updatedAt;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    /**
+     * mac地址
+     */
+    public void setMac(String mac) {
+        this.mac = mac;
+    }
+
+    /**
+     * mac地址
+     */
+    public String getMac() {
+        return mac;
+    }
+
+    /**
+     * x坐标
+     */
+    public void setX(String x) {
+        this.x = x;
+    }
+
+    /**
+     * x坐标
+     */
+    public String getX() {
+        return x;
+    }
+
+    /**
+     * y坐标
+     */
+    public void setY(String y) {
+        this.y = y;
+    }
+
+    /**
+     * y坐标
+     */
+    public String getY() {
+        return y;
+    }
+
+    /**
+     * z坐标
+     */
+    public void setZ(String z) {
+        this.z = z;
+    }
+
+    /**
+     * z坐标
+     */
+    public String getZ() {
+        return z;
+    }
+
+    /**
+     * 电池电量
+     */
+    public void setBattery(Integer battery) {
+        this.battery = battery;
+    }
+
+    /**
+     * 电池电量
+     */
+    public Integer getBattery() {
+        return battery;
+    }
+
+    /**
+     * 最后上传时间
+     */
+    public void setUpLastTime(Long upLastTime) {
+        this.upLastTime = upLastTime;
+    }
+
+    /**
+     * 最后上传时间
+     */
+    public Long getUpLastTime() {
+        return upLastTime;
+    }
+
+    public void setUpLastTime2(String upLastTime2) {
+        this.upLastTime2 = upLastTime2;
+    }
+
+    public String getUpLastTime2() {
+        return upLastTime2;
+    }
+
+    /**
+     * 时间戳
+     */
+    public void setTimestamp(Timestamp timestamp) {
+        this.timestamp = timestamp;
+    }
+
+    /**
+     * 时间戳
+     */
+    public Timestamp getTimestamp() {
+        return timestamp;
+    }
+
+    /**
+     * 最后上传基站
+     */
+    public void setLastGateway(String lastGateway) {
+        this.lastGateway = lastGateway;
+    }
+
+    /**
+     * 最后上传基站
+     */
+    public String getLastGateway() {
+        return lastGateway;
+    }
+
+    public void setUserDataTs(String userDataTs) {
+        this.userDataTs = userDataTs;
+    }
+
+    public String getUserDataTs() {
+        return userDataTs;
+    }
+
+    public void setUserData(String userData) {
+        this.userData = userData;
+    }
+
+    public String getUserData() {
+        return userData;
+    }
+
+    public void setRssi(String rssi) {
+        this.rssi = rssi;
+    }
+
+    public String getRssi() {
+        return rssi;
+    }
+
+    public void setMapId(String mapId) {
+        this.mapId = mapId;
+    }
+
+    public String getMapId() {
+        return mapId;
+    }
+
+    public void setZoneId(String zoneId) {
+        this.zoneId = zoneId;
+    }
+
+    public String getZoneId() {
+        return zoneId;
+    }
+
+    public void setUpdatedAt(String updatedAt) {
+        this.updatedAt = updatedAt;
+    }
+
+    public String getUpdatedAt() {
+        return updatedAt;
+    }
+
+    @Override
+    public String toString() {
+        return "BeaconEntity{" +
+                "id=" + id + '\'' +
+                "mac=" + mac + '\'' +
+                "x=" + x + '\'' +
+                "y=" + y + '\'' +
+                "z=" + z + '\'' +
+                "battery=" + battery + '\'' +
+                "upLastTime=" + upLastTime + '\'' +
+                "upLastTime2=" + upLastTime2 + '\'' +
+                "timestamp=" + timestamp + '\'' +
+                "lastGateway=" + lastGateway + '\'' +
+                "userDataTs=" + userDataTs + '\'' +
+                "userData=" + userData + '\'' +
+                "rssi=" + rssi + '\'' +
+                "mapId=" + mapId + '\'' +
+                "zoneId=" + zoneId + '\'' +
+                "updatedAt=" + updatedAt + '\'' +
+                '}';
+    }
+}

+ 116 - 0
huimv-eartag-location/src/main/java/com/huimv/location/dao/entity/LocatorEntity.java

@@ -0,0 +1,116 @@
+package com.huimv.location.dao.entity;
+
+import javax.persistence.*;
+import java.io.Serializable;
+import java.sql.Timestamp;
+
+@Entity
+@Table(name = "locator")
+public class LocatorEntity implements Serializable {
+
+    private static final long serialVersionUID = 1L;
+
+    @Id
+    @GeneratedValue(strategy = GenerationType.IDENTITY)
+    @Column(name = "id", nullable = false)
+    private Integer id;
+
+    /**
+     * mac地址
+     */
+    @Column(name = "mac")
+    private String mac;
+
+    /**
+     * 基站位置
+     */
+    @Column(name = "location")
+    private String location;
+
+    /**
+     * 时间戳
+     */
+    @Column(name = "update_date")
+    private Timestamp updateDate;
+
+    /**
+     * 基站IP
+     */
+    @Column(name = "locator_ip")
+    private String locatorIp;
+
+    public void setId(Integer id) {
+        this.id = id;
+    }
+
+    public Integer getId() {
+        return id;
+    }
+
+    /**
+     * mac地址
+     */
+    public void setMac(String mac) {
+        this.mac = mac;
+    }
+
+    /**
+     * mac地址
+     */
+    public String getMac() {
+        return mac;
+    }
+
+    /**
+     * 基站位置
+     */
+    public void setLocation(String location) {
+        this.location = location;
+    }
+
+    /**
+     * 基站位置
+     */
+    public String getLocation() {
+        return location;
+    }
+
+    /**
+     * 时间戳
+     */
+    public void setUpdateDate(Timestamp updateDate) {
+        this.updateDate = updateDate;
+    }
+
+    /**
+     * 时间戳
+     */
+    public Timestamp getUpdateDate() {
+        return updateDate;
+    }
+
+    /**
+     * 基站IP
+     */
+    public void setLocatorIp(String locatorIp) {
+        this.locatorIp = locatorIp;
+    }
+
+    /**
+     * 基站IP
+     */
+    public String getLocatorIp() {
+        return locatorIp;
+    }
+
+    @Override
+    public String toString() {
+        return "LocatorEntity{" +
+                "id=" + id + '\'' +
+                "mac=" + mac + '\'' +
+                "location=" + location + '\'' +
+                "updateDate=" + updateDate + '\'' +
+                "locatorIp=" + locatorIp + '\'' +
+                '}';
+    }
+}

+ 9 - 0
huimv-eartag-location/src/main/java/com/huimv/location/dao/repo/BeaconEntityRepository.java

@@ -0,0 +1,9 @@
+package com.huimv.location.dao.repo;
+
+import com.huimv.location.dao.entity.BeaconEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+public interface BeaconEntityRepository extends JpaRepository<BeaconEntity, Integer>, JpaSpecificationExecutor<BeaconEntity> {
+
+}

+ 24 - 0
huimv-eartag-location/src/main/java/com/huimv/location/dao/repo/BeaconRepo.java

@@ -0,0 +1,24 @@
+package com.huimv.location.dao.repo;
+
+import com.huimv.location.dao.entity.BeaconEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+import org.springframework.data.jpa.repository.Query;
+
+import java.util.List;
+
+public interface BeaconRepo extends JpaRepository<BeaconEntity, Integer>, JpaSpecificationExecutor<BeaconEntity> {
+
+    /**
+     * @Method      : getByIdAndStartTimeAndEndTime
+     * @Description : 读取信标数据
+     * @Params      : [mac, startTime, endTime]
+     * @Return      : java.util.List<com.huimv.location.dao.entity.BeaconEntity>
+     *
+     * @Author      : ZhuoNing
+     * @Date        : 2021/9/18
+     * @Time        : 17:29
+     */
+    @Query(nativeQuery = true,value = "SELECT * FROM beacon WHERE mac=?1 AND up_last_time >= ?2 AND up_last_time <= ?3 ORDER BY up_last_time2 ASC ")
+    List<BeaconEntity> getByIdAndStartTimeAndEndTime(String mac, String startTime, String endTime);
+}

+ 9 - 0
huimv-eartag-location/src/main/java/com/huimv/location/dao/repo/LocatorRepo.java

@@ -0,0 +1,9 @@
+package com.huimv.location.dao.repo;
+
+import com.huimv.location.dao.entity.LocatorEntity;
+import org.springframework.data.jpa.repository.JpaRepository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
+
+public interface LocatorRepo extends JpaRepository<LocatorEntity, Integer>, JpaSpecificationExecutor<LocatorEntity> {
+
+}

+ 20 - 0
huimv-eartag-location/src/main/java/com/huimv/location/service/BeaconService.java

@@ -0,0 +1,20 @@
+package com.huimv.location.service;
+
+import com.alibaba.fastjson.JSONArray;
+import com.huimv.location.dao.entity.BeaconEntity;
+
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+
+public interface BeaconService {
+    // 读取所有信标(耳标)最后一次上传数据
+    JSONArray getRemoteBeacons() throws UnsupportedEncodingException;
+
+    //保存信标数据
+    void saveBeacon() throws UnsupportedEncodingException;
+
+    // 获取单个信标(耳标)数据
+    List<BeaconEntity> getOneBeacons(String mac, String startTime, String endTime);
+//
+//    saveLocation();
+}

+ 167 - 0
huimv-eartag-location/src/main/java/com/huimv/location/service/impl/BeaconServiceImpl.java

@@ -0,0 +1,167 @@
+package com.huimv.location.service.impl;
+
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.location.dao.entity.BeaconEntity;
+import com.huimv.location.dao.repo.BeaconRepo;
+import com.huimv.location.service.BeaconService;
+import com.huimv.location.utils.DataUtil;
+import com.huimv.location.utils.HttpClient;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.io.UnsupportedEncodingException;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Service
+public class BeaconServiceImpl implements BeaconService {
+    @Autowired
+    private HttpClient httpClient;
+    @Autowired
+    private com.huimv.location.utils.StringUtils stringUtils;
+    @Autowired
+    private BeaconRepo beaconRepo;
+
+//    private final String host_url = "http://192.168.1.17:44444";
+//    private final String host_url = "http://localhost:44444";
+    @Value("${host_url}")
+    private String host_url;
+
+    @Override
+    public JSONArray getRemoteBeacons() throws UnsupportedEncodingException {
+        String url = host_url + "/beacons";
+        String beaconsData = httpClient.doGet(url);
+        System.out.println("输出所有信标数据>>"+beaconsData);
+        System.out.println("bytes:"+stringUtils.getBytes(beaconsData));
+        System.out.println("KB:"+stringUtils.getKBytes(beaconsData));
+//        System.out.println("MB:"+stringUtils.getMBytes(outData));
+//        String[] beaconsArray = beaconsData.split(",");
+//        System.out.println("length:"+beaconsArray.length);
+//        for(int a=0;a<beaconsArray.length;a++){
+//            System.out.println(a+",beaconInfo>>"+beaconsArray[a]);
+//        }
+        //
+        JSONArray beaconMacJa = getBeaconMac(beaconsData);
+//        System.out.println("beaconMacJa>>"+beaconMacJa);
+        JSONObject beaconsJo = JSONObject.parseObject(beaconsData);
+//        System.out.println("beaconJo>>"+beaconsJo);
+        DataUtil dateUtil = new DataUtil();
+//        JSONObject beaconInfoJo = beaconsJo.getJSONObject("84c2e4d234c6");
+//        System.out.println("beaconInfoJo>>"+beaconInfoJo);
+//        DateUtil dateUtil = new DateUtil();
+        JSONArray beaconDataJa = new JSONArray();
+        for(int a=0;a<beaconMacJa.size();a++){
+            JSONObject beaconInfoJo = beaconsJo.getJSONObject(beaconMacJa.getString(a));
+//            System.out.println(a+",  beaconInfoJo>>"+beaconInfoJo);
+//            long updatedAt = beaconInfoJo.getLong("updatedAt");
+//            System.out.println("  updatedAt>>"+updatedAt+""+dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",updatedAt));
+            JSONObject beaconDataJo = new JSONObject();
+            beaconDataJo.put("mac",beaconMacJa.getString(a));
+            beaconDataJo.put("x",beaconInfoJo.getString("x"));
+            beaconDataJo.put("y",beaconInfoJo.getString("y"));
+            beaconDataJo.put("z",beaconInfoJo.getString("z"));
+            // 最后上传时间
+            JSONObject userDataTsJo = beaconInfoJo.getJSONObject("userDataTs");
+            beaconDataJo.put("up_last_time",userDataTsJo.getLong("0"));
+            beaconDataJo.put("up_last_time2",dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",userDataTsJo.getLong("0")));
+            // 电量
+            beaconDataJo.put("battery",beaconInfoJo.getJSONObject("userData").getJSONObject("0").getIntValue("battery"));
+            beaconDataJo.put("last_gateway",beaconInfoJo.getString("lastGateway"));
+            beaconDataJo.put("user_data_ts",beaconInfoJo.getString("userDataTs"));
+            beaconDataJo.put("user_data",beaconInfoJo.getString("userData"));
+            beaconDataJo.put("rssi",beaconInfoJo.getString("rssi"));
+            beaconDataJo.put("map_id",beaconInfoJo.getString("mapId"));
+            beaconDataJo.put("zone_id",beaconInfoJo.getString("zoneId"));
+            beaconDataJo.put("updated_at",dateUtil.transferLongToDate("yyyy-MM-dd HH:mm:ss",beaconInfoJo.getLong("updatedAt")));
+            beaconDataJa.add(beaconDataJo);
+        }
+        return beaconDataJa;
+    }
+
+    @Override
+    public void saveBeacon() throws UnsupportedEncodingException {
+        // 获取远程耳标数据
+        JSONArray beaconJa = getRemoteBeacons();
+        for(int a=0;a<beaconJa.size();a++){
+            JSONObject beaconJo = beaconJa.getJSONObject(a);
+//            System.out.println("beaconJo="+beaconJo);
+            BeaconEntity beaconEntity = new BeaconEntity();
+            beaconEntity.setMac(beaconJo.getString("mac"));
+            beaconEntity.setX(beaconJo.getString("x"));
+            beaconEntity.setY(beaconJo.getString("y"));
+            beaconEntity.setZ(beaconJo.getString("z"));
+            beaconEntity.setBattery(beaconJo.getIntValue("battery"));
+            beaconEntity.setUpLastTime(beaconJo.getLong("up_last_time"));
+            beaconEntity.setUpLastTime2(beaconJo.getString("up_last_time2"));
+            beaconEntity.setLastGateway(beaconJo.getString("last_gateway"));
+            beaconEntity.setUserDataTs(beaconJo.getString("user_data_ts"));
+            beaconEntity.setUserData(beaconJo.getString("user_data"));
+            beaconEntity.setRssi(beaconJo.getString("rssi"));
+            beaconEntity.setMapId(beaconJo.getString("map_id"));
+            beaconEntity.setZoneId(beaconJo.getString("zone_id"));
+            beaconEntity.setUpdatedAt(beaconJo.getString("updated_at"));
+            beaconRepo.saveAndFlush(beaconEntity);
+        }
+    }
+
+    /**
+     * @Method      : getOneBeacons
+     * @Description : 读取信标数据
+     * @Params      : [mac, startTime, endTime]
+     * @Return      : java.util.List<com.huimv.location.dao.entity.BeaconEntity>
+     *
+     * @Author      : ZhuoNing
+     * @Date        : 2021/9/18
+     * @Time        : 17:29
+     */
+    @Override
+    public List<BeaconEntity> getOneBeacons(String mac, String startTime, String endTime) {
+        // 读取信标数据
+        return beaconRepo.getByIdAndStartTimeAndEndTime(mac,startTime,endTime);
+    }
+
+    public JSONArray getBeaconMac(String data){
+        JSONArray beaconMacJa = new JSONArray();
+        String[] dataArray = data.split("\":\\{");
+        for(int a=0;a<dataArray.length;a++){
+            String sectionData = dataArray[a].trim().replace("{","").replace("\"","");
+//            System.out.println("sectionData>>"+sectionData);
+//            Pattern pattern = Pattern.compile("(\\d|[a-z]){12}");
+//            Pattern pattern = Pattern.compile(".*([0-9]|[a-z]{12})*");
+            //System.out.println(pattern.pattern());
+//            String text3 = sectionData.trim().replace("{","").replace("\"","");
+//            System.out.println("text3>>"+text3);
+//            Matcher isNum = pattern.matcher(sectionData);
+//            System.out.println("isNum.matches()>>"+isNum.matches());
+            if( sectionData.length() >=12)
+            {
+                String part = sectionData.substring(sectionData.length() - 12, sectionData.length());
+//                System.out.println("part:" + part);
+                Pattern pattern = Pattern.compile("[a-z0-9]+");
+                Matcher isNum = pattern.matcher(part);
+//                System.out.println("isNum.matches()>>"+isNum.matches());
+                if( isNum.matches() )
+                {
+
+//                if (mac.length() == 12) {
+                    beaconMacJa.add(part);
+//                String t = textArray3[a].replace("{","").replace("\"","");
+//                System.out.println("t:"+t);
+                }
+            }
+        }
+        return beaconMacJa;
+    }
+}

+ 40 - 0
huimv-eartag-location/src/main/java/com/huimv/location/task/LocationTask.java

@@ -0,0 +1,40 @@
+package com.huimv.location.task;
+
+import com.huimv.location.service.BeaconService;
+import com.huimv.location.utils.HttpClient;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.scheduling.annotation.Scheduled;
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class LocationTask {
+    @Autowired
+    private HttpClient httpClient;
+    @Autowired
+    private BeaconService beaconService;
+//    String host_url = "http://192.168.1.17:44444";
+//    String host_url = "http://localhost:44444";
+    @Value("${host_url}")
+    private String host_url;
+
+    int i=0;
+    @Scheduled(cron = "0/1 * * * * *")
+    public void saveBeacon() throws UnsupportedEncodingException {
+//        String url = host_url+"/beacons/32b00ae08dfc";
+//        String beacons = httpClient.doGet(host_url);
+//        System.out.println("=====>>>>>使用cron  {}"+ System.currentTimeMillis()+",i="+(i++)+" "+outData);
+
+        beaconService.saveBeacon();
+    }
+}

+ 32 - 0
huimv-eartag-location/src/main/java/com/huimv/location/utils/DataUtil.java

@@ -0,0 +1,32 @@
+package com.huimv.location.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+public class DataUtil {
+
+    /**
+     * @Method : transferLongToDate
+     * @Description :
+     * @Params : [dateFormat, millSec]
+     * @Return : java.lang.String
+     * @Author : ZhuoNing
+     * @Date : 2021/9/18
+     * @Time : 14:44
+     */
+    public String transferLongToDate(String dateFormat, Long millSec) {
+        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
+        Date date = new Date(millSec);
+        return sdf.format(date);
+    }
+}

+ 84 - 0
huimv-eartag-location/src/main/java/com/huimv/location/utils/StringUtils.java

@@ -0,0 +1,84 @@
+package com.huimv.location.utils;
+
+import org.springframework.stereotype.Component;
+
+import java.io.UnsupportedEncodingException;
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+@Component
+public class StringUtils {
+    private final String ENCODE = "UTF-8";
+
+
+    public double getBytes(String text) throws UnsupportedEncodingException {
+        return text.getBytes(ENCODE).length;
+    }
+
+    public double getKBytes(String text) throws UnsupportedEncodingException {
+        return text.getBytes(ENCODE).length / 1024;
+    }
+
+    public double getMBytes(String text) throws UnsupportedEncodingException {
+        return text.getBytes(ENCODE).length / (1024 * 1024);
+    }
+
+    /**
+     * @Method : getTextBytes
+     * @Description : 获取文本内容的字节数,单位是B,MB,GB,编码类型是:UTF-8
+     * @Params : [text, encode]
+     * @Return : java.lang.String
+     * @Author : ZhuoNing
+     * @Date : 2021/9/22
+     * @Time : 14:42
+     */
+    public String getTextBytes(String text, String encode) throws UnsupportedEncodingException {
+        long byteSize = text.getBytes(ENCODE).length;
+//        byteSize = 1025;
+        long kbyteSize = 0;
+        long mbyteSize = 0;
+        long gbyteSize = 0;
+//        System.out.println("byteSize>>" + byteSize);
+        //如果字节数少于1024,则直接以B为单位,否则先除于1024,后3位因太少无意义
+        if (byteSize < 1024) {
+            return String.valueOf(byteSize) + "B";
+        } else {
+            kbyteSize = byteSize / 1024;
+            if (byteSize % 1024 > 0) {
+                kbyteSize++;
+            }
+        }
+        //如果原字节数除于1024之后,少于1024,则可以直接以KB作为单位
+        //因为还没有到达要使用另一个单位的时候
+        //接下去以此类推
+        if (kbyteSize < 1024) {
+            return String.valueOf(kbyteSize) + "KB";
+        } else {
+            mbyteSize = kbyteSize / 1024;
+            if (kbyteSize % 1024 > 0) {
+                mbyteSize++;
+            }
+        }
+        if (mbyteSize < 1024) {
+            //因为如果以MB为单位的话,要保留最后1位小数,
+            //因此,把此数乘以100之后再取余
+            mbyteSize = mbyteSize * 100;
+            return String.valueOf((mbyteSize / 100)) + "."
+                    + String.valueOf((mbyteSize % 100)) + "MB";
+        } else {
+            //否则如果要以GB为单位的,先除于1024再作同样的处理
+            gbyteSize = mbyteSize * 100 / 1024;
+            return String.valueOf((gbyteSize / 100)) + "."
+                    + String.valueOf((gbyteSize % 100)) + "GB";
+        }
+
+    }
+}