久热精品视频,成人精品一区二区三区免费,精品午夜视频http://www.aygfsteel.com/RongHao/category/5946.html勤學、勤思zh-cnMon, 20 Sep 2010 06:16:14 GMTMon, 20 Sep 2010 06:16:14 GMT60關于異常的問與答http://www.aygfsteel.com/RongHao/archive/2010/09/19/332478.htmlronghaoronghaoSun, 19 Sep 2010 14:03:00 GMThttp://www.aygfsteel.com/RongHao/archive/2010/09/19/332478.htmlhttp://www.aygfsteel.com/RongHao/comments/332478.htmlhttp://www.aygfsteel.com/RongHao/archive/2010/09/19/332478.html#Feedback1http://www.aygfsteel.com/RongHao/comments/commentRss/332478.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/332478.html閱讀全文

ronghao 2010-09-19 22:03 發表評論
]]>
我對異常的處理方式http://www.aygfsteel.com/RongHao/archive/2006/02/20/31654.htmlronghaoronghaoMon, 20 Feb 2006 07:36:00 GMThttp://www.aygfsteel.com/RongHao/archive/2006/02/20/31654.htmlhttp://www.aygfsteel.com/RongHao/comments/31654.htmlhttp://www.aygfsteel.com/RongHao/archive/2006/02/20/31654.html#Feedback2http://www.aygfsteel.com/RongHao/comments/commentRss/31654.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/31654.html而Exception定義的比較多一點,其實僅僅是類的簽名不同而已。它們表達了不期望的各種事件流,可以通過它們來部分的控制事件邏輯。比如很簡單的一個UnauthorizedException,告訴客戶沒有權限等等,調用捕捉到這個異常就會改變事件流到相應處理頁面提示用戶。

