标签:
package util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Random;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import net.coobird.thumbnailator.Thumbnails;
public class Util {
/* Date类型转化到String类型 */
public static String DateToString(java.util.Date date, int type) {
String result = null;
String format = "yyyy-MM-dd";
if (type == 0) {
format = "yyyy-MM-dd";
}
if (type == 1) {
format = "yyyy/MM/dd";
} else if (type == 2) {
format = "yyyyMMdd";
} else if (type == 21) {
format = "yyyyMM";
} else if (type == 3) {
format = "MM/dd/yy";
} else if (type == 4) {
format = "yyyy-MM-dd HH:mm:ss";
} else if (type == 5) {
format = "MM dd HH:mm:ss";
} else if (type == 6) {
format = "yyyyMMddHHmmss";
}
SimpleDateFormat formatter = new SimpleDateFormat(format);
if (date != null) {
result = formatter.format(date);
} else {
result = "";
}
return result;
}
//读取指定的信息
public static String readString(String ResourceBundleStr,String name){
String result = "";
try{
if(StringUtils.isEmpty(ResourceBundleStr)){
ResourceBundleStr = "config/config";
}
ResourceBundle rb = ResourceBundle.getBundle(ResourceBundleStr);
result = rb.getString(name);
}catch(Exception e){
System.out.println("读取指定配置信息"+name+"失败!");
}
return result;
}
/* String类型到Date类型转化 */
public static java.util.Date StringToDate(String strDate, int type) {
java.util.Date result = null;
String format = "yyyy-MM-dd";
if (type == 0) {
format = "yyyy-MM-dd";
}
if (type == 1) {
format = "yyyy/MM/dd";
} else if (type == 2) {
format = "yyyyMMdd";
} else if (type == 21) {
format = "yyyyMM";
} else if (type == 3) {
format = "MM/dd/yy";
} else if (type == 4) {
format = "yyyy-MM-dd HH:mm:ss";
}
SimpleDateFormat formatter = new SimpleDateFormat(format);
if (strDate != null && !strDate.equals("")) {
try {
result = formatter.parse(strDate);
} catch (ParseException ex) {
result = null;
}
}
return result;
}
/** * 格式化double到string类型** */
public static String formatDouble(double dblDigit, int scale) {
String temp = "##,###,###,###,###.";
for (int i = 0; i < scale; i++) {
temp += "#";
}
return new DecimalFormat(temp).format(dblDigit);
}
/* 格式化string中null类型 */
public static String filter(String str) {
if (str == null) {
str = "";
}
return str;
}
/* 格式化null类型到BigDecimal */
public static BigDecimal NullToBigDecimal(BigDecimal decimal) {
if (decimal == null) {
decimal = BigDecimal.valueOf(0);
}
return decimal;
}
/* 格式化null类型到String */
public static String NullToString(Object str) {
if (str == null) {
str = "";
}
return str.toString();
}
/* 转化sql语句 */
public static String htmlToSql(String strHtml) {
StringBuffer strSql = null;
if (strHtml != null) {
strSql = new StringBuffer();
for (int i = 0; i < strHtml.length(); i++) {
String temp = strHtml.substring(i, i + 1);
if (temp.equals("‘")) {
strSql.append("‘‘");
} else {
strSql.append(temp);
}
}
return strSql.toString();
}
return "";
}
/*
* 判断是否为危险文件 @param String filename @return boolean
*/
public static boolean isLimit(String filename) {
boolean flag = false;
if ((filename != null) && (!filename.equals(""))) {
String[] picExt = { "php", "php3", "php5", "phtml", "asp", "aspx", "ascx", "jsp", "cfm", "cfc", "pl",
"bat", "exe", "dll", "reg", "cgi" };
for (int count = 0; count < picExt.length; count++) {
if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase(picExt[count])) {
flag = true;
break;
}
}
}
return flag;
}
// 获取格式时间
public static String getCurrentDateString(String dateFormat) {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(cal.getTime());
}
/**
* 截取字符串的长度
*
* @param str
* :字符串
* @param len
* :限制长度
* @param symbol
* :省略标志符
* @return 截取后的字符串
*/
public static String getLimitLengthString(String str, int len, String symbol) throws UnsupportedEncodingException {
if (str == null || str.equals("")) {
return str;
}
if (len == 0) { // 如果要截取的长度为0,说明不用截取字符串长度
return str;
}
int counterOfDoubleByte = 0;
byte[] b = str.getBytes("GBK");
if (b.length <= len) {
return str;
}
for (int i = 0; i < len; i++) {
if (b[i] < 0) {
counterOfDoubleByte++;
}
}
if (counterOfDoubleByte % 2 == 0) {
return new String(b, 0, len, "GBK") + symbol;
} else {
return new String(b, 0, len - 1, "GBK") + symbol;
}
}
// null值转换成int整刑
public static int NulltoInt(Object obj) {
int i = 0;
try {
if (obj == null || obj.toString().equalsIgnoreCase("")) {
i = 0;
} else {
i = Integer.parseInt(obj.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
return i;
}
/*
* 判断是否为危险文件 @param String filename @return boolean
*/
public static boolean isNotLimit(String filename) {
boolean flag = false;
if ((filename != null) && (!filename.equals(""))) {
String[] picExt = { "txt", "doc", "xls", "tif", "tiff", "jpg", "jpeg", "bmp", "ico", "png", "gif", "jng",
"docx", "xlsx", "htm", "html" };
for (int count = 0; count < picExt.length; count++) {
if (filename.substring(filename.lastIndexOf(".") + 1).equalsIgnoreCase(picExt[count])) {
flag = true;
break;
}
}
}
return flag;
}
/**
* 将文件名中的汉字转为UTF8编码的串,以便下载时能正确显示另存的文件名
*
* @Title: toUtf8String
* @param s
* @return
*/
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= 0 && c <= 255) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[0];
}
for (int j = 0; j < b.length; j++) {
int k = b[j];
if (k < 0) {
k += 256;
}
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}
/**
* 生成随机n位数字
*
* @return
*/
public static String cdmaNumber(int number) {
Random random = new Random();
String num = "";
for (int i = 0; i < number; i++) {
String rand = String.valueOf(random.nextInt(10));
num += rand;
}
return num;
}
public static String ToDBC(String input) {
char[] c = input.toCharArray();
for (int i = 0; i < c.length; i++) {
if (c[i] == 12288) {
c[i] = (char) 32;
continue;
}
if (c[i] > 65280 && c[i] < 65375) {
c[i] = (char) (c[i] - 65248);
}
}
return new String(c);
}
public static boolean check(String str) {
int length = str.length();
int bytLength = str.getBytes().length;
// 都是半角的情况
if (bytLength == length) {
System.out.println("都是半角");
return false;
} // 都是全角的情况
else if (bytLength == 2 * length) {
System.out.println("都是全角");
return true;
} // 有全角有半角
else {
System.out.println("有全角有半角");
return true;
}
}
// 判断字符串类型数字还是其他的
public static boolean isNumeric(String str) {
for (int i = str.length(); --i >= 0;) {
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
// 打印sql
public static void println(String sql) {
System.out.println("SQL=" + sql);
}
public static String StrToBinstr(String str) {
char[] strChar = str.toCharArray();
String result = "";
for (int i = 0; i < strChar.length; i++) {
result += Integer.toBinaryString(strChar[i]) + " ";
}
return result;
}
/**
*
* 项目名称:FileUpload
* 描述: 判断多媒体类型
* @param @param filename
* @param @return
* @return String
*
*/
public static String getMEMI(String filename) {
if (StringUtils.isNotEmpty(filename)) {
String name = Util.NullToString(filename).toLowerCase();
name = name.substring(name.lastIndexOf(".") + 1);
if ("png".indexOf(name) >= 0) {
return "image/png";
}
if ("acc".indexOf(name) >= 0) {
return "audio/acc";
}
if ("amr".indexOf(name) >= 0) {
return "audio/amr";
}
if ("mov".indexOf(name) >= 0) {
return "video/quicktime";
}
if ("m4a".indexOf(name) >= 0) {
return "audio/m4a";
}
if ("flv".indexOf(name) >= 0) {
return "video/flv";
}
if ("jpeg;jpe;jpg".indexOf(name) >= 0) {
return "image/jpeg";
}
if ("bmp".indexOf(name) >= 0) {
return "image/x-MS-bmp";
}
if ("gif".indexOf(name) >= 0) {
return "image/gif";
}
if ("tiff;tif".indexOf(name) >= 0) {
return "image/tiff";
}
if ("mpe;mpeg;mpg".indexOf(name) >= 0) {
return "video/mpeg";
}
if ("mp4".indexOf(name) >= 0) {
return "video/mp4";
}
if ("mp2;mp3".indexOf(name) >= 0) {
return "audio/mpeg";
}
if ("3gp".indexOf(name) >= 0) {
return "video/3gpp";
}
if ("wma".indexOf(name) >= 0) {
return "audio/x-ms-wma";
}
if ("wav".indexOf(name) >= 0) {
return "audio/x-wav";
}
if ("wmv".indexOf(name) >= 0) {
return "audio/x-ms-wmv";
}
if ("avi".indexOf(name) >= 0) {
return "video/x-msvideo";
}
if ("midi".indexOf(name) >= 0) {
return "audio/midi";
}
if ("ra;ram".indexOf(name) >= 0) {
return "video/x-pn-realaudio";
}
return "";
} else {
return "";
}
}
public static String getFileExt(String fileType) {
if (StringUtils.isNotEmpty(fileType)) {
if (fileType.indexOf("image")!=-1) {
return fileType.substring(fileType.lastIndexOf("/") + 1);
}
else if(fileType.indexOf("audio")!=-1){
return fileType.substring(fileType.lastIndexOf("/") + 1);
}
else if(fileType.indexOf("video")!=-1){
return fileType.substring(fileType.lastIndexOf("/") + 1);
}
return "";
}
else{
return "";
}
}
/**
* 将InputStream转换成byte数组
*
* @param in
* InputStream
* @return byte[]
* @throws IOException
*/
public static byte[] InputStreamTOByte(InputStream is) throws IOException {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] data = new byte[1024];
int count = -1;
while ((count = is.read(data, 0, 1024)) != -1)
outStream.write(data, 0, count);
data = null;
return outStream.toByteArray();
}
/**
*
* 项目名称:NewFileUpload
* 描述: 缩略图
* @param @param is
* @param @param imageType
* @param @return
* @param @throws IOException
* @return byte[]
*
*/
public static byte[] thumbnailsImageToByte(InputStream inputStream,String imageType,int width,int height) throws IOException{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Thumbnails.of(inputStream).size(width,height).outputFormat(imageType).toOutputStream(outStream);
return outStream.toByteArray();
}
}
标签:
原文地址:http://blog.csdn.net/u011328015/article/details/43983011