俊星的BLOG

          #

          在ubuntu上手工安裝JDK

          ubuntu上安裝JDK的方法有多種,我選擇的是手工安裝的方式,下面是安裝過程,以備忘:
          1:下載安裝文件,如jdk-6u14-linux-i586.bin
          2:開始安裝,使用到的命令有:
          // 切換到管理員
          sudo su

          // 刪除文件
          rm jdk-6u14-linux-i586.bin 

          // 級聯刪除文件夾
          rm -rf jdk1
          .6.0_14/

          // 拷貝文件
          cp /home/kinkding/download/jdk-6u14-linux-i586.bin ./

          // 拷貝文件夾
          cp -rf /home/kinkding/dev/jdk1
          .6.0_14 /usr/java/

          // 授予文件執行權限
          chmod +x jdk-6u14-linux-i586.bin 

          // 執行文件
          ./jdk-6u14-linux-i586.bin 

          // 編輯profile文件按
          gedit /etc/profile

          // 在profile中添加的語句,設置環境變量(一般放置在文件尾部,umask 022之前)
          export JAVA_HOME
          =/usr/java/jdk1.6.0_14
          export CLASSPATH
          =$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
          export PATH
          =$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOME/bin

          注意:修改/etc/profile之后,需要注銷重登陸才生效。

          posted @ 2009-06-05 12:54 俊星 閱讀(458) | 評論 (0)編輯 收藏

          JAVA單向加密(MD5,SHA,MAC)

          具體代碼如下:
          import java.security.MessageDigest;

          import javax.crypto.KeyGenerator;
          import javax.crypto.Mac;
          import javax.crypto.SecretKey;
          import javax.crypto.spec.SecretKeySpec;

          import sun.misc.BASE64Decoder;
          import sun.misc.BASE64Encoder;

          /**
           * 加密(主要有:MD4,SHA,MAC)
           * 
           * 
          @author kinkding
           * @history 2009-6-3
           
          */

          public class MyEncrypt {
              
          /** MD5 加密 */
              
          public static byte[] encryptMD5(byte[] data) throws Exception {
                  MessageDigest md5 
          = MessageDigest.getInstance("MD5");
                  md5.update(data);
                  
          return md5.digest();
              }


              
          /** SHA 加密 */
              
          public static byte[] encryptSHA(byte[] data) throws Exception {
                  MessageDigest sha 
          = MessageDigest.getInstance("SHA");
                  sha.update(data);
                  
          return sha.digest();
              }


              
          /** 取得HMAC密鑰 */
              
          public static String getMacKey() throws Exception {
                  KeyGenerator keyGenerator 
          = KeyGenerator.getInstance("HmacMD5");
                  SecretKey secretKey 
          = keyGenerator.generateKey();
                  
          return new BASE64Encoder().encode(secretKey.getEncoded());
              }


              
          /** 執行加密 */
              
          public static byte[] encryptHMAC(byte[] data, String key) throws Exception {
                  
          byte[] bkey = new BASE64Decoder().decodeBuffer(key);
                  SecretKey secretKey 
          = new SecretKeySpec(bkey, "HmacMD5");
                  Mac mac 
          = Mac.getInstance(secretKey.getAlgorithm());
                  mac.init(secretKey);
                  
          return mac.doFinal(data);
              }


              
          private static String toHex(byte[] buffer) {
                  StringBuffer sb 
          = new StringBuffer(buffer.length * 3);
                  
          for (int i = 0; i < buffer.length; i++{
                      sb.append(Character.forDigit((buffer[i] 
          & 0xf0>> 416));
                      sb.append(Character.forDigit(buffer[i] 
          & 0x0f16));
                  }

                  
          return sb.toString();
              }


              
          public static void main(String[] args) {
                  String msg 
          = "生活真好^_^";
                  
          byte[] data = msg.getBytes();
                  
          try {
                      System.out.println(
          "msg:" + msg);
                      System.out.println(
          "md5:" + toHex(encryptMD5(data)));
                      System.out.println(
          "sha:" + toHex(encryptSHA(data)));
                      String key 
          = getMacKey();
                      System.out.println(
          "mac key:" + key);
                      System.out.println(
          "mac:" + toHex(encryptHMAC(data, key)));
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }


              }

          }


          運行效果如下:
          msg:生活真好^_^
          md5:e0649dfaef57789734e920c7ecb9c4ea
          sha:a4bd855836de26b2323778b797629fed4416f12f
          mac key:zBPe28oho2H84+Mg8mF4abpd0MQvdjgqgFdX4hmUQQbOGnX1aFq/oQnogsHVIczgx1AZ1s2/ncPz
          tBQIGLZUnw
          ==
          mac:87f4140161ad43797059e85dd9962897

          posted @ 2009-06-03 23:20 俊星 閱讀(1269) | 評論 (0)編輯 收藏

          WINDOWS和LINUX平臺獲取MAC地址

          Windows平臺:

          主要是通過執行“ipconfig /all”命令實現:
              public String getMyMACAddress() {
                  String macAddr 
          = "";
                  
          try {
                      Process p 
          = Runtime.getRuntime().exec("ipconfig /all");
                      BufferedReader reader 
          = new BufferedReader(new InputStreamReader(p.getInputStream()));
                      String line 
          = null;
                      
          while ((line = reader.readLine()) != null{
                          
          if (line.indexOf("Physical Address"> 0{
                              macAddr 
          = line.substring(line.indexOf(":"+ 1, line.length()).trim();
                              
          break;
                          }

                      }

                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }

                  
          return macAddr;
              }

          Linux下的方式為:

          posted @ 2009-06-03 20:39 俊星 閱讀(344) | 評論 (0)編輯 收藏

          關于lisence的討論

          今天在JAVA EYE上看到了一篇關于授權的討論,http://www.javaeye.com/topic/399622?page=1 ,下面是我看法:

          1. 獲取過程,需要聯網注冊,lisence 暫時包含兩個屬性:用戶數、到期時間
          2. 激活,也需要聯網,修改lisence的兩個屬性,如用戶數遞減,需要特殊處理的是同一用戶可以多次激活
          3. 正常運行后,在本地保存激活信息,不需要再次聯網,之后每次啟動時檢查到期時間

          稍后有時間的話,將寫一個DEMO來試驗一下。

          posted @ 2009-06-02 23:55 俊星 閱讀(224) | 評論 (0)編輯 收藏

          SWT試用之控制鼠標鍵盤

          通過SWT可以控制鼠標鍵盤事件,具體如下:
          import org.eclipse.swt.SWT;
          import org.eclipse.swt.widgets.Display;
          import org.eclipse.swt.widgets.Event;
          import org.eclipse.swt.widgets.Shell;
          import org.eclipse.swt.widgets.Text;

          /**
           * 預期的運行效果為:顯示開始菜單
           * 
          @author kinkding
           * @history 2009-6-2
           
          */

          public class MyEventRes {

              
          public static void main(String[] args) {
                  
          final Display display = new Display();
                  
          final Shell shell = new Shell(display);
                  
          final Text text = new Text(shell, SWT.BORDER);
                  text.setSize(text.computeSize(
          150, SWT.DEFAULT));
                  text.setText(
          "神一樣的人啊!");
                  shell.pack();
                  shell.open();

                  
          new KeyThread(display).start(); // 鍵盤響應
                  
          // new MouseThread(display).start(); // 鼠標響應
                  while (!shell.isDisposed()) {
                      
          if (!display.readAndDispatch())
                          display.sleep();
                  }

                  display.dispose();
              }

          }


          class MouseThread extends Thread {
              Display display;
              
          int h;

              
          public MouseThread(Display display) {
                  
          this.display = display;
                  h 
          = display.getPrimaryMonitor().getBounds().height;
              }


              
          public void run() {
                  Event event 
          = new Event();
                  
          // 移動鼠標
                  event.type = SWT.MouseMove;
                  event.x 
          = 5;
                  event.y 
          = h - 5;
                  display.post(event);
                  
          try {
                      Thread.sleep(
          100);
                  }
           catch (InterruptedException e) {
                  }

                  
          // 按下右鍵
                  event.type = SWT.MouseDown;
                  event.button 
          = 1;
                  display.post(event);
                  
          try {
                      Thread.sleep(
          100);
                  }
           catch (InterruptedException e) {
                  }

                  
          // 恢復
                  event.type = SWT.MouseUp;
                  display.post(event);
              }

          }


          class KeyThread extends Thread {
              Display display;
              
          int h;

              
          public KeyThread(Display display) {
                  
          this.display = display;
                  h 
          = display.getPrimaryMonitor().getBounds().height;
              }


              
          public void run() {
                  Event event 
          = new Event();
                  
          // 按下CTRL
                  event.type = SWT.KeyDown;
                  event.keyCode 
          = SWT.CTRL;
                  display.post(event);
                  
          try {
                      Thread.sleep(
          100);
                  }
           catch (InterruptedException e) {
                  }

                  
          // 按下ESC
                  event.type = SWT.KeyDown;
                  event.keyCode 
          = SWT.ESC;
                  display.post(event);
                  
          try {
                      Thread.sleep(
          100);
                  }
           catch (InterruptedException e) {
                  }

                  
          // 恢復
                  event.type = SWT.KeyUp;
                  event.keyCode 
          = SWT.CTRL;
                  display.post(event);
                  
          try {
                      Thread.sleep(
          100);
                  }
           catch (InterruptedException e) {
                  }

                  event.type 
          = SWT.KeyUp;
                  event.keyCode 
          = SWT.ESC;
                  display.post(event);
              }

          }

          posted @ 2009-06-02 00:15 俊星 閱讀(823) | 評論 (0)編輯 收藏

          僅列出標題
          共10頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 
          主站蜘蛛池模板: 梁平县| 门头沟区| 云梦县| 宝丰县| 浮山县| 成都市| 关岭| 石狮市| 彝良县| 镇康县| 寿阳县| 仪征市| 凯里市| 旺苍县| 南部县| 武汉市| 大丰市| 启东市| 若羌县| 乐都县| 岳阳市| 绩溪县| 浮山县| 灵川县| 麻城市| 明水县| 永登县| 阜宁县| 大关县| 孙吴县| 长武县| 青冈县| 昂仁县| 扎兰屯市| 越西县| 湾仔区| 五大连池市| 清徐县| 泗水县| 丰城市| 郑州市|