ronghao 2006-02-20 15:36 發表評論
]]>
Reflection的三個動態性質(轉)http://www.aygfsteel.com/RongHao/archive/2006/01/17/28322.htmlronghaoronghaoTue, 17 Jan 2006 09:41:00 GMThttp://www.aygfsteel.com/RongHao/archive/2006/01/17/28322.htmlhttp://www.aygfsteel.com/RongHao/comments/28322.htmlhttp://www.aygfsteel.com/RongHao/archive/2006/01/17/28322.html#Feedback0http://www.aygfsteel.com/RongHao/comments/commentRss/28322.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/28322.html一、執行期根據方法的名稱來執行方法
下面的示例演示了這一操作:
import java.lang.reflect.*;
public class method2 {
   
public int add(int a, int b) {
       
return a + b;
   }

   
public static void main(String args[]) {
       
try {
           Class cls 
= Class.forName("method2");
           Class partypes[] 
= new Class[2];
           partypes[
0= Integer.TYPE;
           partypes[
1= Integer.TYPE;
           Method meth 
= cls.getMethod("add", partypes);
           method2 methobj 
= new method2();
           Object arglist[] 
= new Object[2];
           arglist[
0= new Integer(37);
           arglist[
1= new Integer(47);
           Object retobj 
= meth.invoke(methobj, arglist);
           Integer retval 
= (Integer) retobj;
           System.out.println(retval.intvalue());
       }
 catch (Throwable e) {
           System.err.println(e);
       }

   }

}

注:上面劃線的粗體字最好用Object methobj =  cls.newInstance();來代替,原因很明顯如果這個類及方法事先都是清楚的也不需要用reflection了
    假如一個程序在執行的某處的時候才知道需要執行某個方法,這個方法的名稱是在程序的運行過程中指定的 (例如,JavaBean 開發環境中就會做這樣的事),那么上面的程序演示了如何做到。上例中,getMethod 用于查找一個具有兩個整型參數且名為 add 的方法。找到該方法并創建了相應的 Method 對象之后,在正確的對象實例中執行它。執行該方法的時候,需要提供一個參數列表,這在上例中是分別包裝了整數 37 和 47 的兩個 Integer 對象。執行方法的返回的同樣是一個 Integer 對象,它封裝了返回值 84。

二、執行期創建新的對象

對于構造器,則不能像執行方法那樣進行,因為執行一個構造器就意味著創建了一個新的對象 (準確的說,創建一個對象的過程包括分配內存和構造對象)。所以,與上例最相似的例子如下:

import java.lang.reflect.*;
public class constructor2 {
   
public constructor2() {
   }

   
public constructor2(int a, int b) {
       System.out.println(
"a = " + a + " b = " + b);
   }

   
public static void main(String args[]) {
       
try {
           Class cls 
= Class.forName("constructor2");
           Class partypes[] 
= new Class[2];
           partypes[
0= Integer.TYPE;
           partypes[
1= Integer.TYPE;
           Constructor ct 
= cls.getConstructor(partypes);
           Object arglist[] 
= new Object[2];
           arglist[
0= new Integer(37);
           arglist[
1= new Integer(47);
           Object retobj 
= ct.newInstance(arglist);
       }
 catch (Throwable e) {
           System.err.println(e);
       }

   }

}

三、改變字段(域)的值

reflection 的還有一個用處就是改變對象數據字段的值。reflection 可以從正在運行的程序中根據名稱找到對象的字段并改變它,下面的例子可以說明這一點:

import java.lang.reflect.*;
public class field2 {
   
public double d;
   
public static void main(String args[]) {
       
try {
           Class cls 
= Class.forName("field2");
           Field fld 
= cls.getField("d");
           field2 f2obj 
= new field2();
           System.out.println(
"d = " + f2obj.d);
           fld.setDouble(f2obj, 
12.34);
           System.out.println(
"d = " + f2obj.d);
       }
 catch (Throwable e) {
           System.err.println(e);
       }

   }

}
這個例子中,字段 d 的值被變為了 12.34。
實際開發時用Common BeanUtils

ronghao 2006-01-17 17:41 發表評論
]]>
用commons.fileupload實現文件的上傳和下載http://www.aygfsteel.com/RongHao/archive/2005/12/16/24158.htmlronghaoronghaoFri, 16 Dec 2005 02:46:00 GMThttp://www.aygfsteel.com/RongHao/archive/2005/12/16/24158.htmlhttp://www.aygfsteel.com/RongHao/comments/24158.htmlhttp://www.aygfsteel.com/RongHao/archive/2005/12/16/24158.html#Feedback1http://www.aygfsteel.com/RongHao/comments/commentRss/24158.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/24158.htmlcommons.fileupload實現文件的上傳,代碼如下:
<%!
 //服務器端保存上傳文件的路徑
    String saveDirectory = "g:\\upload\\";
    // 臨時路徑 一旦文件大小超過getSizeThreshold()的值時數據存放在硬盤的目錄
    String tmpDirectory = "g:\\upload\\tmp\\";
    // 最多只允許在內存中存儲的數據大小,單位:字節
    int maxPostSize = 1024 * 1024;
%>
<%
    // 文件內容 
    String FileDescription = null;
    // 文件名(包括路徑)
    String FileName = null;
    // 文件大小
    long FileSize = 0;
    // 文件類型
    String ContentType = null;

%>

<%
   DiskFileUpload fu = new DiskFileUpload();
    // 設置允許用戶上傳文件大小,單位:字節
   fu.setSizeMax(200*1024*1024);
    // 設置最多只允許在內存中存儲的數據,單位:字節
   fu.setSizeThreshold(maxPostSize);
    // 設置一旦文件大小超過getSizeThreshold()的值時數據存放在硬盤的目錄
   fu.setRepositoryPath("g:\\upload\\tmp\\");
    //開始讀取上傳信息 得到所有文件
   try{
      List fileItems = fu.parseRequest(request);
     }catch(FileUploadException e){
         //這里異常產生的原因可能是用戶上傳文件超過限制、不明類型的文件等
         //自己處理的代碼
     }
%>
<%
   // 依次處理每個上傳的文件
   Iterator iter = fileItems.iterator();
   while (iter.hasNext()) {
     FileItem item = (FileItem) iter.next();
       //忽略其他不是文件域的所有表單信息
     if (!item.isFormField()) {
       String name = item.getName();
       long size = item.getSize();
       String  contentType = item.getContentType();
     if((name==null||name.equals("")) && size==0)
       continue;
%>
<%
   //保存上傳的文件到指定的目錄
  String[] names=StringUtils.split(name,"\\");  //對原來帶路徑的文件名進行分割
   name = names[names.length-1];
   item.write(new File(saveDirectory+ name));
  }
}
%>
 下面是其簡單的使用場景:
 A、上傳項目只要足夠小,就應該保留在內存里。
 B、較大的項目應該被寫在硬盤的臨時文件上。
 C、非常大的上傳請求應該避免。
 D、限制項目在內存中所占的空間,限制最大的上傳請求,并且設定臨時文件的位置。
 
 可以根據具體使用用servlet來重寫,具體參數配置可以統一放置到一配置文件
 



 文件的下載用servlet實現
      public void doGet(HttpServletRequest request,
                       HttpServletResponse response)
     {
         String aFilePath = null;    //要下載的文件路徑
         String aFileName = null;    //要下載的文件名
         FileInputStream in = null;  //輸入流
         ServletOutputStream out = null;  //輸出流

         try
   {
          
             aFilePath = getFilePath(request);
             aFileName = getFileName(request);

             response.setContentType(getContentType(aFileName) + "; charset=UTF-8");
             response.setHeader("Content-disposition", "attachment; filename=" + aFileName);

             in = new  FileInputStream(aFilePath + aFileName); //讀入文件
            out = response.getOutputStream();
            out.flush();
            int aRead = 0;
           while((aRead = in.read()) != -1 & in != null)
        {
             out.write(aRead);
         }
           out.flush();
      }
       catch(Throwable e)
     {
     log.error("FileDownload doGet() IO error!",e);
      }
         finally
         {
             try
             {
                 in.close();
                 out.close();
             }
             catch(Throwable e)
             {
              log.error("FileDownload doGet() IO close error!",e);
             }
         }
     }



ronghao 2005-12-16 10:46 發表評論
]]>
[Jakarta Commons Cookbook 筆記] StringUtils類使用http://www.aygfsteel.com/RongHao/archive/2005/12/14/23823.htmlronghaoronghaoWed, 14 Dec 2005 04:47:00 GMThttp://www.aygfsteel.com/RongHao/archive/2005/12/14/23823.htmlhttp://www.aygfsteel.com/RongHao/comments/23823.htmlhttp://www.aygfsteel.com/RongHao/archive/2005/12/14/23823.html#Feedback1http://www.aygfsteel.com/RongHao/comments/commentRss/23823.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/23823.html  檢查字符串是否為空或null或僅僅包含空格
  String test = "";
  String test1=" ";
  String test2 = "\n\n\t";
  String test3 = null;
  System.out.println( "test blank? " + StringUtils.isBlank( test ) );
  System.out.println( "test1 blank? " + StringUtils.isBlank( test1 ) );
  System.out.println( "test2 blank? " + StringUtils.isBlank( test2 ) );
  System.out.println( "test3 blank? " + StringUtils.isBlank( test3 ) );
  運行結果:
  test blank? true
  test1 blank? true
  test2 blank? true
  test3 blank? true
  相對應的還有一個StringUtils.isNotBlank(String str)
  StringUtils.isEmpty(String str)則檢查字符串是否為空或null(不檢查是否僅僅包含空格)
 
  分解字符串
  StringUtils.split(null, *, *)            = null
  StringUtils.split("", *, *)              = []
  StringUtils.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
  StringUtils.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
  StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
  StringUtils.split("ab:cd:ef", ":", 1)    = ["ab:cd:ef"]
  StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
  StringUtils.split(String str,String separatorChars,int max) str為null時返回null
  separatorChars為null時默認為按空格分解,max為0或負數時分解沒有限制,max為1時返回整個字符串,max為分解成的個數(大于實際則無效)
 
  去除字符串前后指定的字符
  StringUtils.strip(null, *)          = null
  StringUtils.strip("", *)            = ""
  StringUtils.strip("abc", null)      = "abc"
  StringUtils.strip(" abc ", null)    = "abc"
  StringUtils.strip("  abcyx", "xyz") = "  abc"
  StringUtils.strip(String str,String stripChars) str為null時返回null,stripChars為null時默認為空格

  創建醒目的Header(調試時用)
  public String createHeader( String title ) {
    int width = 30;
    String stars = StringUtils.repeat( "*", width);
    String centered = StringUtils.center( title, width, "*" );
    String heading = StringUtils.join(new Object[]{stars, centered, stars}, "\n");
    return heading;
  }
  調用createHeader("TEST")的輸出結果:
  ******************************
  ************ TEST ************
  ******************************

  字符的全部反轉及以單個詞為單位的反轉
  String original = "In time, I grew tired of his babbling nonsense.";
  StringUtils.reverse( original )   = ".esnesnon gnilbbab sih fo derit werg I ,emit nI"
  以單個詞為單位的反轉
  public Sentence reverseSentence(String sentence) {
    String reversed = StringUtils.chomp( sentence, "." );
    reversed = StringUtils.reverseDelimited( reversed, ' ' );
    reversed = reversed + ".";
    return reversed;
  }
  String sentence = "I am Susan."
  reverseSentence( sentence ) )   = "Susan am I."

  檢查字符串是否僅僅包含數字、字母或數字和字母的混合
  String test1 = "ORANGE";
  String test2 = "ICE9";
  String test3 = "ICE CREAM";
  String test4 = "820B Judson Avenue";
  String test5 = "1976";
  結果:
  boolean t1val = StringUtils.isAlpha( test1 ); // returns true
  boolean t2val = StringUtils.isAlphanumeric( test2 ); // returns true
  boolean t3val = StringUtils.isAlphaSpace( test3 ); // returns true
  boolean t4val = StringUtils.isAlphanumericSpace( test4 ); // returns true
  boolean t5val = StringUtils.isNumeric( test5 ); // returns true



ronghao 2005-12-14 12:47 發表評論
]]>
[Jakarta Commons Cookbook 筆記] ArrayUtils類使用http://www.aygfsteel.com/RongHao/archive/2005/12/13/23712.htmlronghaoronghaoTue, 13 Dec 2005 10:48:00 GMThttp://www.aygfsteel.com/RongHao/archive/2005/12/13/23712.htmlhttp://www.aygfsteel.com/RongHao/comments/23712.htmlhttp://www.aygfsteel.com/RongHao/archive/2005/12/13/23712.html#Feedback4http://www.aygfsteel.com/RongHao/comments/commentRss/23712.htmlhttp://www.aygfsteel.com/RongHao/services/trackbacks/23712.html  primitive 數組克隆及反轉排序
  long[] array = { 1, 3, 2, 3, 5, 6 };
  long[] reversed = ArrayUtils.clone( array );
  ArrayUtils.reverse( reversed );
  System.out.println( "Original: " + ArrayUtils.toString( array ) );   //打印
  System.out.println( "Reversed: " + ArrayUtils.toString( reversed ) );
 
