公用代码工作杂记

+++JSON获取代码+++++++++++++++++++
获取json数据是list还是单列
List<SingleChoiceVo> singleChoiceList = new ArrayList<SingleChoiceVo>();
if(null!=job.get("singleChoice")){
       Object listArray_singleChoice = new JSONTokener(job.get("singleChoice").toString()).nextValue();  
   if (listArray_singleChoice instanceof JSONArray){  
       JSONArray jsonArray = (JSONArray)listArray_singleChoice;  
       for (int k = 0; k < jsonArray.size(); k++) {  
       JSONObject parameterObject = jsonArray.getJSONObject(k);  
       SingleChoiceVo comvo= JSON.parseObject(parameterObject.toString(),SingleChoiceVo.class);
       singleChoiceList.add(comvo);
       }  
   }else if (listArray_singleChoice instanceof JSONObject) {  
       JSONObject jsonObject3 = (JSONObject)listArray_singleChoice;  
       singleChoiceList= JSON.parseArray(jsonObject3.toString(),SingleChoiceVo.class);
   } 
}
answer.setSingleChoice(singleChoiceList); 
+++系统判断代码+++++++++++++++++++
判断windows系统还是 Linux系统

Properties prop = System.getProperties();
String os = prop.getProperty("os.name");
if (os != null && os.toLowerCase().indexOf("linux") > -1) {
folder= new File("//usr//local//");
} else {
folder = new File("D:\\images");
}
+++验证代码+++++++++++++++++++
    /**
     * 验证是否为手机号
     *
     * @param mobileNo
     * @return
     */
    public static boolean isValidMobileNo(String mobileNo) {
        // 1、(13[0-9])|(15[02789])|(18[679])|(17[0-9]) 13段 或者15段 18段17段的匹配
        // 2、\\d{8} 整数出现8次
        boolean flag = false;
        Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9])|(17[0-9]))\\d{8}$");
        Matcher match = p.matcher(mobileNo);
        if (mobileNo != null) {
            flag = match.matches();
        }
        return flag;
    }


    /**
     * 验证是否为正确的邮箱号
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        // 1、\\w+表示@之前至少要输入一个匹配字母或数字或下划线 \\w 单词字符:[a-zA-Z_0-9]
        // 2、(\\w+\\.)表示域名. 如新浪邮箱域名是sina.com.cn
        // {1,3}表示可以出现一次或两次或者三次.
        String reg = "\\w+@(\\w+\\.){1,3}\\w+";
        Pattern pattern = Pattern.compile(reg);
        boolean flag = false;
        if (email != null) {
            Matcher matcher = pattern.matcher(email);
            flag = matcher.matches();
        }
        return flag;
    }

+++MD5加密代码+++++++++++++++++++
package com.cn.riwise.m.api.util;

import java.security.MessageDigest;

public class MD5Util {

    private static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++)
            resultSb.append(byteToHexString(b[i]));

        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0)
            n += 256;
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charsetname == null || "".equals(charsetname))
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes()));
            else
                resultString = byteArrayToHexString(md.digest(resultString
                        .getBytes(charsetname)));
        } catch (Exception exception) {
        }
        return resultString;
    }

    private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

    public static void main(String[] args) {
        System.out.println(MD5Util.MD5Encode("1", null));
    }
}


++++++++++邮件发送代码++++++++

public static void main(String [] args) throws GeneralSecurityException
{
   // 收件人电子邮箱
   String to = "xxxxxxx@126.com";
   // 发件人电子邮箱
   String from = "xxxxx@qq.com";
   // 指定发送邮件的主机为 smtp.qq.com
   String host = "smtp.qq.com";  //QQ 邮件服务器
   // 获取系统属性
   Properties properties = System.getProperties();
   // 设置邮件服务器
   properties.setProperty("mail.smtp.host", host);
   properties.put("mail.smtp.auth", "true");
   MailSSLSocketFactory sf = new MailSSLSocketFactory();
   sf.setTrustAllHosts(true);
   properties.put("mail.smtp.ssl.enable", "true");
   properties.put("mail.smtp.ssl.socketFactory", sf);
   // 获取默认session对象
   Session session = Session.getDefaultInstance(properties,new Authenticator(){
     public PasswordAuthentication getPasswordAuthentication()
     {
      return new PasswordAuthentication("xxxxxx@qq.com", "azmdzupofjrjbdbc"); //发件人邮件用户名、密码
     }
    });
   try{
      // 创建默认的 MimeMessage 对象
      MimeMessage message = new MimeMessage(session);
      // Set From: 头部头字段
      message.setFrom(new InternetAddress(from));
      // Set To: 头部头字段
      message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
      // Set Subject: 头部头字段
      message.setSubject("This is the Subject Line!");
      // 设置消息体
      message.setContent("<h1>This is actual message</h1>",
              "text/html" );
      // 发送消息
      Transport.send(message);
      System.out.println("Sent message successfully....from runoob.com");
   }catch (MessagingException mex) {
      mex.printStackTrace();
   }
}

转载于:https://my.oschina.net/u/3204029/blog/2989123

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值