邮件和短信发送

    科技2025-04-30  20

    文章目录

    短信发送邮件发送手写测试使用封装的工具类

    短信发送

    使用阿里云接口。 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");// 主机名 smtp.163.com properties.put("mail.smtp.port", 465);// 端口号 465安全连接 properties.put("mail.smtp.auth", "true"); properties.put("mail.smtp.ssl.enable", "true");// 设置是否使用ssl安全连接 ---一般都使用 properties.put("mail.debug", "true");// 设置是否显示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()); } }

    使用封装的工具类

    EmailTest.java public class EmailTest { public static void main(String[] args) throws Exception { try { MailSender mail = new MailSender(); mail.setHost("smtp.qq.com"); // 设置 SMTP 主机 mail.setFrom("发件邮箱"); // 设置发送者地址xxx@163.com mail.addTo("目标地址"); // 添加发送目标地址xxx@xx.com //mail.addCc("抄送的邮箱地址"); // 添加抄送目标地址xxx@xx.com mail.addFileAcc("附件"); // 添加文件附件 // 从 byte[] 中读取数据并添加为附件(这个功能有时非常有用) mail.addByteAcc("我是大怪兽".getBytes(), MailSender.DEFAULT_CONTENT_TYPE, "我是谁.txt"); //mail.addReplyTo(mail.getFrom()); // 添加回复地址 mail.setAuth(true); // 设置验证模式 // mail.setNeedReceipt(true); // 设置是否需要回执 mail.setUser("发件邮箱"); // 设置邮箱登录名 mail.setPassword("授权码"); // 设置邮箱登录密码 mail.setSubject("邮件主题"); // 设置邮件主题 mail.setText("<h1>您好,点击一下链接进行邮箱验证:</h1>测试邮件<br>这是一封java发出的邮件");// 设置邮件文本内容 mail.setCharset("UTF-8"); // 设置邮件文本内容编码 mail.setContentType("text/html"); // 设置邮件文本内容格式 mail.send(); // 发送邮件 } catch(Exception e) { e.printStackTrace(); } } } MailSender.java /** * ClassName: MailSender * @Description: 邮件发送 * @author Mr.Wang * @date 2017年11月11日 */ public class MailSender { /** 默认 Content Type -> text/plain */ public static final String DEFAULT_CONTENT_TYPE = "text/plain"; /** 默认字符编码 -> UTF-8 */ public static final String DEFAULT_ENCODING = GeneralHelper.DEFAULT_ENCODING; /** 默认 SMTP 端口 -> 25 */ 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); } /** 添加 byte array 形式的附件 */ public void addByteAcc(byte[] accessory, String type, String fileName) throws Exception { ByteArrayDataSource ds = new ByteArrayDataSource(accessory, type, fileName); //BASE64Encoder enc = new BASE64Encoder(); //fileName = "=?GBK?B?" + enc.encode(fileName.getBytes()) + "?="; fileName = MimeUtility.encodeText(fileName, charset, "B"); MimeBodyPart mimeFile = new MimeBodyPart(); mimeFile.setDataHandler(new DataHandler(ds)); //mimeFile.setFileName(ds.getName()); 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); else msg.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 /** Byte Array 数据源,发送字节数组附件时用到 */ 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) { // Do something! } catch (IOException ignored) { // Ignore } 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; } } GeneralHelper.java /** * ClassName: GeneralHelper * @Description: 通用方法帮助类 * @author Mr.Wang * @date 2017年11月11日 */ 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(); /** 当前操作系统平台是否为 Windows */ public static final boolean IS_WINDOWS_PLATFORM = isWindowsPlatform(); /** 当前操作系统平台的换行符 */ public static final String NEWLINE_CHAR = IS_WINDOWS_PLATFORM ? "\r\n" : "\n"; /** 检查字符串不为 null 或空字符串 */ public final static boolean isStrNotEmpty(String str) { return str != null && str.length() != 0; } /** 检查字符串不为 null 、空字符串或只包含空格 */ public final static boolean isTrimStrNotEmpty(String str) { boolean result = isStrNotEmpty(str); return result ? isStrNotEmpty(str.trim()) : result; } /** 检查字符串为 null 或空字符串 */ public final static boolean isStrEmpty(String str) { return !isStrNotEmpty(str); } /** 检查字符串为 null 、空字符串或只包含空格 */ public final static boolean isTrimStrEmpty(String str) { boolean result = isStrEmpty(str); return result ? result : isStrEmpty(str.trim()); } /** 把参数 str 转换为安全字符串:如果 str = null,则把它转换为空字符串 */ public final static String safeString(String str) { if(str == null) str = ""; return str; } /** 把参数 obj 转换为安全字符串:如果 obj = null,则把它转换为空字符串 */ public final static String safeString(Object obj) { if(obj == null) return ""; return obj.toString(); } /** 把参数 str 转换为安全字符串并执行去除前后空格:如果 str = null,则把它转换为空字符串 */ 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)$"); } /** 检查字符串是否符合 IP 地址格式 */ 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])$"); } /** 检查字符串是否符合 HTML 超链接元素格式 */ public final static boolean isStrLink(String str) { return str.matches("<a[^>]*href=\\\"[^\\s\\\"]+\\\"[^>]*>[^<]*<\\/a>"); } /** 检查字符串是否符合 URL 格式 */ 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+_\\-\\.%=&amp;]*)?)?(#[a-z][a-z0-9_]*)?$"); } /** 屏蔽正则表达式的转义字符(但不屏蔽 ignores 参数中包含的字符) */ 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(); } /** 符分割字符串(分割符:" \t\n\r\f,;") */ public final static String[] splitStr(String str) { return splitStr(str, " \t\n\r\f,;"); } /** 符分割字符串(分割符:由 delim 参数指定) */ 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; } /** 调用 {@linkplain Thread#sleep(long)} 方法使当前线程睡眠 period 毫秒 <br> * * 如果 {@linkplain Thread#sleep(long)} 方法被中断则返回 false * */ public final static boolean waitFor(long period) { if(period > 0) { try { Thread.sleep(period); } catch(Exception e) { return false; } } else Thread.yield(); return true; } /** 调用 {@linkplain Thread#sleep(long)} 方法使当前线程睡眠 period 个 unit 时间单元 <br> * * 如果 {@linkplain Thread#sleep(long)} 方法被中断则返回 false * */ public final static boolean waitFor(long period, TimeUnit unit) { return waitFor(unit.toMillis(period)); } /** String -> Integer,如果转换不成功则返回 null */ public final static Integer str2Int(String s) { Integer returnVal; try { returnVal = Integer.decode(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> int,如果转换不成功则返回默认值 d */ public final static int str2Int(String s, int d) { int returnVal; try { returnVal = Integer.parseInt(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> int,如果转换不成功则返回 0 */ public final static int str2Int_0(String s) { return str2Int(s, 0); } /** String -> Short,如果转换不成功则返回 null */ public final static Short str2Short(String s) { Short returnVal; try { returnVal = Short.decode(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> short,如果转换不成功则返回默认值 d */ public final static short str2Short(String s, short d) { short returnVal; try { returnVal = Short.parseShort(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> short,如果转换不成功则返回 0 */ public final static short str2Short_0(String s) { return str2Short(s, (short)0); } /** String -> Long,如果转换不成功则返回 null */ public final static Long str2Long(String s) { Long returnVal; try { returnVal = Long.decode(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> long,如果转换不成功则返回默认值 d */ public final static long str2Long(String s, long d) { long returnVal; try { returnVal = Long.parseLong(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> long,如果转换不成功则返回 0 */ public final static long str2Long_0(String s) { return str2Long(s, 0L); } /** String -> Float,如果转换不成功则返回 null */ public final static Float str2Float(String s) { Float returnVal; try { returnVal = Float.valueOf(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> float,如果转换不成功则返回默认值 d */ public final static float str2Float(String s, float d) { float returnVal; try { returnVal = Float.parseFloat(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> float,如果转换不成功则返回 0 */ public final static float str2Float_0(String s) { return str2Float(s, 0F); } /** String -> Double,如果转换不成功则返回 null */ public final static Double str2Double(String s) { Double returnVal; try { returnVal = Double.valueOf(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> double,如果转换不成功则返回默认值 d */ public final static double str2Double(String s, double d) { double returnVal; try { returnVal = Double.parseDouble(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> double,如果转换不成功则返回 0.0 */ public final static double str2Double_0(String s) { return str2Double(s, 0D); } /** String -> Byte,如果转换不成功则返回 null */ public final static Byte str2Byte(String s) { Byte returnVal; try { returnVal = Byte.decode(safeTrimString(s)); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> byte,如果转换不成功则返回默认值 d */ public final static byte str2Byte(String s, byte d) { byte returnVal; try { returnVal = Byte.parseByte(safeTrimString(s)); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> byte,如果转换不成功则返回 0 */ public final static byte str2Byte_0(String s) { return str2Byte(s, (byte)0); } /** String -> Character,如果转换不成功则返回 null */ public final static Character str2Char(String s) { Character returnVal; try { returnVal = safeTrimString(s).charAt(0); } catch(Exception e) { returnVal = null; } return returnVal; } /** String -> char,如果转换不成功则返回默认值 d */ public final static char str2Char(String s, char d) { char returnVal; try { returnVal = safeTrimString(s).charAt(0); } catch(Exception e) { returnVal = d; } return returnVal; } /** String -> char,如果转换不成功则返回 0 */ public final static char str2Char_0(String s) { return str2Char(s, Character.MIN_VALUE); } /** String -> Boolean,如果转换不成功则返回 null */ public final static Boolean str2Boolean(String s) { return Boolean.valueOf(safeTrimString(s)); } /** String -> boolean,如果转换不成功则返回默认值 d */ 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; } /** String -> boolean,如果转换不成功则返回 0 */ public final static boolean str2Boolean_False(String s) { return str2Boolean(s, false); } /** String -> java.util.Date, str 的格式由 format 定义 */ 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; } /** String -> java.util.Date,由函数自身判断 str 的格式 */ 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; } /** String -> java.util.Date,由 Patterns 指定可能的日期格式 */ 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; } /** String -> java.util.Date,由 GeneralHelper.SHORT_DATE_PATTERN 指定可能的日期格式 */ public final static Date str2ShortDate(String str) { return str2Date(str, SHORT_DATE_PATTERN); } /** String -> java.util.Date,由 GeneralHelper.LONG_DATE_PATTERN 指定可能的日期格式 */ public final static Date str2LongDate(String str) { return str2Date(str, LONG_DATE_PATTERN); } /** String -> java.util.Date,由 GeneralHelper.LONG_DATE_PATTERN_WITH_MILSEC 指定可能的日期格式 */ public final static Date str2LongDateWithMilliSecond(String str) { return str2Date(str, LONG_DATE_PATTERN_WITH_MILSEC); } /** 类型转换处理器接口 */ public static interface TypeHandler<T> { T handle(String v); } /** String -> Any,字符串转换为 8 种基础数据类型、及其包装类 {@link Date}、 或 {@link String} * * @param type : 目标类型的 {@link Class} 对象 * @param v : 要转换的字符串 * @return : 转换结果,如果转换不成功返回 null * @throws : 如果目标类型不支持抛出 {@link IllegalArgumentException} * */ public static final <T> T str2Object(Class<T> type, String v) { return str2Object(type, v, null); } /** String -> Any,如果 handler 为 null 则把字符串转换为 8 种基础数据类型、及其包装类、 {@link Date} 或 {@link String}, * 如果 handler 不为 null 则由 handler 执行转换 * * @param type : 目标类型的 {@link Class} 对象 * @param v : 要转换的字符串 * @param handler : 类型转换处理器 * @return : 转换结果,如果转换不成功返回 null * @throws : 如果目标类型不支持抛出 {@link IllegalArgumentException} * */ @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); else throw new IllegalArgumentException(String.format("object type '%s' not valid", type)); return (T)param; } /** Any -> Object[] <br> * * obj == null : 返回 Object[] {null} <br> * obj 为对象数组 : 强制转换为 Object[], 并返回自身 <br> * obj 为基础类型数组 : 返回 Object[], 其元素类型为基础类型的包装类 <br> * obj 为 {@link Collection} : 通过 toArray() 方法返回 Object[] <br> * obj 为 {@link Iterable} : 遍历 {@link Iterable}, 并返回包含其所有元素的 Object[] <br> * obj 为 {@link Iterator} : 遍历 {@link Iterator}, 并返回包含其所有元素的 Object[] <br> * obj 为 {@link Enumeration} : 遍历 {@link Enumeration}, 并返回包含其所有元素的 Object[] <br> * obj 为普通对象 : 返回 Object[] {obj} <br> * * @param obj : 任何对象 * */ 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); } else array = 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(); } else array = new Object[] {obj}; return array; } /** 返回 date 加上 value 天后的日期(清除时间信息) */ public final static Date addDate(Date date, int value) { return addDate(date, value, true); } /** 返回 date 加上 value 天后的日期,trimTime 指定是否清除时间信息 */ public final static Date addDate(Date date, int value, boolean trimTime) { return addTime(date, Calendar.DATE, value, trimTime); } /** 返回 date 加上 value 个 field 时间单元后的日期(不清除时间信息) */ public final static Date addTime(Date date, int field, int value) { return addTime(date, field, value, false); } /** 返回 date 加上 value 个 field 时间单元后的日期,trimTime 指定是否去除时间信息 */ 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(); } /** java.util.Date -> String,str 的格式由 format 定义 */ public final static String date2Str(Date date, String format) { DateFormat df = new SimpleDateFormat(format); return df.format(date); } /** 修整 SQL 语句字符串:' -> '',(includeWidlcard 指定是否对星号和问号作转换:* -> %, ? -> _) */ public static final String regularSQLStr(String str, boolean includeWidlcard) { str = str.replace("'", "''"); if(includeWidlcard) { str = str.replace('*', '%'); str = str.replace('?', '_'); } return str; } /** 获取 clazz 的 {@link ClassLoader} 对象,如果为 null 则返回当前线程的 Context {@link ClassLoader} */ public static final ClassLoader getClassLoader(Class<?> clazz) { ClassLoader loader = clazz.getClassLoader(); if(loader == null) loader = Thread.currentThread().getContextClassLoader(); return loader; } /** 加载类名为 className 的 {@link Class} 对象,如果加载失败则返回 null */ 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; } /** 用 {@linkplain Class#forName(String)} 加载 {@link Class} 对象,如果加载失败则返回 null */ public static final Class<?> classForName(String name) { Class<?> clazz = null; try { clazz = Class.forName(name); } catch(ClassNotFoundException e) { } return clazz; } /** 用 {@linkplain Class#forName(String, boolean, ClassLoader)} 加载 {@link Class} 对象,如果加载失败则返回 null */ 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; } /** 获取 clazz 资源环境中 resPath 相对路径的 URL 对象 */ 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; } /** 获取 clazz 资源环境中 resPath 相对路径的 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; } /** 获取 clazz 资源环境中 resPath 的 {@link InputStream} */ 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; } /** 获取 clazz 资源环境中 resPath 相对路径的 URL 绝对路径(返还的绝对路径用 UTF-8 编码) */ public static final String getClassResourcePath(Class<?> clazz, String resPath) { return getClassResourcePath(clazz, resPath, DEFAULT_ENCODING); } /** 获取 clazz 资源环境中 resPath 相对路径的 URL 绝对路径(返还的绝对路径用 pathEnc 编码) */ 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; } /** 获取 clazz 资源环境中 resPath 相对路径的 URL 绝对路径列表(返还的绝对路径用 UTF-8 编码) */ public static final List<String> getClassResourcePaths(Class<?> clazz, String resPath) { return getClassResourcePaths(clazz, resPath, DEFAULT_ENCODING); } /** 获取 clazz 资源环境中 resPath 相对路径的 URL 绝对路径列表(返还的绝对路径用 pathEnc 编码) */ 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; } /** 获取 clazz 资源环境的当前 URL 绝对路径(返回的绝对路径用 pathEnc 编码) */ public static final String getClassPath(Class<?> clazz) { return getClassResourcePath(clazz, "."); } /** 获取 resource 资源的 locale 本地化文件中名字为 key 的字符串资源,并代入 params 参数 */ 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; } /** 获取 resource 资源的默认本地化文件中名字为 key 的字符串资源,并代入 params 参数 */ public static final String getResourceMessage(String resource, String key, Object ... params) { return getResourceMessage(Locale.getDefault(), resource, key, params); } /** 获取 e 异常的堆栈列表,最带的堆栈层数由 levels 指定 */ 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; } /** 获取 e 异常的整个堆栈列表 */ public static final List<String> getExceptionMessageStack(Throwable e) { return getExceptionMessageStack(e, 0); } /** 输出 e 异常的 levels 层堆栈列表到 ps 中 */ public static final void printExceptionMessageStack(Throwable e, int levels, PrintStream ps) { List<String> list = getExceptionMessageStack(e, levels); for(String msg : list) ps.println(msg); } /** 输出 e 异常的 levels 层堆栈列表到标准错误流中 */ public static final void printExceptionMessageStack(Throwable e, int levels) { printExceptionMessageStack(e, levels, System.err); } /** 输出 e 异常的整个堆栈列表到 ps 中 */ public static final void printExceptionMessageStack(Throwable e, PrintStream ps) { printExceptionMessageStack(e, 0, ps); } /** 输出 e 异常的整个堆栈列表到标准错误流中 */ public static final void printExceptionMessageStack(Throwable e) { printExceptionMessageStack(e, 0); } /** 把元素添加到 {@link Map} 中,不保证线程安全(不替换原值) */ public static final <K, V> boolean tryPut(Map<K, V> map, K key, V value) { return tryPut(map, key, value, false); } /** 把元素添加到 {@link Map} 中,不保证线程安全 */ 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; } /** 把元素添加到 {@link Map} 中,并保证线程安全(不替换原值) */ public static final <K, V> boolean syncTryPut(Map<K, V> map, K key, V value) { return syncTryPut(map, key, value, false); } /** 把元素添加到 {@link Map} 中,并保证线程安全 */ 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); } } /** 把元素添加到 {@link Map} 中,不保证线程安全(不替换原值) */ public static final <K, V> int tryPutAll(Map<K, V> map, Map<K, V> src) { return tryPutAll(map, src, false); } /** 把元素添加到 {@link Map} 中,不保证线程安全 */ 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; } /** 把元素添加到 {@link Map} 中,并保证线程安全(不替换原值) */ public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src) { return syncTryPutAll(map, src, false); } /** 把元素添加到 {@link Map} 中,并保证线程安全 */ public static final <K, V> int syncTryPutAll(Map<K, V> map, Map<K, V> src, boolean replace) { synchronized(map) { return tryPutAll(map, src, replace); } } /** 从 {@link Map} 中删除元素,不保证线程安全 */ public static final <K, V> boolean tryRemove(Map<K, V> map, K key) { if(map.containsKey(key)) { map.remove(key); return true; } return false; } /** 从 {@link Map} 中删除元素,并保证线程安全 */ public static final <K, V> boolean syncTryRemove(Map<K, V> map, K key) { synchronized(map) { return tryRemove(map, key); } } /** 清空 {@link Map},不保证线程安全 */ public static final <K, V> void tryClear(Map<K, V> map) { map.clear(); } /** 清空 {@link Map},并保证线程安全 */ public static final <K, V> void syncTryClear(Map<K, V> map) { synchronized(map) { tryClear(map); } } /** 获取当前 JVM 进程的 ID */ public static final int getProcessId() { return Integer.parseInt(ManagementFactory.getRuntimeMXBean().getName().split("@")[0]); } /** 获取当前 JVM 进程的 Java 版本 */ public static final String getJavaVersion() { return System.getProperty("java.version"); } /** 获取当前操作系统的名称 */ public static final String getOSName() { return System.getProperty("os.name"); } /** 检查当前操作系统是否为 Windows 系列 */ public static final boolean isWindowsPlatform() { // return CURRENT_OS.toUpperCase().indexOf("WINDOWS") != -1; return File.pathSeparatorChar == ';'; } /** 按拼音排序的字符串比较器 */ public static class PinYinComparator implements Comparator<String> { @Override public int compare(String o1, String o2) { java.text.Collator cmp = java.text.Collator.getInstance(Locale.CHINA); return cmp.compare(o1, o2); } } /** 按文件名称进行文件筛选的文件过滤器,构造函数参数 name 指定文件名的正则表达式 */ 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); } @Override public boolean accept(File file) { Matcher matcher = pattern.matcher(file.getName()); return matcher.matches(); } } /** * * @Description:比较两个时间点 如果secondDate表示的时间等于此 firstDate 表示的时间,则返回 0 值; 如果此 * firstDate 的时间在参数<secondDate>表示的时间之前,则返回小于 0 的值; 如果此 * firstDate 的时间在参数<secondDate>表示的时间之后,则返回大于 0 的值 * @param firstDate * @param secondDate * @ReturnType int * @author: * @Created 2012 2012-9-20上午08:34:33 */ public static int compare(Date firstDate, Date secondDate) { Calendar firstCalendar = null; /** 使用给定的 Date 设置此 Calendar 的时间。 **/ if (firstDate != null) { firstCalendar = Calendar.getInstance(); firstCalendar.setTime(firstDate); } Calendar secondCalendar = null; /** 使用给定的 Date 设置此 Calendar 的时间。 **/ if (firstDate != null) { secondCalendar = Calendar.getInstance(); secondCalendar.setTime(secondDate); } try { /** * 比较两个 Calendar 对象表示的时间值(从历元至现在的毫秒偏移量)。 如果参数表示的时间等于此 Calendar * 表示的时间,则返回 0 值; 如果此 Calendar 的时间在参数表示的时间之前,则返回小于 0 的值; 如果此 * Calendar 的时间在参数表示的时间之后,则返回大于 0 的值 * **/ return firstCalendar.compareTo(secondCalendar); } catch (NullPointerException e) { throw new IllegalArgumentException(e); } catch (IllegalArgumentException e) { throw new IllegalArgumentException(e); } } /** * List<Object>去重 * Object 必须实现equals和hashCode方法 * @param list */ 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); } /** * 将map转换成List * @param map * @return */ 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(); //将map对象里面的属性循环遍历出来 while(it.hasNext()){ Entry<String, Object> entry = it.next(); //得到value值,装到list里面,也可以entry.getKey()。 //如果2个都需要装。可以弄成一个对象来装 list.add(entry.getValue()); } return list; } /** * 将Date转换成String【yyyy-MM-dd】 * @param date * @return */ public static String dateConvertString(Date date){ SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd"); String str=sdf.format(date); return str; } /** * 根据指定长度 分隔字符串 * * @param str * 需要处理的字符串 * @param length * 分隔长度 * * @return 字符串集合 */ 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; } /** * 将字符串List转化为字符串,以分隔符间隔. * * @param list * 需要处理的List. * * @param separator * 分隔符. * * @return 转化后的字符串 */ 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(); } /** * Description: 随机从数组中取出指定的不重复的n个数。 * @param ArrayList 原始数组 * @param int n 随机抽取的个数 * @return 返回抽取的数组 */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static List getRandomArray(List list) { Collections.shuffle(list); //洗牌 return list; } /** * 截取指定的长度 * @param list * @param num * @return */ public static List subList(List list ,int num) { List newList = getRandomArray(list); return newList.subList(0, num); } }
    Processed: 0.020, SQL: 8