  對象數組克隆及反轉排序
  Long[] array = new Long[] { new Long(3), new Long(56), new Long(233) };
  Long[] reversed = ArrayUtils.clone( array );
  ArrayUtils.reverse( reversed );
 
  primitive 數組與對象數組之間的轉換
  long[] primitiveArray = new long[] { 12, 100, 2929, 3323 };
  Long[] objectArray = ArrayUtils.toObject( primitiveArray );
  Double[] doubleObjects = new Double[] { new Double( 3.22, 5.222, 3.221 ) };
  double[] doublePrimitives = ArrayUtils.toPrimitive( doubleObject );
  注意:對象數組可以含有null元素,primitive 數組則不容許含有null元素,所以對象數組轉換為primitive 數組時,可以添入第二個參數,當碰到為null的元素時用其代替(如下,Double.NaN)。如果不添入第二個參數,當碰到為null的元素時,則會拋出NullPointerException 。
  double[] result = ArrayUtils.toPrimitive( resultObjArray, Double.NaN  );
 
  查找一個數組中是否含有特定的元素(查找對象數組時,比較的是對象的equals()方法),及特定元素的第一次出現位置和最后一次出現位置
  String[] stringArray = { "Red", "Orange", "Blue", "Brown", "Red" };
  boolean containsBlue = ArrayUtils.contains( stringArray, "Blue" );
  int indexOfRed = ArrayUtils.indexOf( stringArray, "Red");
  int lastIndexOfRed = ArrayUtils.lastIndexOf( string, "Red" ); 
 
