1 packagecom.rui.util;2
3 importjava.text.DateFormat;4 importjava.text.DecimalFormat;5 importjava.text.SimpleDateFormat;6 importjava.util.Date;7 importjava.util.Random;8
9 /**
10 *11 * @ClassName: StrUtils12 * @Description: 工具类13 *@authorposeidon14 * @date 2015年10月23日 下午8:13:4515 *@versionV1.0.016 */
17 public classStrUtils {18
19
20 /**
21 *22 * @Title: isEmpty23 * @Description: 空判断24 *@paramcontent25 *@returnboolean26 */
27 public static booleanisEmpty(String content){28 return (content==null || content.equals(""))?true:false;29 }30
31 /**
32 *33 * @Title: isNotEmpty34 * @Description: 非空判断35 *@paramcontent36 *@returnboolean37 */
38 public static booleanisNotEmpty(String content){39 return !isEmpty(content);40 }41
42
43 /**
44 *45 * @Title: formatDate46 * @Description: 格式化日期类47 *@paramdate48 *@parampattern49 *@returnString50 */
51 public staticString formatDate(Date date,String pattern){52 if(date!=null){53 String dateString = newSimpleDateFormat(pattern).format(date);54 returndateString;55 }else{56 return "";57 }58 }59
60 /**
61 *62 * @Title: getExt63 * @Description: 获取文件的后缀64 *@paramname 文件名称65 *@paramflag true有点false没点66 *@returnString67 */
68 public static String getExt(String name,booleanflag){69 if(isNotEmpty(name)){70 String ext = null;71 if(flag){72 ext = name.substring(name.lastIndexOf("."), name.length());73 }else{74 ext = name.substring(name.lastIndexOf(".")+1, name.length());75 }76 returnext;77 }else{78 return "";79 }80 }81
82 /**
83 *84 * @Title: generateFileName85 * @Description: 为上传文件自动分配文件名称,避免重复86 *@paramfileName87 *@paramrandomNum88 *@paramdataPattern89 *@returnString90 */
91 public static String generateFileName(String fileName,intrandomNum,String dataPattern) {92 //获得当前时间
93 DateFormat format = newSimpleDateFormat(dataPattern);94 //转换为字符串
95 String formatDate = format.format(newDate());96 //随机生成文件编号
97 int random = newRandom().nextInt(randomNum);98 //获得文件后缀名称
99 int position = fileName.lastIndexOf(".");100 String extension =fileName.substring(position);101 //组成一个新的文件名称
102 return formatDate + random +extension;103 }104
105
106 /**
107 *108 * @Title: countFileSize109 * @Description: 根据File文件的长度统计文件的大小110 *@paramfileSize111 *@returnString112 */
113 public static String countFileSize(longfileSize) {114 String fileSizeString = "";115 try{116 DecimalFormat df = new DecimalFormat("#.00");117 long fileS =fileSize;118 if (fileS == 0) {119 fileSizeString = "0KB";120 } else if (fileS < 1024) {121 fileSizeString = df.format((double) fileS) + "B";122 } else if (fileS < 1048576) {123 fileSizeString = df.format((double) fileS / 1024) + "KB";124 } else if (fileS < 1073741824) {125 fileSizeString =df126 .format(((double) fileS / 1024 / 1024) - 0.01)127 + "MB";128 } else{129 fileSizeString = df.format((double) fileS / 1024 / 1024 / 1024)130 + "G";131 }132 } catch(Exception e) {133 e.printStackTrace();134 }135 returnfileSizeString;136 }137
138 /**
139 *140 * @Title: conversionSpecialCharacters141 * @Description: 把两个反斜线转换成正斜线142 *@paramstring143 *@returnString144 */
145 public staticString conversionSpecialCharacters(String string) {146 return string.replaceAll("\\\\", "/");147 }148
149 public static voidmain(String[] args) {150
151
152 }153 }