<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 7,  comments - 6,  trackbacks - 0

    import java.security.Key;
    import java.security.SecureRandom;
    import javax.crypto.Cipher;
    import javax.crypto.KeyGenerator;
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;

    /**
     *
     * 使用DES加密與解密,可對byte[],String類型進行加密與解密
     * 密文可使用String,byte[]存儲.
     *
     * 方法:
     * void getKey(String strKey)從strKey的字條生成一個Key
     *
     * String getEncString(String strMing)對strMing進行加密,返回String密文
     * String getDesString(String strMi)對strMin進行解密,返回String明文
     *
     *byte[] getEncCode(byte[] byteS)byte[]型的加密
     *byte[] getDesCode(byte[] byteD)byte[]型的解密
     */

    public class DesEncrypt {
      Key key;

      /**
       * 根據(jù)參數(shù)生成KEY
       * @param strKey
       */
       public void getKey(String strKey) {
        try {
          KeyGenerator _generator = KeyGenerator.getInstance("DES");
          _generator.init(new SecureRandom(strKey.getBytes()));
          this.key = _generator.generateKey();
          _generator = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      /**
       * 加密String明文輸入,String密文輸出
       * @param strMing
       * @return
       */
      public String getEncString(String strMing) {
        byte[] byteMi = null;
        byte[] byteMing = null;
        String strMi = "";
        BASE64Encoder base64en = new BASE64Encoder();
        try {
          byteMing = strMing.getBytes("UTF8");
          byteMi = this.getEncCode(byteMing);
          strMi = base64en.encode(byteMi);
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          base64en = null;
          byteMing = null;
          byteMi = null;
        }
        return strMi;
      }

      /**
       * 解密 以String密文輸入,String明文輸出
       * @param strMi
       * @return
       */
      public String getDesString(String strMi) {
        BASE64Decoder base64De = new BASE64Decoder();
        byte[] byteMing = null;
        byte[] byteMi = null;
        String strMing = "";
        try {
          byteMi = base64De.decodeBuffer(strMi);
          byteMing = this.getDesCode(byteMi);
          strMing = new String(byteMing, "UTF8");
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          base64De = null;
          byteMing = null;
          byteMi = null;
        }
        return strMing;
      }

      /**
       * 加密以byte[]明文輸入,byte[]密文輸出
       * @param byteS
       * @return
       */
      private byte[] getEncCode(byte[] byteS) {
        byte[] byteFina = null;
        Cipher cipher;
        try {
          cipher = Cipher.getInstance("DES");
          cipher.init(Cipher.ENCRYPT_MODE, key);
          byteFina = cipher.doFinal(byteS);
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          cipher = null;
        }
        return byteFina;
      }

      /**
       * 解密以byte[]密文輸入,以byte[]明文輸出
       * @param byteD
       * @return
       */
      private byte[] getDesCode(byte[] byteD) {
        Cipher cipher;
        byte[] byteFina = null;
        try {
          cipher = Cipher.getInstance("DES");
          cipher.init(Cipher.DECRYPT_MODE, key);
          byteFina = cipher.doFinal(byteD);
        } catch (Exception e) {
          e.printStackTrace();
        } finally {
          cipher = null;
        }
        return byteFina;
      }

      public static void main(String args[]) {
        DesEncrypt des=new DesEncrypt();//實例化一個對像
        des.getKey("aadd");//生成密匙

        String strEnc = des.getEncString("明文");//加密字符串,返回String的密文
        System.out.println("加密文:"+strEnc);

        String strDes = des.getDesString(strEnc);//把String 類型的密文解密
        System.out.println("解密文:"+strDes);
      }
    }

    posted @ 2005-11-28 15:08 清咖 閱讀(981) | 評論 (1)編輯 收藏
    Chapter 21. Sending Email with Spring mail abstraction layer
    Prev     Next

    Chapter 21. Sending Email with Spring mail abstraction layer

    21.1. Introduction

    Spring provides a higher level of abstraction for sending electronic mail which shields the user from the specifics of underlying mailing system and is responsible for a low level resource handling on behalf of the client.

    21.2. Spring mail abstraction structure

    The main package of Spring mail abstraction layer is org.springframework.mail package. It contains central interface for sending emails called MailSender and the value object which encapsulates properties of a simple mail such as from, to, cc, subject, text called SimpleMailMessage. This package also contains a hierarchy of checked exceptions which provide a higher level of abstraction over the lower level mail system exceptions with the root exception being MailException.Please refer to JavaDocs for more information on mail exception hierarchy.

    Spring also provides a sub-interface of MailSender for specialized JavaMail features such as MIME messages, namely org.springframework.mail.javamail.JavaMailSender It also provides a callback interface for preparation of JavaMail MIME messages, namely org.springframework.mail.javamail.MimeMessagePreparator

    MailSender:

    public interface MailSender {
    
        /**
         * Send the given simple mail message.
         * @param simpleMessage message to send
         * @throws MailException in case of message, authentication, or send errors
         */
        public void send(SimpleMailMessage simpleMessage) throws MailException;
    
        /**
         * Send the given array of simple mail messages in batch.
         * @param simpleMessages messages to send
         * @throws MailException in case of message, authentication, or send errors
         */
        public void send(SimpleMailMessage[] simpleMessages) throws MailException;
    
    }

    JavaMailSender:

    public interface JavaMailSender extends MailSender {
    
        /**
         * Create a new JavaMail MimeMessage for the underlying JavaMail Session
         * of this sender. Needs to be called to create MimeMessage instances
         * that can be prepared by the client and passed to send(MimeMessage).
         * @return the new MimeMessage instance
         * @see #send(MimeMessage)
         * @see #send(MimeMessage[])
         */
        public MimeMessage createMimeMessage();
    
        /**
         * Send the given JavaMail MIME message.
         * The message needs to have been created with createMimeMessage.
         * @param mimeMessage message to send
         * @throws MailException in case of message, authentication, or send errors
         * @see #createMimeMessage
         */
        public void send(MimeMessage mimeMessage) throws MailException;
    
        /**
         * Send the given array of JavaMail MIME messages in batch.
         * The messages need to have been created with createMimeMessage.
         * @param mimeMessages messages to send
         * @throws MailException in case of message, authentication, or send errors
         * @see #createMimeMessage
         */
        public void send(MimeMessage[] mimeMessages) throws MailException;
    
        /**
         * Send the JavaMail MIME message prepared by the given MimeMessagePreparator.
         * Alternative way to prepare MimeMessage instances, instead of createMimeMessage
         * and send(MimeMessage) calls. Takes care of proper exception conversion.
         * @param mimeMessagePreparator the preparator to use
         * @throws MailException in case of message, authentication, or send errors
         */
        public void send(MimeMessagePreparator mimeMessagePreparator) throws MailException;
    
        /**
         * Send the JavaMail MIME messages prepared by the given MimeMessagePreparators.
         * Alternative way to prepare MimeMessage instances, instead of createMimeMessage
         * and send(MimeMessage[]) calls. Takes care of proper exception conversion.
         * @param mimeMessagePreparators the preparator to use
         * @throws MailException in case of message, authentication, or send errors
         */
        public void send(MimeMessagePreparator[] mimeMessagePreparators) throws MailException;
    
    }

    MimeMessagePreparator:

    public interface MimeMessagePreparator {
    
        /**
         * Prepare the given new MimeMessage instance.
         * @param mimeMessage the message to prepare
         * @throws MessagingException passing any exceptions thrown by MimeMessage
         * methods through for automatic conversion to the MailException hierarchy
         */
        void prepare(MimeMessage mimeMessage) throws MessagingException;
    
    }

    21.3. Using Spring mail abstraction

    Let's assume there is a business interface called OrderManager

    public interface OrderManager {
    
        void placeOrder(Order order);
    }

    and there is a use case that says that an email message with order number would need to be generated and sent to a customer placing that order. So for this purpose we want to use MailSender and SimpleMailMessage

    Please note that as usual, we work with interfaces in the business code and let Spring IoC container take care of wiring of all the collaborators for us.

    Here is the implementation of OrderManager

    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    
    public class OrderManagerImpl implements OrderManager {
    
        private MailSender mailSender;
        private SimpleMailMessage message;
    
        public void setMailSender(MailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void setMessage(SimpleMailMessage message) {
            this.message = message;
        }
    
        public void placeOrder(Order order) {
    
            //... * Do the business calculations....
            //... * Call the collaborators to persist the order
    
            //Create a thread safe "sandbox" of the message
            SimpleMailMessage msg = new SimpleMailMessage(this.message);
            msg.setTo(order.getCustomer().getEmailAddress());
            msg.setText(
                "Dear "
                    + order.getCustomer().getFirstName()
                    + order.getCustomer().getLastName()
                    + ", thank you for placing order. Your order number is "
                    + order.getOrderNumber());
            try{
                mailSender.send(msg);
            }
            catch(MailException ex) {
                //log it and go on
                System.err.println(ex.getMessage());            
            }
        }
    }

    Here is what the bean definitions for the code above would look like:

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host"><value>mail.mycompany.com</value></property>
    </bean>
    
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
      <property name="from"><value>customerservice@mycompany.com</value></property>
      <property name="subject"><value>Your order</value></property>
    </bean>
    
    <bean id="orderManager" class="com.mycompany.businessapp.support.OrderManagerImpl">
      <property name="mailSender"><ref bean="mailSender"/></property>
      <property name="message"><ref bean="mailMessage"/></property>
    </bean>

    Here is the implementation of OrderManager using MimeMessagePreparator callback interface. Please note that the mailSender property is of type JavaMailSender in this case in order to be able to use JavaMail MimeMessage:

    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import javax.mail.internet.MimeMessage;
    import org.springframework.mail.MailException;
    import org.springframework.mail.javamail.JavaMailSender;
    import org.springframework.mail.javamail.MimeMessagePreparator;
    
    public class OrderManagerImpl implements OrderManager {
    
        private JavaMailSender mailSender;
        
        public void setMailSender(JavaMailSender mailSender) {
            this.mailSender = mailSender;
        }
    
        public void placeOrder(final Order order) {
    
            //... * Do the business calculations....
            //... * Call the collaborators to persist the order
            
            MimeMessagePreparator preparator = new MimeMessagePreparator() {
                public void prepare(MimeMessage mimeMessage) throws MessagingException {
                    mimeMessage.setRecipient(Message.RecipientType.TO, 
                            new InternetAddress(order.getCustomer().getEmailAddress()));
                    mimeMessage.setFrom(new InternetAddress("mail@mycompany.com"));
                    mimeMessage.setText(
                        "Dear "
                            + order.getCustomer().getFirstName()
                            + order.getCustomer().getLastName()
                            + ", thank you for placing order. Your order number is "
                            + order.getOrderNumber());
                }
            };
            try{
                mailSender.send(preparator);
            }
            catch (MailException ex) {
                //log it and go on
                System.err.println(ex.getMessage());            
            }
        }
    }

    If you want to use JavaMail MimeMessage to the full power, the MimeMessagePreparator is available at your fingertips.

    Please note that the mail code is a crosscutting concern and is a perfect candidate for refactoring into a custom Spring AOP advice, which then could easily be applied to OrderManager target. Please see the AOP chapter.

    21.3.1. Pluggable MailSender implementations

    Spring comes with two MailSender implementations out of the box - the JavaMail implementation and the implementation on top of Jason Hunter's MailMessage class that's included in http://servlets.com/cos (com.oreilly.servlet). Please refer to JavaDocs for more information.

    21.4. Using the JavaMail MimeMessageHelper

    One of the components that comes in pretty handy when dealing with JavaMail messages is the org.springframework.mail.javamail.MimeMessageHelper. It prevents you from having to use the nasty APIs the the javax.mail.internet classes. A couple of possible scenarios:

    21.4.1. Creating a simple MimeMessage and sending it

    Using the MimeMessageHelper it's pretty easy to setup and send a MimeMessage:

    // of course you would setup the mail sender using 
    // DI in any real-world cases
    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost("mail.host.com");
    
    MimeMessage message = sender.createMimeMesage();
    MimeMessageHelper helper = new MimeMessageHelper(message);
    helper.setTo("test@host.com");
    helper.setText("Thank you for ordering!");
    
    sender.send(message);
    			

    21.4.2. Sending attachments and inline resources

    Email allow for attachments, but also for inline resources in multipart messages. Inline resources could for example be images or stylesheet you want to use in your message, but don't want displayed as attachment. The following shows you how to use the MimeMessageHelper to send an email along with an inline image.

    JavaMailSenderImpl sender = new JavaMailSenderImpl();
    sender.setHost("mail.host.com");
    
    MimeMessage message = sender.createMimeMesage();
    
    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    helper.setTo("test@host.com");
    
    // use the true flag to indicate the text included is HTML
    helper.setText(
      "<html><body><img src='cid:identifier1234'></body></html>"
      true);
    
    // let's include the infamous windows Sample file (this time copied to c:/)
    FileSystemResource res = new FileSystemResource(new File("c:/Sample.jpg"));
    helper.addInline("identifier1234", res);
    
    // if you would need to include the file as an attachment, use
    // addAttachment() methods on the MimeMessageHelper
    
    sender.send(message);
    			

    Inline resources are added to the mime message using the Content-ID specified as you've seen just now (identifier1234 in this case). The order in which you're adding the text and the resource are VERY important. First add the text and after that the resources. If you're doing it the other way around, it won't work!

    posted @ 2005-11-24 10:37 清咖 閱讀(876) | 評論 (0)編輯 收藏
    這個頁面怎么老師刷啊刷的。。。。。。。可憐一下偶的眼睛
    posted @ 2005-11-18 10:22 清咖 閱讀(203) | 評論 (0)編輯 收藏

    1。spring日期類型要寫自己的方法覆蓋原有的綁定類型
    例如生日域的綁定
     protected void initBinder(HttpServletRequest arg0, ServletRequestDataBinder arg1) throws Exception {
      arg1.registerCustomEditor(Date.class,"birthday",getCustomDateEditor());
      super.initBinder(arg0, arg1);
     }

    2.trim
    The string without leading and trailing white-space. Example: (${"  green mouse  "?trim})  
    The output: (green mouse)  
      3.split
      
     <#list "someMOOtestMOOtext"?split("MOO") as x>
    - ${x}
    </#list>   
    willprint :
    - some
    - test
    - text
     

    posted @ 2005-11-18 10:21 清咖 閱讀(764) | 評論 (0)編輯 收藏
    人因夢想而偉大,因精神而不休。

    注,這頁面怎么老自動刷新,,,,
    posted @ 2005-11-15 12:12 清咖 閱讀(309) | 評論 (1)編輯 收藏
    不過把用戶限制在使用java的用戶,是不是會影響人氣。
    posted @ 2005-11-13 15:25 清咖 閱讀(236) | 評論 (1)編輯 收藏
    但是頁面好難看
    posted @ 2005-11-11 14:55 清咖 閱讀(272) | 評論 (3)編輯 收藏
    <2005年11月>
    303112345
    6789101112
    13141516171819
    20212223242526
    27282930123
    45678910

    常用鏈接

    留言簿(1)

    隨筆檔案

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免免费国产AAAAA片| 桃子视频在线观看高清免费视频| 日本激情猛烈在线看免费观看 | 亚洲午夜久久影院| 亚洲国产精品久久网午夜| 在线观看亚洲AV日韩A∨| 免费国产在线精品一区| a级毛片视频免费观看| 日韩精品免费一级视频| 免费不卡中文字幕在线| 亚洲成在人线av| youjizz亚洲| 一级毛片高清免费播放| 久久99国产乱子伦精品免费| 免费的一级黄色片| 亚洲精品~无码抽插| 在线a亚洲老鸭窝天堂av高清| 精品免费久久久久国产一区 | 免费看国产一级特黄aa大片| 亚洲美女又黄又爽在线观看| 亚洲日本国产综合高清| jyzzjyzz国产免费观看| 成人免费午夜无码视频| 久久激情亚洲精品无码?V| 亚洲人成在久久综合网站| 一级一黄在线观看视频免费| 日韩精品免费一级视频| 久久影视国产亚洲| 亚洲免费二区三区| 91成人免费观看在线观看| 最新猫咪www免费人成| 亚洲午夜无码久久久久| 中中文字幕亚洲无线码| 三根一起会坏掉的好痛免费三级全黄的视频在线观看 | 两个人看的www免费高清| 毛片免费vip会员在线看| 亚洲国产另类久久久精品小说| 亚洲狠狠成人综合网| 好久久免费视频高清| 国产大片91精品免费观看男同| 亚洲一二成人精品区|