lqxue

          導航

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          留言簿(5)

          隨筆分類

          文章分類

          收藏夾

          隨筆檔案

          文章檔案

          相冊

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          book

          tools

          最新評論

          #

          [收藏]java實現URL帶參數請求(get/post)

          http://www.80x86.cn/article.asp?id=48

          posted @ 2007-05-30 14:27 lqx 閱讀(224) | 評論 (0)編輯 收藏

          [收藏] 使用javamail發信過程中的一些問題及解決方法

            1 今天在研究javamail發信的過程中,出現了一些小問題,現總結如下,以免后來者走些不必要的彎路,先把完整的能夠正常運行的代碼示例粘貼如下:
            2 發郵件源代碼:
            3 package com.hyq.test;
            4 
            5 import java.util.Properties;
            6 import javax.mail.*;
            7 import javax.mail.internet.*;
            8 
            9 public class MailExample {
           10 
           11   public static void main (String args[]) throws Exception {
           12     
           13     String host = "smtp.163.com";   //發件人使用發郵件的電子信箱服務器
           14     String from = "你自己的電子信箱";    //發郵件的出發地(發件人的信箱)
           15     String to = "收件人信箱";   //發郵件的目的地(收件人信箱)
           16 
           17     // Get system properties
           18     Properties props = System.getProperties();
           19 
           20     // Setup mail server
           21     props.put("mail.smtp.host", host);
           22 
           23     // Get session
           24     props.put("mail.smtp.auth""true"); //這樣才能通過驗證
           25 
           26     MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱""你自己的信箱密碼");
           27     Session session = Session.getDefaultInstance(props, myauth);
           28 
           29 //    session.setDebug(true);
           30 
           31     // Define message
           32     MimeMessage message = new MimeMessage(session);
           33     
           34 
           35     // Set the from address
           36     message.setFrom(new InternetAddress(from));
           37 
           38     // Set the to address
           39     message.addRecipient(Message.RecipientType.TO,
           40       new InternetAddress(to));
           41 
           42     // Set the subject
           43     message.setSubject("測試程序!");
           44 
           45     // Set the content
           46     message.setText("這是用java寫的發送電子郵件的測試程序!");
           47 
           48     message.saveChanges();
           49 
           50       Transport.send(message);
           51     
           52   }
           53 }
           54 
           55 校驗發信人權限的方法
           56 package com.hyq.test;
           57 
           58 import javax.mail.PasswordAuthentication;
           59 
           60 class MyAuthenticator
           61       extends javax.mail.Authenticator {
           62     private String strUser;
           63     private String strPwd;
           64     public MyAuthenticator(String user, String password) {
           65       this.strUser = user;
           66       this.strPwd = password;
           67     }
           68 
           69     protected PasswordAuthentication getPasswordAuthentication() {
           70       return new PasswordAuthentication(strUser, strPwd);
           71     }
           72   }
           73 
           74 
           75 注意:上面的事例僅為使用163信箱時發送電子郵件的方法,因為使用的host為:smtp.163.com,如源代碼中:String host =  "smtp.163.com";   //發件人使用發郵件的電子信箱服務器,如果使用其它的電子郵件發送,就必須在其郵件服務器上查找相應的電子郵件服務器,例如搜狐就要使用smtp.sohu.com,具體情況具體對待,都可以從所使用的郵件服務器上獲得的。如果沒有使用host ,也就是說,沒有進行props.put("mail.smtp.host", host);設置,那么就會拋 javax.mail.MessagingException: Could not connect to SMTP host: localhost,  port: 25;的異常。當然了,如果你沒有正確配置,這個異常仍然會被拋出的。
           76 
           77 有些郵件服務系統是不需要驗證發件人的授權的,所以可以很簡單的使用
           78     Session session = Session.getDefaultInstance(props, null);
           79              而不必使用
           80     props.put("mail.smtp.auth""true"); 
           81     MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱""你自己的信箱密碼");
           82     Session session = Session.getDefaultInstance(props, myauth);
           83 
           84 就可以發送電子郵件了,這個多為一些企事業單位的內部電子信箱系統。
           85 但是對于很多門戶網站上的電郵系統,如:163,sohu,yahoo等等,如果仍然簡單的這樣使用就會拋
           86 
           87 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxuAyCAfmnZE8BwtIA==.32705S2
           88 
           89 
           90  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
           91 
           92  at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
           93 
           94  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
           95 
           96  at javax.mail.Transport.send0(Transport.java:169)
           97 
           98  at javax.mail.Transport.send(Transport.java:98)
           99 
          100 這樣的異常,要求你必須進行授權校驗,它的目的就是阻止他人任意亂發郵件,也算是為了減少垃圾郵件的出現吧。這時候,我們就要使用
          101     props.put("mail.smtp.auth""true"); 
          102     MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱""你自己的信箱密碼");
          103     Session session = Session.getDefaultInstance(props, myauth);
          104 
          105 這里還有一個特別注意的事情:在你使用Session.getDefaultInstance時,一定要將    props.put ("mail.smtp.auth""true"); 置為true,它默認的是false,如果你沒有做這一步,雖然你使用了 Session.getDefaultInstance(props, myauth);,你自己也確實     MyAuthenticator myauth = new MyAuthenticator("你自己的電子信箱""你自己的信箱密碼 ");但是它仍然會拋出
          106 com.sun.mail.smtp.SMTPSendFailedException: 553 authentication is required,smtp8,wKjADxJA2SBrm3ZEFv0gIA==.40815S2
          107 
          108 
          109  at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1388)
          110 
          111  at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:959)
          112 
          113  at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:583)
          114 
          115  at javax.mail.Transport.send0(Transport.java:169)
          116 
          117  at javax.mail.Transport.send(Transport.java:98)
          118 這樣的異常。我就在這一步費了好長時間,后來才發現了這個問題,很是郁悶。不過還好,總算解決了。
          119 
          120 其實上面的做法只是比較簡單的一種,也有很多其它的寫法,如:
          121 Properties props = System.getProperties();可以使用
          122 Properties props = new Properties();來代替。
          123 
          124 
          125 Transport.send(message);可以使用下面的代碼來代替
          126       String username = "你的電子信箱用戶名";
          127       String password = "你的電子信箱密碼";
          128       message.saveChanges(); //    implicit with send()
          129       Transport transport = session.getTransport("smtp");
          130       transport.connect("mail.htf.com.cn", username, password);
          131       transport.sendMessage(message, message.getAllRecipients());
          132       transport.close();
          133 這種方法在同時發送多封電子郵件時比較有用。
          134 
          135 還有一些具體的相關概念,可以查看相關的官方文檔,在我查詢資料時,發現了一篇文章寫得相當仔細,可以加以參考:http://www.matrix.org.cn/resource/article/44/44101_JavaMail.html
          136 
          137 另附上使用org.apache.commons.mail進行發電子郵件的示例:
          138 import org.apache.commons.mail.SimpleEmail;
          139 import org.apache.commons.mail.*;
          140 
          141 public class TestCommon {
          142   public TestCommon() {
          143   }
          144   public static void main(String[] args){
          145     SimpleEmail email = new SimpleEmail();
          146     email.setHostName("smtp.163.com");//設置使用發電子郵件的郵件服務器
          147     try {
          148       email.addTo("收件人信箱");
          149       email.setAuthentication("發件人信箱","發件人信箱密碼");
          150       email.setFrom("發件人信箱");
          151       email.setSubject("Test apache.commons.mail message");
          152       email.setMsg("This is a simple test of commons-email");
          153       email.send();
          154     }
          155     catch (EmailException ex) {
          156       ex.printStackTrace();
          157     }
          158   }
          159 
          注:文章來自http://garyea.javaeye.com/blog/76460

          posted @ 2007-05-30 11:45 lqx 閱讀(223) | 評論 (0)編輯 收藏

          java 編碼研究感想

          1、iso8859-1
          屬于單字節編碼,最多能表示的字符范圍是0-255,應用于英文系列。比如,字母'a'的編碼為0x61=97。
          很明顯,iso8859-1編碼表示的字符范圍很窄,無法表示中文字符。但是,由于是單字節編碼,和計算機最基礎的表示單位一致,所以很多時候,仍舊使用 iso8859-1編碼來表示。而且在很多協議上,默認使用該編碼。比如,雖然"中文"兩個字不存在iso8859-1編碼,以gb2312編碼為例,應 該是"[u]d6d0 cec4[/u]"兩個字符,使用iso8859-1編碼的時候則將它拆開為4個字節來表示:"[u]d6 d0 ce c4[/u]"(事實上,在進行存儲的時候,也是以字節為單位處理的)。而如果是UTF編碼,則是6個字節"[u]e4 b8 ad e6 96 87[/u]"。很明顯,這種表示方法還需要以另一種編碼為基礎。
          2、 GB2312/GBK
          這就是漢子的國標碼,專門用來表示漢字,是雙字節編碼,而英文字母和iso8859-1一致(兼容iso8859-1編碼)。其中gbk編碼能夠用來同時表示繁體字和簡體字,而gb2312只能表示簡體字,gbk是兼容gb2312編碼的。
          3、 unicode
          這是最統一的編碼,可以用來表示所有語言的字符,而且是定長雙字節(也有四字節的)編碼,包括英文字母在內。所以可以說它是不兼容iso8859-1編碼 的,也不兼容任何編碼。不過,相對于iso8859-1編碼來說,uniocode編碼只是在前面增加了一個0字節,比如字母'a'為"[u]00 61[/u]"。
          需要說明的是,定長編碼便于計算機處理(注意GB2312/GBK不是定長編碼),而unicode又可以用來表示所有字符,所以在很多軟件內部是使用unicode編碼來處理的,比如java。
          4、UTF
          考慮到unicode編碼不兼容iso8859-1編碼,而且容易占用更多的空間:因為對于英文字母,unicode也需要兩個字節來表示。所以 unicode不便于傳輸和存儲。因此而產生了utf編碼,utf編碼兼容iso8859-1編碼,同時也可以用來表示所有語言的字符,不過,utf編碼 是不定長編碼,每一個字符的長度從1-6個字節不等。另外,utf編碼自帶簡單的校驗功能。一般來講,英文字母都是用一個字節表示,而漢字使用三個字節。
          注意,雖然說utf是為了使用更少的空間而使用的,但那只是相對于unicode編碼來說,如果已經知道是漢字,則使用GB2312/GBK無疑是最節省 的。不過另一方面,值得說明的是,雖然utf編碼對漢字使用3個字節,但即使對于漢字網頁,utf編碼也會比unicode編碼節省,因為網頁中包含了很 多的英文字符。
          5、如果我們以一種能表示中文的編碼格式(例如GBK、unicode)來保存中文到文件中,那么當我們用properties load時,只要load時的編碼格式(默認8859-1)和你保存的文件的編碼格式相同,那么就不會出現亂碼。
          6、之所以\u4F60這種形式支持國際化,是因為這種形式的內容無論那種編碼都是支持的,當我們用properties.getProperty()時,這個方法會對key和value都進行轉化一次,當其碰見這種碼時,它就把他轉化為unicode碼后返回。 所以,我們可以利用工具(例如:native2ascii )把.properties文件轉化成這種格式以方便我們支持國際化。

          參考鏈接:
              1、http://www.aygfsteel.com/beike/archive/2006/04/29/44038.html
              2、http://tech.ccidnet.com/art/1077/20050704/279619_1.html
              3、http://linux.chinaunix.net/bbs/archiver/tid-896583.html

          posted @ 2007-05-29 13:37 lqx 閱讀(191) | 評論 (0)編輯 收藏

          [收藏]JAVA里字符編碼的探索與理解

          http://www.aygfsteel.com/beike/archive/2006/04/29/44038.html

          posted @ 2007-05-28 17:16 lqx 閱讀(155) | 評論 (0)編輯 收藏

          如何在eclipse 中隱藏 .svn 包

          1、delete the project from eclipse.
          2. create the porject again and save the project file to the same directory as the prior one.

          posted @ 2007-05-26 13:12 lqx 閱讀(357) | 評論 (0)編輯 收藏

          清除文件中含有指定特征字符串(例如puma166)的行。


          最近感染了一個病毒,也說不上是啥毒,反正所有的.html,.htm,.jsp文件一經被IE執行,它都會在文件的尾部加上一句類似這樣一句代碼“< IFRAME ID=IFrame1 FRAMEBORDER=0 SCROLLING=NO SRC="http://www.puma166.com/...."></ IFRAME > ”;殺毒軟件是不能幫我把它從文件中刪除了,自己寫了一段程序來實現。

          import java.io.BufferedReader;
          import java.io.File;
          import java.io.FileReader;
          import java.io.FileWriter;

          public class Test {
              
          private static String feature = "puma166";// the special string which indicate that we will remove this line 
              private static String filetypes = "#.htm#.html#.jsp#.asp#.aspx#";
              
          public static void moveAntivus(String filename,String feature) throws Exception{
                  FileReader fr 
          = new FileReader(filename);
                  String tmpFileName 
          = filename+"_tmp";
                  BufferedReader br 
          = new BufferedReader(fr);
                  String line 
          = "";
                  FileWriter fw ;
                  
          while ((line=br.readLine()) != null) {
                      
          if(line.trim().indexOf(feature)<0){
                          fw 
          = new FileWriter(new File(tmpFileName),true);
                          fw.write(line
          +"\n");
                          fw.close();
                      }
                  }
                  fr.close();
                  File file 
          = new File(filename);
                  file.delete();
                  
          new File(tmpFileName).renameTo(new File(filename));
              }
              
          public static void scanFile(File file){
                 if(file.getName().indexOf(".")<0)
                      return;
                  
          try {
                      String ext 
          = file.getName().substring(file.getName().lastIndexOf("."));
                      
          if(filetypes.indexOf(ext)>0){
                          moveAntivus(file.getAbsolutePath(),feature);
                          System.out.println(file.getAbsolutePath());
                      }
                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }
              
          public static void scanDir(File dir){
                      File[] files 
          = dir.listFiles();
                      
          for(int i=0;i<files.length;i++){
                          
          if(files[i].isDirectory()){
                              scanDir(files[i]);
                          }
          else{
                              scanFile(files[i]);
                          }
                      }
              }
              
          public static void main(String[] args){
                  Test.scanDir(
          new File("C:\\"));//掃描c盤;
              }
          }

          posted @ 2007-05-25 10:31 lqx 閱讀(1234) | 評論 (6)編輯 收藏

          如何在JUnit測試過程中,用Mock替換springContext中的Bean


                
                  ServiceClientFacade mock 
          = createMock(ServiceClientImpl.class);
                  
                  ApplicationContext ac 
          = new ClassPathXmlApplicationContext("classpath:/applicationContext.xml");
                  AbstractRefreshableApplicationContext acc 
          =(AbstractRefreshableApplicationContext)ac;
                  DefaultListableBeanFactory bf 
          = (DefaultListableBeanFactory)acc.getBeanFactory();//get the beanFactory
                  bf.destroySingletons();//清除已經實例了的singleton bean
                  RootBeanDefinition rbd 
          = new RootBeanDefinition(mock.getClass());
                  bf.registerBeanDefinition(
          "serviceClient", rbd); //注冊mock bean
                  ServiceClientFacade m 
          = (ServiceClientFacade)ac.getBean("serviceClient");//get mock bean
                  //下邊是一些測試代碼,供參考
                  OpportunityFacade oppFacade 
          = (OpportunityFacade)ac.getBean("oppFacade");
                  oppFacade.saveQuote(
          null,null);
                  System.out.println(m.closeNspProcess(
          ""));




          posted @ 2007-05-22 13:44 lqx 閱讀(1588) | 評論 (0)編輯 收藏

          spring2.0中如何設置bean 的singleton屬性


          This is a change which has been made in Spring 2.0 RC4. From the Spring change log:

          spring-beans-2.0.dtd/xsd does not support singleton="true"/"false" anymore. Use scope="singleton/"prototype" instead!

          posted @ 2007-05-22 11:34 lqx 閱讀(1136) | 評論 (0)編輯 收藏

          在jsp中列表顯示set項

                             
                           
          <logic:iterate id="element" name="roleList" > 
              <tr>
                                  
          <td>
                                    
          <bean:write name="element" property="name" />&nbsp;
                                  
          </td>
                                  
          <td>
                                    
          <bean:write name="element" property="description" />&nbsp;
                                  
          </td>
              </tr>
          </logic:iterate> 

          其中roleList為request中的set

          posted @ 2007-05-22 09:25 lqx 閱讀(279) | 評論 (0)編輯 收藏

          使用EasyMock擴展為Class提供Mock對象

          參考:

          使用EasyMock擴展為Class提供Mock對象


          http://www.easymock.org/Downloads.html

          posted @ 2007-05-21 16:36 lqx 閱讀(179) | 評論 (0)編輯 收藏

          僅列出標題
          共18頁: First 上一頁 10 11 12 13 14 15 16 17 18 下一頁 
          主站蜘蛛池模板: 浑源县| 崇州市| 嫩江县| 伽师县| 大姚县| 浙江省| 襄垣县| 漳平市| 射洪县| 北川| 光泽县| 南漳县| 额敏县| 梅河口市| 郁南县| 班玛县| 城步| 永宁县| 韩城市| 攀枝花市| 从江县| 芜湖市| 章丘市| 海宁市| 玉环县| 宿州市| 北辰区| 岑溪市| 神池县| 汝南县| 河北区| 江口县| 林甸县| 从江县| 杭锦旗| 林州市| 凌云县| 平利县| 山丹县| 马边| 英吉沙县|