import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
*/
public class CommonUtil
{
private static double EARTH_RADIUS = 6378.137;
/**
* 判断字符串是否不为空* @param originalString 原始字符串
* @return 不为空返回true,否则返回false
*/
public static boolean isNotEmptyString(String originalString)
{
return originalString != null && !"".equals(originalString.trim());
}
* 判断list不为空
* @param list
* @return 不为空返回true,否则返回false
*/
public static boolean isNotEmptyList(List<?> list)
{
return list != null && !list.isEmpty();
}
/**
* 生成32位UUID 并去掉"-"
*/
public static String getUUID() {
return UUID.randomUUID().toString().replaceAll("-", "");
/**
* 填充字符串
* @param code value
* @param length value
* @param fillCode value
* @return String
*/
public static String getStatusCode(String code, int length, String fillCode)
{
String statusCode;
for (statusCode = code; statusCode.length() < length; statusCode = (new StringBuilder()).append(
fillCode).append(statusCode).toString())
{
}
return statusCode;
}
/**
* 〈直接向用户返回指定信息〉
* @param response
* @param msg
*/
public static void returnMsg(HttpServletResponse response, String msg)
{
if (msg == null || "".equals(msg))
{
return;
}
Writer out = null;
try
{
response.setCharacterEncoding("utf-8");
out = response.getWriter();
out.write(msg);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
/**
* 〈正则匹配〉
* @param reg
* @param str
* @return boolean
*/
public static boolean test(String reg, String str)
{
Matcher matcher = Pattern.compile(reg).matcher(str);
return matcher.find();
}
//*分割字符串后,将字符串数组转换成整型。
public static String[] convertStringToIntArray(String stringSplit)
{
String s[] = stringSplit.split(",");
String s3[] = new String[s.length];
for (int i = 0; i < s.length; i++ )
{
s3[i] = s[i];/*将字符串数组转换成整型,结果出现异常。后面事件处理时,调用sort,出现错误。*/
}
return s3;
}
/**
* 计算list记录数
* @param list
* @return
*/
public static int totalNum(List<?> list)
{
if ( !list.isEmpty())
{
int num = Integer.parseInt(list.toArray()[0].toString());
list.clear();
return num;
}
else
{
return 0;
}
}
/**
* 获得当前日期字符串格式(精确到秒)
*
* @return String
*/
public static String getCurrentDate()
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return sdf.format(new Date(System.currentTimeMillis()));
}
/**
* 转换日期,按照给定的格式
* @return String
*/
public static String convertDateToString(String mode, Date date)
{
SimpleDateFormat sdf = new SimpleDateFormat(mode);
return sdf.format(date);
}
public static Date formDate(String mode, Date date)
{
try
{
SimpleDateFormat formatter = new SimpleDateFormat(mode);
Date strtodate = formatter.parse(formatter.format(date));
return strtodate;
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return date;
}
public static String convertListTOSelect(Collection list, String listKey,
String listValue, String name,
String value, String id,
String width, boolean hasEmpty)
{
StringBuffer sb = new StringBuffer();
if (name == null)
{
name = "";
}
if (id == null)
{
id = "";
}
sb.append("<select style='width:" + width + "' name=" + name + " id="
+ id + ">");
if (hasEmpty)
{
sb.append("<option value=''></option>");
}
if (list != null && !list.isEmpty())
{
for (Object object : list)
{
Class c1 = object.getClass();
try
{
Field[] fields = c1.getDeclaredFields();
AccessibleObject.setAccessible(fields, true);
for (Field f : fields)
{
if (f.getName().equals(listKey))
{
if (value != null)
{
if (value.equals(f.get(object).toString()))
{
sb.append("<option value=" + f.get(object)
+ " selected>");
}
else
{
sb.append("<option value=" + f.get(object)
+ ">");
}
}
else
{
sb.append("<option value=" + f.get(object)
+ ">");
}
}
if (f.getName().equals(listValue))
{
sb.append(f.get(object) + "</option>");
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
sb.append("</select>");
return sb.toString();
}
/**
* 向页面输出文件流
* @param request
* @param response
* @param localPath 文件绝对路径
* @param fileName 显示文件名
*/
public static void outWriteFile(HttpServletRequest request,HttpServletResponse response,String localPath,String fileName)
{
try {
fileName= new String(fileName.getBytes("GBK"), "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
response.setContentType("application/x-download");
response.setHeader("content-disposition","attachment; filename="+fileName);
InputStream in = null;
OutputStream out = null;
try {
byte[] buffer = new byte[1024];
File f = new File(localPath);
if (!f.exists() || !f.isFile()) {
return;
}
in = new FileInputStream(f);
out = response.getOutputStream();
while (in.read(buffer) > 0) {
out.write(buffer);
out.flush();}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != in) {
in.close();
}
if (null != out) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
**
* 产生4位随机数(0000-9999)
* @return 4位随机数
*/
public static String getFourRandom(){
Random random = new Random();
String fourRandom = random.nextInt(10000) + "";
int randLength = fourRandom.length();
if(randLength<4){
for(int i=1; i<=4-randLength; i++)
fourRandom = "0" + fourRandom ;
}
return fourRandom;
}
/**
* 把流水号转换成4位字符串
* @param orderNo
* @return
*/
public static String getProcessNo(int orderNo)
{
if (orderNo > 9999)
{
return String.valueOf(orderNo);
}
String processNo = "0000";
return processNo.substring(0,processNo.length() - String.valueOf(orderNo).length()) + orderNo;
}
private static double rad(double d) {
return d * Math.PI / 180.0;
}
/**
*〈返回一个时间格式〉
* @param strDate
* @return Date
*/
public static Date StringToDate(String strDate)
{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date begin = null;
try {
begin = df.parse(strDate);
} catch (Exception e) {
// e.printStackTrace();
}
return begin;
}
}