package cn.ksource.core.util;
|
|
import java.awt.image.BufferedImage;
|
import java.io.BufferedReader;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.io.InputStreamReader;
|
import java.io.OutputStream;
|
import java.io.UnsupportedEncodingException;
|
import java.net.HttpURLConnection;
|
import java.net.URL;
|
import java.net.URLDecoder;
|
import java.text.DecimalFormat;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Calendar;
|
import java.util.Date;
|
import java.util.HashMap;
|
import java.util.Map;
|
import java.util.Random;
|
import java.util.Set;
|
import java.util.UUID;
|
import java.util.Map.Entry;
|
import java.util.regex.Matcher;
|
import java.util.regex.Pattern;
|
|
import javax.imageio.ImageIO;
|
import javax.servlet.http.Cookie;
|
import javax.servlet.http.HttpServletRequest;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import sun.misc.BASE64Decoder;
|
import sun.misc.BASE64Encoder;
|
|
import com.thoughtworks.xstream.core.util.Base64Encoder;
|
|
public class StringUtil {
|
|
public static String test(){
|
return StringUtil.getUUID();
|
}
|
|
|
/**
|
* 验证是否为Email格式
|
* @param email
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Sep 7, 2013 12:06:34 PM
|
*/
|
public static boolean isEmail(String email){
|
Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配
|
Matcher matcher = pattern.matcher(email);
|
if (matcher.matches()){
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
*
|
* @param param
|
* @return
|
*/
|
public static String asc2gbk(String param){
|
String ret = "";
|
if (StringUtils.isNotBlank(param)) {
|
try {
|
ret = new String(param.getBytes("ISO-8859-1"),"GBK").trim();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
ret ="";
|
}
|
}
|
return ret;
|
}
|
public static String asc2utf8(String param){
|
String ret = "";
|
if (StringUtils.isNotBlank(param)) {
|
try {
|
ret = new String(param.getBytes("ISO-8859-1"),"UTF-8").trim();
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
ret ="";
|
}
|
}
|
return ret;
|
}
|
/**
|
* 功能描述:阿拉伯数字转汉字<BR>
|
*
|
* @param a
|
* @return
|
* @author:杨凯<BR>
|
* 时间:Apr 25, 2009 7:10:23 PM<BR>
|
*/
|
public static String translateNumToChinese(int a) {
|
String[] units = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
|
String[] nums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
|
String result = "";
|
if (a < 0) {
|
result = "负";
|
a = Math.abs(a);
|
}
|
String t = String.valueOf(a);
|
for (int i = t.length() - 1; i >= 0; i--) {
|
int r = (int) (a / Math.pow(10, i));
|
if (r % 10 != 0) {
|
String s = String.valueOf(r);
|
String l = s.substring(s.length() - 1, s.length());
|
result += nums[Integer.parseInt(l) - 1];
|
result += (units[i]);
|
} else {
|
if (!result.endsWith("零")) {
|
result += "零";
|
}
|
}
|
}
|
return result;
|
}
|
|
/**
|
* 功能描述:取得32位UUID<BR>
|
*
|
* @return
|
* @author:杨凯<BR>
|
* 时间:Feb 20, 2009 11:04:36 PM<BR>
|
*/
|
public static String getUUID() {
|
return UUID.randomUUID().toString().replaceAll("-", "");
|
}
|
|
|
/**
|
* 功能描述:电话号码用××代替<BR>
|
*
|
* @param photo
|
* @return
|
* @author:杨凯<BR>
|
* 时间:Apr 25, 2009 6:54:11 PM<BR>
|
*/
|
public static String dhhmreplaceByXX(String photo) {
|
if (org.apache.commons.lang.StringUtils.isBlank(photo)) {
|
return null;
|
}
|
char[] strs = photo.toCharArray();
|
for (int i = 3; i < 8; i++) {
|
strs[i] = '*';
|
}
|
return new String(strs);
|
}
|
|
/**
|
* 去掉字符串中的回车换行符
|
*
|
* @param str
|
* @return
|
*/
|
public static String removeNewline(String str) {
|
String result = null;
|
if (str != null) {
|
result = str.replaceAll("\\r\\n", "");
|
result = result.replaceAll(new String(new char[] { 10 }), "");
|
}
|
return result;
|
}
|
|
/**
|
* 功能描述:判断字符串是否为汉字<BR>
|
* @param str
|
* @return
|
* @author:杨凯<BR>
|
* 时间:Jul 8, 2009 9:02:48 AM<BR>
|
*/
|
public static boolean isNumber(String str) {
|
if (str.matches("\\d*")) {
|
return true;
|
} else {
|
return false;
|
}
|
|
}
|
|
/**
|
* 功能描述:浮点型判断
|
*
|
* @param str
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-7-29<BR>
|
*/
|
public static boolean isFloat(String str) {
|
if (str == null || "".equals(str))
|
return false;
|
Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
|
return pattern.matcher(str).matches();
|
}
|
|
/**
|
* 功能描述:整型判断
|
*
|
* @param str
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-7-29<BR>
|
*/
|
public static boolean isInteger(String str) {
|
if (StringUtil.isBlank(str))
|
return false;
|
return str.matches("^-?\\d+");
|
}
|
|
/**
|
* 功能描述:获取子字符串的个数
|
*
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-7-16<BR>
|
*/
|
public static int getSubCount(String sub, String string) {
|
if (StringUtils.isBlank(string)) {
|
return 0;
|
}
|
int subLength = sub.length();
|
int forLength = string.length() - sub.length() + 1; // 循环次数
|
int count = 0; // 记录循环次数
|
for (int i = 0; i < forLength; i++) {
|
if (StringUtils.substring(string, i, i + subLength).equals(sub)) {
|
count++;
|
}
|
}
|
return count;
|
}
|
|
/**
|
* 功能描述:接受一个List<String>,如果有任何一个为空,则返回true,否则返回false
|
* @param list
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-8-8<BR>
|
*/
|
public static boolean isBlank(String... list){
|
for (String string : list) {
|
if(StringUtils.isBlank(string)){
|
return true;
|
}
|
}
|
return false;
|
}
|
|
|
/**
|
* 如果IsNull,就返回defalut
|
* @param str
|
* @param defalut
|
* @return
|
* @version V1.0.0
|
* @author 杨凯
|
* @date Sep 19, 2013 8:59:22 AM
|
*/
|
public static String defaultIfNull(String str,String defalut){
|
if (StringUtils.isBlank(str)) {
|
return defalut;
|
}
|
return str;
|
}
|
|
/**
|
* 功能描述:都不为空,则返回true
|
* @param list
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-9-25 上午09:04:47<BR>
|
*/
|
public static boolean isNotBlank(String... list){
|
return !isBlank(list);
|
}
|
|
/**
|
* 功能描述:trim从前台获取的参数,包括null变成"",'变成''<BR>
|
* @param s
|
* @return
|
* @author 胡晓<BR>
|
* 时间:2009-10-28 下午03:00:52<BR>
|
*/
|
public static String trimParam(Object s){
|
String str = ConvertUtil.obj2Str(s);
|
if(StringUtils.isBlank(str)){
|
return "";
|
}else{
|
return str.replaceAll("'", "''").trim();
|
}
|
}
|
|
/**
|
* 描述:tapestry 比较一个字符串包含另一个字符串
|
* @param o1
|
* @param o2
|
* @return
|
* 作者:李坤
|
* 时间:Sep 9, 2010
|
*/
|
public static boolean Str1ContainsStr2(Object o1 , Object o2){
|
if(ConvertUtil.obj2Str(o1).contains(ConvertUtil.obj2Str(o2))){
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 功能描述:截取指定长度字符串,一个汉字占两个字节,字符和数字占用一个<BR>
|
* @param value
|
* @param length
|
* @param flag 为真则后加"..."
|
* @return
|
* @author:杨凯<BR>
|
* 时间:Sep 12, 2009 3:53:36 PM<BR>
|
*/
|
public static String subStr(String value,int length,boolean flag){
|
if (StringUtils.isBlank(value) || value.getBytes().length <= length)
|
return value;
|
for (int i = 0; i <= value.length(); i++) {
|
if (value.substring(0, i).getBytes().length > length) {
|
if (i - 1 >= 0) {
|
return value.substring(0, i - 1)+(flag ? "...":"");
|
}
|
}
|
}
|
return value;
|
}
|
/**
|
* 清除一个时间格式字符串中的"-" ":"和空格,返回一个格式化的String<br>
|
* 例如:将一个 2010-11-13 12:00 转换为 201011131200 <br>
|
*
|
* 作者:<a href="mailto:winhunter@163.com">郝世博</a><br>
|
* 时间:2010-12-08 18:02
|
*
|
* @param date
|
* @return
|
*/
|
public static String clearDateStrToLong(String date) throws NumberFormatException{
|
String ret = "";
|
if(date!=null) {
|
ret = date.replaceAll("[-]*[\\s]*[:]*", "");
|
}
|
return ret;
|
}
|
/**
|
* 描述:小数格式化
|
* @param value (需要格式化的数字)
|
* @param length (保留几位小数)
|
* @return
|
* 作者:李坤
|
* 时间:Nov 5, 2010
|
*/
|
public static Double getDoubleFormat(Double value,Integer length){
|
String str=".";
|
for (int i = 0; i < length; i++) {
|
str+="#";
|
}
|
return Double.valueOf(new DecimalFormat(str).format(value));
|
}
|
|
public static String decode(String str){
|
String string = "";
|
if(StringUtil.isNotBlank(str)){
|
try {
|
string = URLDecoder.decode(str, "gbk");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
return string;
|
}
|
|
public static String decode_utf(String str){
|
String string = "";
|
if(StringUtil.isNotBlank(str)){
|
try {
|
string = URLDecoder.decode(str, "utf-8");
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
return string;
|
}
|
|
public static String decode_Iso_utf(String str) throws UnsupportedEncodingException{
|
if(StringUtil.isBlank(str)){
|
return str;
|
}
|
return new String(str.getBytes("ISO-8859-1"),"GBK");
|
}
|
|
/**
|
* 将2010-1-1 转为 20100101.
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Apr 8, 2011 6:48:15 PM
|
*/
|
public static String datestr2long(String date){
|
String[] time = date.split("-");
|
StringBuffer dateBuf = new StringBuffer();
|
for (int i = 0; i < time.length; i++) {
|
if (i==0) {
|
dateBuf.append(time[i]);
|
}else {
|
dateBuf.append(time[i].length()<2?"0"+time[i]:time[i]);
|
}
|
}
|
return dateBuf.toString();
|
}
|
|
/**
|
* 将一个字符串根据给定的长度分隔成N个字符串的数组.
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Mar 30, 2011 11:06:59 PM
|
*/
|
public static String[] splitForLength(String str , int length){
|
int len = str.length();
|
int pageSize = (int) Math.ceil(len/70D);
|
String[] array = new String[pageSize];
|
for (int i = 0; i < pageSize; i++) {
|
int begin = i*70;
|
int end = ( begin+70) >= len ? len : (begin+70 );
|
array[i] = str.substring( begin, end);
|
}
|
return array;
|
}
|
|
|
/**
|
* 根据一个字符数组组成一个由指定字符分隔的字符串.
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Mar 31, 2011 1:42:17 PM
|
*/
|
public static String strArrayToStr(String[] arr,String sign){
|
StringBuffer buf = new StringBuffer();
|
for (int i = 0; i < arr.length; i++) {
|
buf.append(arr[i]);
|
if ( i<arr.length-1 ) {
|
buf.append(sign);
|
}
|
}
|
return buf.toString();
|
}
|
|
|
/**
|
* 将一个字符串中的空格和换行转行成html代码.
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Apr 8, 2011 1:46:00 PM
|
*/
|
public static String str2Html(String str){
|
String retStr = "";
|
if(str!=null){
|
retStr = str;
|
retStr = retStr.replace("&", "&");
|
retStr = retStr.replace(" ", " ");
|
retStr = retStr.replace("<", "<");
|
retStr = retStr.replace(">", ">");
|
retStr = retStr.replaceAll(" ", " ");
|
retStr = retStr.replaceAll("\r\n", "<br>");
|
retStr = retStr.replaceAll("\r", "<br>");
|
}
|
return retStr;
|
}
|
|
/**
|
* .
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Jun 23, 2011 6:40:22 PM
|
*/
|
public static String num2Weekstr(int num){
|
String result = "";
|
switch (num) {
|
case 1:
|
result = "一";
|
break;
|
case 2:
|
result = "二";
|
break;
|
case 3:
|
result = "三";
|
break;
|
case 4:
|
result = "四";
|
break;
|
case 5:
|
result = "五";
|
break;
|
case 6:
|
result = "六";
|
break;
|
case 7:
|
result = "日";
|
break;
|
default:
|
break;
|
}
|
return result;
|
}
|
|
/**
|
* 将字符串或路径中的 \ 转为 /.
|
* @author <a href="mailto:winhunter@163.com">郝世博</a>
|
* @time Jul 8, 2011 3:11:54 PM
|
*/
|
public static String convert(String text){
|
StringBuffer buf = new StringBuffer("");
|
if ( null==text ) {
|
return null;
|
}
|
char[] chars = text.toCharArray();
|
for ( char tmp : chars ) {
|
if (tmp=='\\') {
|
tmp = '/';
|
}
|
buf.append(tmp);
|
}
|
|
return buf.toString();
|
}
|
/**
|
* 编码
|
* 描述:
|
* @param str
|
* @return
|
* 作者:李坤
|
* 时间:Aug 23, 2011
|
*/
|
public static String base64Encode(String str){
|
if(StringUtil.isBlank(str))
|
return str;
|
return new Base64Encoder().encode(str.getBytes());
|
}
|
/**
|
* 解码
|
* 描述:
|
* @param str
|
* @return
|
* @throws IOException
|
* 作者:李坤
|
* 时间:Aug 23, 2011
|
*/
|
public static String base64Decode(String str) throws IOException{
|
if(StringUtil.isBlank(str))
|
return str;
|
return new String(new BASE64Decoder().decodeBuffer(str));
|
}
|
/**
|
* 获取随机数
|
*/
|
public static String getRandom(int length) {
|
String yzm = StringUtils.right(Math.random()+"", length);
|
while (yzm.length()!=length) {
|
yzm = StringUtils.right(Math.random()+"", length);
|
}
|
return yzm;
|
}
|
|
|
|
|
|
public static String trim(String str) {
|
if(StringUtil.isBlank(str)){
|
return str;
|
}
|
return str.trim();
|
}
|
public static String[] split(String str, String regex) {
|
return str.split(regex);
|
}
|
public static String replace(String str, String regex,String replacement) {
|
return str.replaceAll(regex, replacement);
|
}
|
/**
|
* 判断一个自己数组组成的字符串是否包含字符串
|
* @param strs 父字符串
|
* @param reg 用来拆分父字符串成为数组
|
* @param str 子字符串
|
* @return
|
*/
|
public static Boolean arrayContainsStr(String strs,String reg,String str) {
|
if(isBlank(strs)){
|
return false;
|
}
|
String array [] = strs.split(reg);
|
for (String string : array) {
|
if(string.equals(str)){
|
return true;
|
}
|
}
|
return false;
|
}
|
/**
|
* 获取当前工程路径
|
* @return
|
*/
|
public static String getProjectPath() {
|
return StringUtil.class.getResource("/").getPath().substring(1).split("WEB-INF")[0];
|
}
|
|
|
public static String getStringValue(String inStr,int pos){
|
String retStr="";
|
if(inStr!=null&!"".equals(inStr)){
|
String[] tempS=inStr.split(",");
|
retStr= tempS[pos];
|
}
|
|
return retStr;
|
}
|
public static boolean isEmpty(String str){
|
if(null==str||str.trim().equals("")||"null".equals(str)){
|
return true;
|
}
|
return false;
|
}
|
/**
|
* 分割字符串中间加字符
|
*/
|
public static String getStringSplitByStr(String inStr,String str,int j){
|
if(inStr!=null&!"".equals(inStr)&str!=null&!"".equals(str)){
|
StringBuilder sb=new StringBuilder(inStr);
|
int length=sb.length();
|
for(int i=1;i<=length/j;i++)
|
{
|
if(i==length/j && (length%j)==0);
|
else
|
sb.insert(i*j+i-1,str);
|
}
|
inStr = sb.toString();
|
}
|
|
return inStr;
|
|
}
|
|
/**
|
* 判断是否为空
|
*/
|
public static boolean notEmpty(String str) {
|
boolean b = false;
|
if(null != str && !"".equals(str.trim())) {
|
b = true;
|
}
|
return b;
|
}
|
|
|
public static boolean notEmptyNum(String str) {
|
boolean b = false;
|
if(null != str && !"".equals(str.trim()) && StringUtils.isNumeric(str)) {
|
b = true;
|
}
|
return b;
|
}
|
|
|
/**
|
* 判断是否是正确的手机号码
|
* @param mobile
|
* @return
|
*/
|
public static boolean isValidMobileNum(String mobile){
|
if(StringUtil.notEmptyNum(mobile)) {
|
mobile = mobile.trim();
|
}
|
Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
|
Matcher matcher = pattern.matcher(mobile);
|
return matcher.matches();
|
}
|
|
/**
|
* 判断是否是正确的电话号码(固定电话)
|
* @param mobile
|
* @return
|
*/
|
public static boolean isValidPhone(String phone){
|
if(StringUtil.notEmptyNum(phone)) {
|
phone = phone.trim();
|
}
|
Pattern pattern = Pattern.compile("^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$");
|
Matcher matcher = pattern.matcher(phone);
|
return matcher.matches();
|
}
|
|
/**
|
* 判断是否为正确的邮政编码
|
*/
|
public static boolean isPostCode(String postCode) {
|
if(StringUtil.notEmptyNum(postCode)) {
|
postCode = postCode.trim();
|
}
|
Pattern pattern = Pattern.compile("^\\d{6}$");
|
Matcher matcher = pattern.matcher(postCode);
|
return matcher.matches();
|
}
|
|
/**
|
* BASE64解密
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static String decryptBASE64(String key) {
|
try {
|
return new String((new BASE64Decoder()).decodeBuffer(key));
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* BASE64加密
|
* @param key
|
* @return
|
* @throws Exception
|
*/
|
public static String encryptBASE64(String key) {
|
String v = new BASE64Encoder().encodeBuffer(key.getBytes());
|
return v.replaceAll("\n", "").replaceAll("\r", "");
|
}
|
|
|
/**
|
* 判断长度
|
*/
|
public static boolean isRightLength(String key,int minLength,int maxLength) {
|
int length = key.trim().length();
|
if(length>=minLength && length<=maxLength) {
|
return true;
|
}
|
return false;
|
}
|
|
/**
|
* 判断是否是密码(由6-18位的字母,数字,下划线组成)
|
* @param args password
|
*/
|
public static boolean isPasswordReg(String password) {
|
String passwordReg = "^\\w{6,18}$";
|
Pattern pattern = Pattern.compile(passwordReg);
|
return pattern.matcher(password).matches();
|
}
|
|
|
/**
|
* 生成任意长度数字
|
* @param len 长度
|
* @return number
|
*/
|
public static String randomNumber(int len){
|
int length = len<=0?6:len;// 随机密码长度为
|
StringBuffer buffer = new StringBuffer("0123456789");
|
StringBuffer sb = new StringBuffer();
|
Random r = new Random();
|
int range = buffer.length();
|
//在此控制,随机数字的第一位不能是0,因为有的随机码是需要传给前台JS的,JS获取到首位为0的数字会自动将0去掉,这样就和后台对应不上,所以在此直接将首位为0的情况排除
|
for (boolean firstZero=true;sb.length()<length;) {
|
int nextInt = Integer.valueOf(String.valueOf(buffer.charAt(r.nextInt(range))));
|
if(firstZero&&nextInt==0){
|
continue;
|
}else{
|
firstZero=false;
|
sb.append(nextInt);
|
}
|
}
|
return sb.toString();
|
}
|
|
|
/**
|
* 将cookie封装到Map里面
|
* @param request
|
* @return
|
*/
|
public static Map<String,Cookie> readCookieMap(HttpServletRequest request){
|
Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
|
Cookie[] cookies = request.getCookies();
|
if(null!=cookies){
|
for(Cookie cookie : cookies){
|
cookieMap.put(cookie.getName(), cookie);
|
}
|
}
|
return cookieMap;
|
}
|
|
/**
|
* 转化日期格式的形式 如:yyyy年MM月 转化为 yyyyMM
|
* @param oldFormat 老的日期格式
|
* @param newFormat 新的日期格式
|
* @param dateStr 日期数据
|
* @return
|
*/
|
public static String formatDate(String oldFormat,String newFormat,String dateStr) {
|
SimpleDateFormat format = new SimpleDateFormat(oldFormat);
|
try {
|
Date d = format.parse(dateStr);
|
SimpleDateFormat nFormat = new SimpleDateFormat(newFormat);
|
String result = nFormat.format(d);
|
return result;
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 获取图片长和宽
|
* @param url 图片链接
|
* @return
|
*/
|
public static int[] getImgSize(String url) {
|
int[] result = new int[2];
|
|
File picture = new File(url);
|
BufferedImage sourceImg;
|
try {
|
sourceImg = ImageIO.read(new FileInputStream(picture));
|
int width = sourceImg.getWidth();
|
int height = sourceImg.getHeight();
|
System.out.println(width);
|
System.out.println(height);
|
result[0] = width;
|
result[1] = height;
|
return result;
|
} catch (Exception e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 比较两个日期的大小
|
* @param beginTime 开始时间
|
* @param endTime 结束时间
|
* @param formatStr 日期格式
|
* @return
|
*/
|
public static boolean compareDate(String beginTime,String endTime,String formatStr) {
|
boolean b = false;
|
if(StringUtil.notEmpty(beginTime) && StringUtil.notEmpty(endTime)) {
|
SimpleDateFormat format = new SimpleDateFormat(formatStr);//设置日期格式
|
try {
|
Date beginDate = format.parse(beginTime);
|
Date endDate = format.parse(endTime);
|
if(endDate.after(beginDate)) {
|
b = true;
|
}
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
}
|
return b;
|
}
|
/**
|
* 通过文件后缀获取文件对应的图片
|
*/
|
public static String docSuffer(String suffer) {
|
suffer = suffer.toLowerCase();
|
if(suffer.equals("doc") || suffer.equals("docx")) {
|
return "doc";
|
} else if(suffer.equals("xls") || suffer.equals("xlsx")) {
|
return "excel";
|
} else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
|
return "img";
|
} else if(suffer.equals("pdf")) {
|
return "pdf";
|
} else if(suffer.equals("ppt") || suffer.equals("pptx")) {
|
return "ppt";
|
} else if(suffer.equals("rar") || suffer.equals("zip")) {
|
return "rar";
|
} else if(suffer.equals("txt")) {
|
return "txt";
|
} else {
|
return "cloud";
|
}
|
}
|
/**
|
* 得到文件转化后的大小
|
* @param fs 文件大小 (单位B)
|
*/
|
public static String getFileSize(double fs) {
|
String fileSize = new String();
|
if(fs<1024) {
|
fileSize = fs+" B";
|
} else if(fs>=1024 && fs<1024*1024) {
|
double fsize = NumberUtil.div(fs, Double.valueOf("1024"), 0);
|
fileSize = ConvertUtil.obj2Int(fsize)+" KB";
|
} else {
|
double size = 1024*1024;
|
double fsize = NumberUtil.div(fs, size, 0);
|
fileSize = ConvertUtil.obj2Int(fsize)+" MB";
|
}
|
return fileSize;
|
}
|
|
/**
|
* 微信端文件后缀对应的图片名称
|
*/
|
public static String wechateDocSuffer(String suffer) {
|
suffer = suffer.toLowerCase();
|
if(suffer.equals("doc") || suffer.equals("docx")) {
|
return "doc";
|
} else if(suffer.equals("xls") || suffer.equals("xlsx")) {
|
return "xls";
|
} else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
|
return "img";
|
} else if(suffer.equals("pdf")) {
|
return "pdf";
|
} else if(suffer.equals("ppt") || suffer.equals("pptx")) {
|
return "ppt";
|
} else if(suffer.equals("rar") || suffer.equals("zip")) {
|
return "rar";
|
} else if(suffer.equals("txt")) {
|
return "txt";
|
} else {
|
return "default";
|
}
|
}
|
/**
|
* 详情页对应的图片名称
|
*/
|
public static String detailDocSuffer(String suffer) {
|
suffer = suffer.toLowerCase();
|
if(suffer.equals("doc") || suffer.equals("docx")) {
|
return "DOC2";
|
} else if(suffer.equals("xls") || suffer.equals("xlsx")) {
|
return "XLS2";
|
} else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
|
return "JPG2";
|
} else if(suffer.equals("pdf")) {
|
return "PDF2";
|
} else if(suffer.equals("ppt") || suffer.equals("pptx")) {
|
return "PPT2";
|
} else if(suffer.equals("rar") || suffer.equals("zip")) {
|
return "Archive2";
|
} else if(suffer.equals("txt")) {
|
return "TXT2";
|
} else {
|
return "common1_1";
|
}
|
}
|
/**
|
* 通过年,月获得该月的第一天和最后一天
|
* @param year 格式 yyyy
|
* @param month 格式 MM
|
* @return 数组第一个当前月第一天, 数组第二个:当前月最后一天
|
*/
|
public static String[] queryFirstLastDate(String year,String month) {
|
String[] str = new String[2];
|
if(!StringUtil.notEmptyNum(year)||year.length()!=4||Integer.valueOf(year)<0) {
|
year = DateUtil.format(new Date(), "yyyy");
|
}
|
if(!StringUtil.notEmptyNum(month)||Integer.valueOf(month)<1||Integer.valueOf(month)>12) {
|
month = DateUtil.format(new Date(), "MM");
|
}
|
if(month.length()!=2) {
|
month = "0"+month;
|
}
|
String firstDate = year+"-"+month+"-01";
|
str[0] = firstDate;
|
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
try {
|
Date date = dateFormat.parse(firstDate);
|
String lastDate = dateFormat.format(lastDayOfMonth(date));
|
str[1] = lastDate;
|
} catch (ParseException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return str;
|
}
|
|
/*
|
* 获得指定日期所在月最后一天
|
*/
|
public static Date lastDayOfMonth(Date date) {
|
Calendar cal = Calendar.getInstance();
|
cal.setTime(date);
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
cal.roll(Calendar.DAY_OF_MONTH, -1);
|
return cal.getTime();
|
}
|
|
|
}
|