公用代碼工做雜記

+++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系統html

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) {
        // 一、(13[0-9])|(15[02789])|(18[679])|(17[0-9]) 13段 或者15段 18段17段的匹配
        // 二、\\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;
    }java


    /**
     * 驗證是否爲正確的郵箱號
     *
     * @param email
     * @return
     */
    public static boolean isValidEmail(String email) {
        // 一、\\w+表示@以前至少要輸入一個匹配字母或數字或下劃線 \\w 單詞字符:[a-zA-Z_0-9]
        // 二、(\\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;
    }linux

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

import java.security.MessageDigest;json

public class MD5Util {windows

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

        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];
    }session

    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;
    }app

    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();    } }

相關文章
相關標籤/搜索