  由二維對象數組創建一個 Map
  Object[] weightArray =
    new Object[][] { {"H" , new Double( 1.007)},
                     {"He", new Double( 4.002)},
                     {"Li", new Double( 6.941)},
                     {"Be", new Double( 9.012)},
                     {"B",  new Double(10.811)},
                     {"C",  new Double(12.010)},
                     {"N",  new Double(14.007)},
                     {"O",  new Double(15.999)},
                     {"F",  new Double(18.998)},
                     {"Ne", new Double(20.180)} };

  Map weights = ArrayUtils.toMap( weightArray );
  Double hydrogenWeight = (Double)weights.get( "H" );
  注意:當二維對象數組"key"值重復時,創建的Map,后面的鍵-值對會把前面的覆蓋掉



ronghao 2005-12-13 18:48 發表評論
]]>
主站蜘蛛池模板: 吉安县| 耒阳市| 青龙| 慈利县| 宕昌县| 陇南市| 古田县| 车致| 固始县| 福州市| 施甸县| 滦南县| 梅河口市| 滨州市| 西峡县| 高唐县| 孟津县| 平潭县| 丹阳市| 司法| 西峡县| 南昌市| 云南省| 临泽县| 湘西| 定结县| 错那县| 济南市| 涡阳县| 乌鲁木齐市| 曲靖市| 娄烦县| 郧西县| 铁岭县| 南部县| 浪卡子县| 上蔡县| 天全县| 旬邑县| 图木舒克市| 宾阳县|