文章目录
短信发送
public class Sms {public static void main(String[] args) {sendMsg("写手机号","模板json","模板json");}public static void sendMsg(String phone,String name,String days){DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKey ID", "AccessKey Secret");IAcsClient client = new DefaultAcsClient(profile);CommonRequest request = new CommonRequest();request.setSysMethod(MethodType.POST);request.setSysDomain("dysmsapi.aliyuncs.com");request.setSysVersion("2017-05-25");request.setSysAction("SendSms");request.putQueryParameter("RegionId", "cn-hangzhou");request.putQueryParameter("PhoneNumbers", phone);request.putQueryParameter("SignName", "模板名称");request.putQueryParameter("TemplateCode", "SMS_199580007");request.putQueryParameter("TemplateParam", "{name:"+name+",days:"+days+"}");try {CommonResponse response = client.getCommonResponse(request);System.out.println(response.getData());} catch (ServerException e) {e.printStackTrace();} catch (ClientException e) {e.printStackTrace();}}
}
邮件发送
- 依赖:mail.jar
- 发信邮箱需开启POP3/SMTP服务,并获得授权码。
手写测试
public class SendEmail {public static void main(String[] args) throws Exception {String from = "发信邮箱";String upwd = "授权码";Properties properties = new Properties();properties.put("mail.transport.protocol", "smtp");properties.put("mail.smtp.host", "smtp.qq.com");properties.put("mail.smtp.port", 465);properties.put("mail.smtp.auth", "true");properties.put("mail.smtp.ssl.enable", "true");properties.put("mail.debug", "true");Session session = Session.getDefaultInstance(properties);Message message = new MimeMessage(session);message.setSubject("新年快乐");message.setFrom(new InternetAddress(from));message.setRecipient(Message.RecipientType.TO,new InternetAddress("收件邮箱,可以是数组"));message.setText("祝大家新年快乐,假期愉快!");Transport transport = session.getTransport();transport.connect(from,upwd);transport.sendMessage(message,message.getAllRecipients());}
}
使用封装的工具类
public class EmailTest {public static void main(String[] args) throws Exception{try{MailSender mail = new MailSender();mail.setHost("smtp.qq.com"); mail.setFrom("发件邮箱"); mail.addTo("目标地址"); mail.addFileAcc("附件"); mail.addByteAcc("我是大怪兽".getBytes(), MailSender.DEFAULT_CONTENT_TYPE, "我是谁.txt");mail.setAuth(true);
mail.setUser("发件邮箱"); mail.setPassword("授权码"); mail.setSubject("邮件主题"); mail.setText("您好,点击一下链接进行邮箱验证:
测试邮件
这是一封java发出的邮件");mail.setCharset("UTF-8"); mail.setContentType("text/html"); mail.send(); }catch(Exception e){e.printStackTrace();}}}
public class MailSender
{public static final String DEFAULT_CONTENT_TYPE = "text/plain";public static final String DEFAULT_ENCODING = GeneralHelper.DEFAULT_ENCODING;public static final int DEFAULT_PORT = 465;private String host = "";private String from = "";private String user = "";private String password = "";private String subject = "";private String text = "";private String contentType = DEFAULT_CONTENT_TYPE;private String charset = DEFAULT_ENCODING;private int port = DEFAULT_PORT;private boolean auth = true;private boolean needReceipt = false;private Date sentDate = null;private List<String> to = new ArrayList<String>();private List<String> cc = new ArrayList<String>();private List<String> bcc = new ArrayList<String>();private List<String> replyTo = new ArrayList<String>();private List<String> fileAcc = new ArrayList<String>();private List<MimeBodyPart> byteAcc = new ArrayList<MimeBodyPart>();public int getPort(){return port;}public void setPort(int port){this.port = port;}public boolean isAuth(){return auth;}public void setAuth(boolean auth){this.auth = auth;}public String getCharset(){return charset;}public void setCharset(String charset){this.charset = charset;}public String getContentType(){return contentType;}public void setContentType(String contentType){this.contentType = contentType;}public boolean isNeedReceipt(){return needReceipt;}public void setNeedReceipt(boolean needReceipt){this.needReceipt = needReceipt;}public String getFrom(){return from;}public void setFrom(String from){this.from = from;}public String getHost(){return host;}public void setHost(String host){this.host = host;}public String getPassword(){return password;}public void setPassword(String password){this.password = password;}public String getSubject(){return subject;}public void setSubject(String subject){this.subject = subject;}public String getText(){return text;}public void setText(String text){this.text = text;}public Date getSentDate(){return sentDate;}public void setSentDate(Date sentDate){this.sentDate = sentDate;}public String getUser(){return user;}public void setUser(String user){this.user = user;}public List<String> getFileAcc(){return fileAcc;}public List<MimeBodyPart> getByteAcc(){return byteAcc;}public void setFileAcc(List<String> accessory){this.fileAcc = accessory;}public void setByteAcc(List<MimeBodyPart> accessory){this.byteAcc = accessory;}public List<String> getReplyTo(){return replyTo;}public List<String> getTo(){return to;}public void setTo(List<String> to){this.to = to;}public List<String> getCc(){return cc;}public void setCc(List<String> cc){this.cc = cc;}public List<String> getBcc(){return bcc;}public void setBcc(List<String> bcc){this.bcc = bcc;}public void addFileAcc(String accessory){fileAcc.add(accessory);}public void addByteAcc(byte[] accessory, String type, String fileName) throws Exception{ByteArrayDataSource ds = new ByteArrayDataSource(accessory, type, fileName);fileName = MimeUtility.encodeText(fileName, charset, "B");MimeBodyPart mimeFile = new MimeBodyPart();mimeFile.setDataHandler(new DataHandler(ds));mimeFile.setFileName(fileName);byteAcc.add(mimeFile);}public void addReplyTo(String address){replyTo.add(address);}public void addTo(String address){to.add(address);}public void addCc(String address){cc.add(address);}public void addBcc(String address){bcc.add(address);}public void send() throws Exception{Transport transport = null;try{Properties props = new Properties();props.put("mail.transport.protocol", "smtp");props.put("mail.smtp.auth", Boolean.toString(auth));props.put("mail.smtp.ssl.enable", "true");Session session = Session.getDefaultInstance(props, null);MimeMessage msg = new MimeMessage(session);msg.setFrom(new InternetAddress(from));for(String i : to)msg.addRecipient(Message.RecipientType.TO, new InternetAddress(i));for(String i : cc)msg.addRecipient(Message.RecipientType.CC, new InternetAddress(i));for(String i : bcc)msg.addRecipient(Message.RecipientType.BCC, new InternetAddress(i));if(replyTo.size() > 0){InternetAddress[] replyAddress = new InternetAddress[replyTo.size()];for(int i = 0; i < replyAddress.length; i++)replyAddress[i] = new InternetAddress((String)replyTo.get(i));msg.setReplyTo(replyAddress);}if(needReceipt)msg.addHeader("Disposition-Notification-To", from);if(sentDate != null)msg.setSentDate(sentDate);elsemsg.setSentDate(new Date());msg.setSubject(subject, charset);MimeMultipart mm = new MimeMultipart();MimeBodyPart mbText = new MimeBodyPart();mbText.setContent(text, contentType + ";charset=" + charset);mm.addBodyPart(mbText);for(String filePath : fileAcc){String fileName = (new File(filePath)).getName();fileName = MimeUtility.encodeText(fileName, charset, "B");MimeBodyPart mbFile = new MimeBodyPart();DataSource datasource = new FileDataSource(filePath);mbFile.setDataHandler(new DataHandler(datasource));mbFile.setFileName(fileName);mm.addBodyPart(mbFile);}for(MimeBodyPart part : byteAcc)mm.addBodyPart(part);msg.setContent(mm);msg.saveChanges();transport = session.getTransport();transport.connect(host, port, user, password);transport.sendMessage(msg, msg.getAllRecipients());}finally{if(transport != null) try{ transport.close(); } catch (Exception e) { }}}
}
- ByteArrayDataSource .java
class ByteArrayDataSource implements DataSource
{public static final String DEFAULT_ENCODING = GeneralHelper.DEFAULT_ENCODING;private ByteArrayOutputStream baos = null;private String type = "application/octet-stream";private String name = "ByteArrayDataSource";private void init(String type, String name){if(type != null)this.type = type;if(name != null)this.name = name; }public ByteArrayDataSource(byte[] data, String type, String name) throws IOException{ByteArrayInputStream Bis = null;try{Bis = new ByteArrayInputStream(data);this.byteArrayDataSource(Bis, type, name);}catch (IOException ioex){throw ioex;}finally{try{if (Bis != null){Bis.close();}}catch (IOException ignored){}}}public ByteArrayDataSource(InputStream aIs, String type, String name) throws IOException{this.byteArrayDataSource(aIs, type, name);}private void byteArrayDataSource(InputStream aIs, String type, String name) throws IOException{init(type, name);BufferedInputStream bis = null;BufferedOutputStream bos = null;try{int length = 0;byte[] buffer = new byte[4096];bis = new BufferedInputStream(aIs);baos = new ByteArrayOutputStream();bos = new BufferedOutputStream(baos);while ((length = bis.read(buffer)) != -1){bos.write(buffer, 0, length);} }catch (IOException ioex){throw ioex;}finally{try{if (bis != null){bis.close();}if (baos != null){baos.close();}if (bos != null){bos.close();}}catch (IOException ignored){}}}public ByteArrayDataSource(String data, String type, String name) throws IOException{this(data, DEFAULT_ENCODING, type, name);}public ByteArrayDataSource(String data, String encoding, String type, String name) throws IOException{init(type, name);try{baos = new ByteArrayOutputStream();baos.write(data.getBytes(encoding));}catch (UnsupportedEncodingException uex){}catch (IOException ignored){}finally{try{if (baos != null){baos.close();}}catch (IOException ignored){}}}public String getContentType(){return type;}public InputStream getInputStream() throws IOException{if (baos == null){throw new IOException("no data");}return new ByteArrayInputStream(baos.toByteArray());}public String getName(){return name;}public OutputStream getOutputStream() throws IOException{baos = new ByteArrayOutputStream();return baos;}
}
public class GeneralHelper
{private static final String[] SHORT_DATE_PATTERN = {"yyyy-MM-dd", "yyyy/MM/dd", "yyyy\\MM\\dd", "yyyyMMdd"};private static final String[] LONG_DATE_PATTERN = {"yyyy-MM-dd HH:mm:ss", "yyyy/MM/dd HH:mm:ss", "yyyy\\MM\\dd HH:mm:ss", "yyyyMMddHHmmss"};private static final String[] LONG_DATE_PATTERN_WITH_MILSEC = {"yyyy-MM-dd HH:mm:ss.SSS", "yyyy/MM/dd HH:mm:ss.SSS", "yyyy\\MM\\dd HH:mm:ss.SSS", "yyyyMMddHHmmssSSS"};public static final String EMPTY_STRING = "";public static final String DEFAULT_ENCODING = "UTF-8";public static final String OS_PLATFORM = getOSName();public static final boolean IS_WINDOWS_PLATFORM = isWindowsPlatform();public static final String NEWLINE_CHAR = IS_WINDOWS_PLATFORM ? "\r\n" : "\n";public final static boolean isStrNotEmpty(String str){return str != null && str.length() != 0;}public final static boolean isTrimStrNotEmpty(String str){boolean result = isStrNotEmpty(str);return result ? isStrNotEmpty(str.trim()) : result;}public final static boolean isStrEmpty(String str){return !isStrNotEmpty(str);}public final static boolean isTrimStrEmpty(String str){boolean result = isStrEmpty(str);return result ? result : isStrEmpty(str.trim());}public final static String safeString(String str){if(str == null)str = "";return str;}public final static String safeString(Object obj){if(obj == null)return "";return obj.toString();}public final static String safeTrimString(String str){return safeString(str).trim();}public final static boolean isStrNumeric(String str){return str.matches("^0$|^\\-?[1-9]+[0-9]*$");}public final static boolean isStrEmailAddress(String str){return str.matches("^[a-z0-9_\\-]+(\\.[_a-z0-9\\-]+)*@([_a-z0-9\\-]+\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)$");}public final static boolean isStrIPAddress(String str){return str.matches("^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");}public final static boolean isStrLink(String str){return str.matches("]*href=\\\"[^\\s\\\"]+\\\"[^>]*>[^<]*<\\/a>");}public final static boolean isStrURL(String str){return str.matches("^((https?|ftp|news):\\/\\/)?([a-z]([a-z0-9\\-]*\\.)+([a-z]{2}|aero|arpa|biz|com|coop|edu|gov|info|int|jobs|mil|museum|name|nato|net|org|pro|travel)|(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]))(\\/[a-z0-9_\\-\\.~]+)*(\\/([a-z0-9_\\-\\.]*)(\\?[a-z0-9+_\\-\\.%=&]*)?)?(#[a-z][a-z0-9_]*)?$");}public static final String escapeRegexChars(String str, char ... ignores){final char ESCAPE_CHAR = '\\';final char[] REGEX_CHARS = {'.', ',', '?', '+', '-', '*', '^', '$', '|', '&', '{', '}', '[', ']', '(', ')', '\\'};char[] regex_chars = REGEX_CHARS;if(ignores.length > 0){Set<Character> cs = new HashSet<Character>(REGEX_CHARS.length);for(int i = 0; i < REGEX_CHARS.length; i++)cs.add(REGEX_CHARS[i]);for(int i = 0; i < ignores.length; i++)cs.remove(ignores[i]);int i = 0;regex_chars = new char[cs.size()];Iterator<Character> it = cs.iterator();while(it.hasNext())regex_chars[i++] = it.next(); }StringBuilder sb = new StringBuilder();for(int i = 0; i < str.length(); i++){char c = str.charAt(i);for(int j = 0; j < regex_chars.length; j++){if(c == regex_chars[j]){sb.append(ESCAPE_CHAR);break;}}sb.append(c);}return sb.toString();}public final static String[] splitStr(String str){return splitStr(str, " \t\n\r\f,;");}public final static String[] splitStr(String str, String delim){StringTokenizer st = new StringTokenizer(str, delim);String[] array = new String[st.countTokens()];int i = 0;while(st.hasMoreTokens())array[i++] = st.nextToken();return array;}public final static boolean waitFor(long period){if(period > 0){try{Thread.sleep(period);}catch(Exception e){return false;}}elseThread.yield();return true;}public final static boolean waitFor(long period, TimeUnit unit){return waitFor(unit.toMillis(period));}public final static Integer str2Int(String s){Integer returnVal;try {returnVal = Integer.decode(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static int str2Int(String s, int d){int returnVal;try {returnVal = Integer.parseInt(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static int str2Int_0(String s){return str2Int(s, 0);}public final static Short str2Short(String s){Short returnVal;try {returnVal = Short.decode(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static short str2Short(String s, short d){short returnVal;try {returnVal = Short.parseShort(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static short str2Short_0(String s){return str2Short(s, (short)0);}public final static Long str2Long(String s){Long returnVal;try {returnVal = Long.decode(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static long str2Long(String s, long d){long returnVal;try {returnVal = Long.parseLong(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static long str2Long_0(String s){return str2Long(s, 0L);}public final static Float str2Float(String s){Float returnVal;try {returnVal = Float.valueOf(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static float str2Float(String s, float d){float returnVal;try {returnVal = Float.parseFloat(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static float str2Float_0(String s){return str2Float(s, 0F);}public final static Double str2Double(String s){Double returnVal;try {returnVal = Double.valueOf(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static double str2Double(String s, double d){double returnVal;try {returnVal = Double.parseDouble(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static double str2Double_0(String s){return str2Double(s, 0D);}public final static Byte str2Byte(String s){Byte returnVal;try {returnVal = Byte.decode(safeTrimString(s));} catch(Exception e) {returnVal = null;}return returnVal;}public final static byte str2Byte(String s, byte d){byte returnVal;try {returnVal = Byte.parseByte(safeTrimString(s));} catch(Exception e) {returnVal = d;}return returnVal;}public final static byte str2Byte_0(String s){return str2Byte(s, (byte)0);}public final static Character str2Char(String s){Character returnVal;try {returnVal = safeTrimString(s).charAt(0);} catch(Exception e) {returnVal = null;}return returnVal;}public final static char str2Char(String s, char d){char returnVal;try {returnVal = safeTrimString(s).charAt(0);} catch(Exception e) {returnVal = d;}return returnVal;}public final static char str2Char_0(String s){return str2Char(s, Character.MIN_VALUE);}public final static Boolean str2Boolean(String s){return Boolean.valueOf(safeTrimString(s));}public final static boolean str2Boolean(String s, boolean d){s = safeTrimString(s);if(s.equalsIgnoreCase("true"))return true;else if(s.equalsIgnoreCase("false"))return false;return d;}public final static boolean str2Boolean_False(String s){return str2Boolean(s, false);}public final static Date str2Date(String str, String format){Date date = null;try{DateFormat df = new SimpleDateFormat(format);date = df.parse(safeTrimString(str));}catch(Exception e){}return date;}public final static Date str2Date(String str){Date date = null;try{final char SEPARATOR = '-';final String[] PATTERN = {"yyyy", "MM", "dd", "HH", "mm", "ss", "SSS"};String[] values = safeTrimString(str).split("\\D");String[] element = new String[values.length];int length = 0;for(String e : values){e = e.trim();if(e.length() != 0){element[length++] = e;if(length == PATTERN.length)break;}}if(length > 0){StringBuilder value = new StringBuilder();if(length > 1){for(int i = 0; i < length; ++i){value.append(element[i]);value.append(SEPARATOR);}}else{String src = element[0];int remain = src.length();int pos = 0;int i = 0;for(i = 0; remain > 0 && i < PATTERN.length; ++i){int p_length = PATTERN[i].length();int v_length = Math.min(p_length, remain);String v = src.substring(pos, pos + v_length);pos += v_length;remain -= v_length;value.append(v);value.append(SEPARATOR);}length = i;}StringBuilder format = new StringBuilder();for(int i = 0; i < length; ++i){format.append(PATTERN[i]);format.append(SEPARATOR);}date = str2Date(value.toString(), format.toString());}}catch(Exception e){}return date;}public final static Date str2Date(String str, String[] Patterns){Date date = null;for(int i = 0; i < Patterns.length; ++i){date = str2Date(str, Patterns[i]);if( date != null)break;}return date;}public final static Date str2ShortDate(String str){return str2Date(str, SHORT_DATE_PATTERN);}public final static Date str2LongDate(String str){return str2Date(str, LONG_DATE_PATTERN);}public final static Date str2LongDateWithMilliSecond(String str){return str2Date(str, LONG_DATE_PATTERN_WITH_MILSEC);}public static interface TypeHandler<T>{T handle(String v);}public static final <T> T str2Object(Class<T> type, String v){return str2Object(type, v, null);}@SuppressWarnings("unchecked")public static final <T> T str2Object(Class<T> type, String v, TypeHandler<T> handler){Object param = null;if(handler != null)return handler.handle(v);if(type == String.class)param = safeTrimString(v);else if(type == int.class)param = str2Int_0(v);else if(type == long.class)param = str2Long_0(v);else if(type == byte.class)param = str2Byte_0(v);else if(type == char.class)param = str2Char_0(v);else if(type == float.class)param = str2Float_0(v);else if(type == double.class)param = str2Double_0(v);else if(type == short.class)param = str2Short_0(v);else if(type == boolean.class)param = str2Boolean_False(v);else if(type == Integer.class)param = str2Int(v);else if(type == Long.class)param = str2Long(v);else if(type == Byte.class)param = str2Byte(v);else if(type == Character.class)param = str2Char(v);else if(type == Float.class)param = str2Float(v);else if(type == Double.class)param = str2Double(v);else if(type == Short.class)param = str2Short(v);else if(type == Boolean.class)param = str2Boolean(v);else if(Date.class.isAssignableFrom(type))param = str2Date(v);elsethrow new IllegalArgumentException(String.format("object type '%s' not valid", type));return (T)param;}public static final Object[] object2Array(Object obj){Object[] array;if(obj == null)array = new Object[] {obj};else if(obj.getClass().isArray()){Class<?> clazz = obj.getClass().getComponentType();if(Object.class.isAssignableFrom(clazz))array = (Object[])obj;else{int length = Array.getLength(obj);if(length > 0){array = new Object[length];for(int i = 0; i < length; i++)array[i] = Array.get(obj, i);}elsearray = new Object[0];}}else if(obj instanceof Collection<?>)array = ((Collection<?>)obj).toArray();else if(obj instanceof Iterable<?>){List<Object> list = new ArrayList<Object>();Iterator<?> it = ((Iterable<?>)obj).iterator();while(it.hasNext())list.add(it.next());array = list.toArray();}else if(obj instanceof Iterator){List<Object> list = new ArrayList<Object>();Iterator<?> it = (Iterator<?>)obj;while(it.hasNext())list.add(it.next());array = list.toArray();}else if(obj instanceof Enumeration<?>){List<Object> list = new ArrayList<Object>();Enumeration<?> it = (Enumeration<?>)obj;while(it.hasMoreElements())list.add(it.nextElement());array = list.toArray();}elsearray = new Object[] {obj};return array;}public final static Date addDate(Date date, int value){return addDate(date, value, true);}public final static Date addDate(Date date, int value, boolean trimTime){return addTime(date, Calendar.DATE, value, trimTime);}public final static Date addTime(Date date, int field, int value){return addTime(date, field, value, false);}public final static Date addTime(Date date, int field, int value, boolean trimTime){Calendar c = Calendar.getInstance();c.setTime(date);c.add(field, value);if(trimTime){c.set(Calendar.HOUR, 0);c.set(Calendar.MINUTE, 0);c.set(Calendar.SECOND, 0);c.set(Calendar.MILLISECOND, 0);}return c.getTime();}public final static String date2Str(Date date, String format){DateFormat df = new SimpleDateFormat(format);return df.format(date);}public static final String regularSQLStr(String str, boolean includeWidlcard){str = str.replace("'", "''");if(includeWidlcard){str = str.replace('*', '%');str = str.replace('?', '_');}return str;}public static final ClassLoader getClassLoader(Class<?> clazz){ClassLoader loader = clazz.getClassLoader();if(loader == null)loader = Thread.currentThread().getContextClassLoader();return loader;}public static final Class<?> loadClass(String className){Class<?> clazz = null;ClassLoader loader = getClassLoader(GeneralHelper.class);try{clazz = loader.loadClass(className);}catch(ClassNotFoundException e){}return clazz;}public static final Class<?> classForName(String name){Class<?> clazz = null;try{clazz = Class.forName(name);}catch(ClassNotFoundException e){}return clazz;}public static final Class<?> classForName(String name, boolean initialize, ClassLoader loader){Class<?> clazz = null;try{clazz = Class.forName(name, initialize, loader);}catch(ClassNotFoundException e){}return clazz;}public static final URL getClassResource(Class<?> clazz, String resPath){URL url = clazz.getResource(resPath);if(url == null){ClassLoader loader = clazz.getClassLoader();if(loader != null) url = loader.getResource(resPath);if(url == null){loader = Thread.currentThread().getContextClassLoader();if(loader != null) url = loader.getResource(resPath);}}return url;}public static final List<URL> getClassResources(Class<?> clazz, String resPath){List<URL> urlList = new ArrayList<URL>();Enumeration<URL> urls = null;try{ClassLoader loader = clazz.getClassLoader();if(loader != null) urls = loader.getResources(resPath);if(urls == null || !urls.hasMoreElements()){loader = Thread.currentThread().getContextClassLoader();if(loader != null) urls = loader.getResources(resPath);}}catch(IOException e){throw new RuntimeException(e);}if(urls != null){while(urls.hasMoreElements())urlList.add(urls.nextElement());}return urlList;}public static final InputStream getClassResourceAsStream(Class<?> clazz, String resPath){InputStream is = clazz.getResourceAsStream(resPath);if(is == null){ClassLoader loader = clazz.getClassLoader();if(loader != null) is = loader.getResourceAsStream(resPath);if(is == null){loader = Thread.currentThread().getContextClassLoader();if(loader != null) is = loader.getResourceAsStream(resPath);}}return is;}public static final String getClassResourcePath(Class<?> clazz, String resPath){return getClassResourcePath(clazz, resPath, DEFAULT_ENCODING);}public static final String getClassResourcePath(Class<?> clazz, String resPath, String pathEnc){String path = null;try{URL url = getClassResource(clazz, resPath);if(url != null){path = url.getPath();path = URLDecoder.decode(path, pathEnc);}}catch(UnsupportedEncodingException e){throw new RuntimeException(e);}return path;}public static final List<String> getClassResourcePaths(Class<?> clazz, String resPath){return getClassResourcePaths(clazz, resPath, DEFAULT_ENCODING);}public static final List<String> getClassResourcePaths(Class<?> clazz, String resPath, String pathEnc){List<String> pathList = new ArrayList<String>();try{List<URL> urlList = getClassResources(clazz, resPath);for(URL url : urlList){String path = URLDecoder.decode(url.getPath(), pathEnc);pathList.add(path);}}catch(UnsupportedEncodingException e){throw new RuntimeException(e);}return pathList;}public static final String getClassPath(Class<?> clazz){return getClassResourcePath(clazz, ".");}public static final String getResourceMessage(Locale locale, String resource, String key, Object ... params){ResourceBundle bundle = ResourceBundle.getBundle(resource, locale);String msg = bundle.getString(key);if(params != null && params.length > 0)msg = MessageFormat.format(msg, params);return msg;}public static final String getResourceMessage(String resource, String key, Object ... params){return getResourceMessage(Locale.getDefault(), resource, key, params);}public static final List<String> getExceptionMessageStack(Throwable e, int levels){List<String> list = new ArrayList<String>();if(levels == 0)levels = Integer.MAX_VALUE;for(int i = 0; i < levels; ++i){StringBuilder sb = new StringBuilder();if(i > 0)sb.append("Caused by -> ");sb.append(e.getClass().getName());String msg = e.getLocalizedMessage();if(msg != null)sb.append(": ").append(msg);list.add(sb.toString());e = e.getCause();if(e == null)break;}return list;}public static final List<String> getExceptionMessageStack(Throwable e){return getExceptionMessageStack(e, 0);}public static final void printExceptionMessageStack(Throwable e, int levels, PrintStream ps){List<String> list = getExceptionMessageStack(e, levels);for(String msg : list)ps.println(msg);}public static final void printExceptionMessageStack(Throwable e, int levels){printExceptionMessageStack(e, levels, System.err);}public static final void printExceptionMessageStack(Throwable e, PrintStream ps){printExceptionMessageStack(e, 0, ps);}public static final void printExceptionMessageStack(Throwable e){printExceptionMessageStack(e, 0);}public static final <K, V> boolean tryPut(Map<K, V> map, K key, V value){return tryPut(map, key, value, false);}public static final <K, V> boolean tryPut(Map<K, V> map, K key, V value, boolean replace){if(replace || !map.containsKey(key)){map.put(key, value);return true;}return false;}public static final <K, V> boolean syncTryPut(Map<K, V> map, K key, V value){return syncTryPut(map, key, value, false);}public static final <K, V> boolean syncTryPut(Map<K, V> map, K key, V value, boolean replace){synchronized(map){return tryPut(map, key, value, replace);}}public static final <K, V> int tryPutAll(Map<K, V> map, Map<K, V> src){return tryPutAll(map, src, false);}public static final <K, V> int tryPutAll(Map<K, V> map, Map<K, V> src, boolean replace){if(replace){map.putAll(src);return src.size();}int count = 0;Set<Entry<K, V>> entries = src.entrySet();for(Entry<K, V> e : entries){if(!map.containsKey(e.getKey())){map.put(e.getKey(), e.getValue());++count;}}return count;}public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src){return syncTryPutAll(map, src, false);}public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src, boolean replace){synchronized(map){return tryPutAll(map, src, replace);}}public static final <K, V> boolean tryRemove(Map<K, V> map, K key){if(map.containsKey(key)){map.remove(key);return true;}return false;}public static final <K, V> boolean syncTryRemove(Map<K, V> map, K key){synchronized(map){return tryRemove(map, key);}}public static final <K, V> void tryClear(Map<K, V> map){map.clear();}public static final <K, V> void syncTryClear(Map<K, V> map){synchronized(map){tryClear(map);}}public static final int getProcessId(){return Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]);}public static final String getJavaVersion(){return System.getProperty("java.version");}public static final String getOSName(){return System.getProperty("os.name");}public static final boolean isWindowsPlatform(){return File.pathSeparatorChar == ';';}public static class PinYinComparator implements Comparator<String>{@Overridepublic int compare(String o1, String o2){java.text.Collator cmp = java.text.Collator.getInstance(Locale.CHINA);return cmp.compare(o1, o2);}}public static class FileNameFileFilter implements FileFilter{protected static final int FLAGS = IS_WINDOWS_PLATFORM ? Pattern.CASE_INSENSITIVE : 0;Pattern pattern;public FileNameFileFilter(String name){String exp = name;exp = exp.replace('.', '#');exp = exp.replaceAll("#", "\\\\.");exp = exp.replace('*', '#');exp = exp.replaceAll("#", ".*");exp = exp.replace('?', '#');exp = exp.replaceAll("#", ".?");exp = "^" + exp + "$";pattern = Pattern.compile(exp, FLAGS);}@Overridepublic boolean accept(File file){Matcher matcher = pattern.matcher(file.getName());return matcher.matches();}}public static int compare(Date firstDate, Date secondDate) {Calendar firstCalendar = null;if (firstDate != null) {firstCalendar = Calendar.getInstance();firstCalendar.setTime(firstDate);}Calendar secondCalendar = null;if (firstDate != null) {secondCalendar = Calendar.getInstance();secondCalendar.setTime(secondDate);}try {return firstCalendar.compareTo(secondCalendar);} catch (NullPointerException e) {throw new IllegalArgumentException(e);} catch (IllegalArgumentException e) {throw new IllegalArgumentException(e);}}public static void removeDuplicateWithObject(List list) {Set set = new HashSet();List newList = new ArrayList();for (Iterator iter = list.iterator(); iter.hasNext();) {Object element = iter.next();if (set.add(element))newList.add(element);}list.clear();list.addAll(newList);}public static List<Object> mapByList(HashMap<String, Object> map){List<Object> list = new ArrayList<Object>();Set<Entry<String, Object>> set = map.entrySet();Iterator<Entry<String, Object>> it = set.iterator();while(it.hasNext()){Entry<String, Object> entry = it.next();list.add(entry.getValue());}return list;}public static String dateConvertString(Date date){SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");String str=sdf.format(date);return str;}public static List<String> splitString(String str, int length) {List<String> list = new ArrayList<String>();for (int i = 0; i < str.length(); i += length) {int endIndex = i + length;if (endIndex <= str.length()) {list.add(str.substring(i, i + length));} else {list.add(str.substring(i, str.length() - 1));}}return list;}public static String toString(List<String> list, String separator) {StringBuffer stringBuffer = new StringBuffer();for (String str : list) {stringBuffer.append(separator + str);}stringBuffer.deleteCharAt(0);return stringBuffer.toString();} @SuppressWarnings({ "unchecked", "rawtypes" })public static List getRandomArray(List list) { Collections.shuffle(list); return list;}public static List subList(List list ,int num) {List newList = getRandomArray(list);return newList.subList(0, num);}}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!